Partially Validate Page In ASP.NET
Sometimes, when a postback happens, instead of explicitly or implicitly calling the Page.Validate(), we just want to partially validate the page, or say we only want certain validators to be executed. For example, we try to do duplication check of a email address or a phone number.
I'll demostrate different solutions for validating whether an entered email address is a dup one.
Solution 1
I have the following controls on my page,
<asp:textbox id="txtEmail" runat="server" MaxLength="50" AutoPostBack="True"></asp:textbox><BR>
<asp:requiredfieldvalidator id="rvEmail" runat="server" controltovalidate="txtEmail" errormessage="Email can not be empty."
Display="None"></asp:requiredfieldvalidator>
<asp:regularexpressionvalidator id="covEmailRegExp" runat="server" errormessage="Email is invalid." Display="None"
ControlToValidate="txtEmail" ValidationExpression=".*@.*\..*"></asp:regularexpressionvalidator>
<asp:customvalidator id="cuvEmail" runat="server" Display="None" ControlToValidate="txtEmail" ErrorMessage="Dup Email."></asp:customvalidator>
Please notice, the textbox txtEmail's Autopostback property is set to true, as a result, everytime when the value of .txtEmail's value is changed, a postback is caused.
In the code behind file, the event of txtEmail.TextChanged is handled this way,
Private Sub txtEmail_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtEmail.TextChanged
rvEmail.Validate()
covEmailRegExp.Validate()
cuvEmail.Validate()
End Sub
The txtEmail.TextChanged event doesn't trigger Page.Validate() if Page.Validate() is not explicitly called in Page_Load().
This solution looks simple.
Solution 2
Sometimes, we don't like txtEmail's Autopostback=true because of this or that reason. In this following example, I use a LinkButton lbtnChkDup to trigger the page postback. By this way, we put the flexibility of when and whether to check the dup the user's hand.
I have the following controls on my page,
<asp:textbox id="txtEmail" runat="server" MaxLength="50"></asp:textbox><BR>
<asp:LinkButton id="lbtnChkDupe" runat="server" CausesValidation="False">Check Dup</asp:LinkButton>
<asp:requiredfieldvalidator id="rvEmail" runat="server" controltovalidate="txtEmail" errormessage="Email can not be empty." Display="None"></asp:requiredfieldvalidator>
<asp:regularexpressionvalidator id="covEmailRegExp" runat="server" errormessage="Email is invalid." Display="None" ControlToValidate="txtEmail" ValidationExpression=".*@.*\..*"></asp:regularexpressionvalidator>
<asp:customvalidator id="cuvEmail" runat="server" Display="None" ControlToValidate="txtEmail" ErrorMessage="Dup Email."></asp:customvalidator>
Please notice, the textbox txtEmail's Autopostback property is the default(false). The clicking of the LinkButton leads to a postback. However, for the LinkButton, the CausesValidation property has to be set to False; or else, the validation process is out of our control.
In the code behind file, there are two ways to implement the validation process. Well, perhaps you have your own ways.
First one is, I have lines of code for the event of lbtnChkDupe.Click,
Private Sub lbtnChkDupe_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lbtnChkDupe.Click
rvEmail.Validate()
covEmailRegExp.Validate()
cuvEmail.Validate()
End Sub
Second one is to override RaisePostBackEvent,
Protected Overrides Sub RaisePostBackEvent(ByVal sourceControl As System.Web.UI.IPostBackEventHandler, ByVal
eventArgument As String)
Dim MyControl As Control = sourceControl
If (TypeOf sourceControl Is LinkButton) AndAlso (MyControl.ClientID = "lbtnChkDupe") Then
rvEmail.Validate()
covEmailRegExp.Validate()
cuvEmail.Validate()
Else
sourceControl.RaisePostBackEvent(eventArgument)
End If
End Sub
If the RaisePostBackEvent is overrided, then lbtnChkDupe_Click has no opportunity to shine in this example, because sourceControl.RaisePostBackEvent() is not triggered.
Here is the sequence how events are called when lbtnChkDupe is clicked,
Page_Load()
txtEmail_TextChanged(), if it is defined and the text is changed.
Overrides RaisePostBackEvent(), if it is defined.
lbtnChkDupe_Click(), if it is defined and PostBackEvent is raised(not in this example)
Apparently, to improve perfomance,
rvEmail.Validate()
covEmailRegExp.Validate()
cuvEmail.Validate()
only need to be called for once in any of them.