Click here to Skip to main content
15,910,980 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello wonderful people of Code Project!

So I've created a custom web control that implements IValidator; however I'm a bit stuck on changing some of the logic of my validation code and was wondering if anyone had any ideas how I could proceed.

The gist of my control is as follows:
VB
<DefaultProperty("Text"),
ToolboxData("<{0}:PercentageTextBox  runat="server"></{0}:PercentageTextBox>")> _
Public Class PercentageTextBox : Inherits TextBox
    Implements IValidator

    Private _validateInput As Boolean = True 'Used in Validate()
    Public Overridable Property IsRequired As Boolean 'Used in Validate()

    Public Property isValid As Boolean Implements System.Web.UI.IValidator.IsValid
    Public Property ErrorMessage As String Implements   
    System.Web.UI.IValidator.ErrorMessage

    '...

    Public Sub Validate() Implements System.Web.UI.IValidator.Validate
        'VALIDATION LOGIC HERE RETURNS A VALUE FOR ErrorMessage WHEN FAILED
        '...
    End Sub

    '...

End Class


When I first started I defined the initialisation in the custom control as such:
VB
Protected Overrides Sub OnInit(e As System.EventArgs)
    MyBase.OnInit(e)
    Page.Validators.Add(Me)
End Sub


So I was associating the validation of the control with the validation of the page. This was fine at first as I was using the control for an individual task on a page. Now I've hit inevitable situation where I'm using several of these controls across a single page which means all controls representing the validation of the page itself, a behaviour I do no longer want.

I think what I want to do is:

1) Remove the control validator from the validation of the page in the initialisation.

2) Define a custom validator in the markup that calls the Validate routine. This is the one I'm a bit stuck on. I'm having difficulty linking the custom validator to use the code behind the custom control.

In my markup I have:
ASP.NET
<asp:ValidationSummary ID="vs1" 
    runat="server" ValidationGroup="vg1" ForeColor="Red" />

<cc1:PercentageTextBox ID="ptb1"  runat="server" BoundValue='<%# Bind("someboundvalue")%>' ValidationGroup="vg1" ValidateInput="true" IsRequired="true"/>

<asp:CustomValidator ID="cv1" runat="server" ClientValidationFunction="Validate" />

However this doesn't seem to invoke the Validator in the PercentageTextBox anymore.

I hope I've explained enough of what's going on here. Any questions, tips or advice as to what I could do would be appreciated.

Thanks!
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900