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:
<DefaultProperty("Text"),
ToolboxData("<{0}:PercentageTextBox runat="server"></{0}:PercentageTextBox>")> _
Public Class PercentageTextBox : Inherits TextBox
Implements IValidator
Private _validateInput As Boolean = True
Public Overridable Property IsRequired As Boolean
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
End Sub
End Class
When I first started I defined the initialisation in the custom control as such:
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: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!