prevent you from having to POSTBACK to the server to access validator’s properties.

Source : http://forums.asp.net/p/839953/840084.aspx

This example shows how to validate the LastName textbox but only if the FirstName textbox contains a value. The first part is simply HTML tags with the form controls, the second part is simply the code for “onblur” even in the Page_Load event handler.

FIRST HTML
*****************************************************************************
<%@ Page Language=”vb” AutoEventWireup=”false” Codebehind=”WebForm1.aspx.vb” Inherits=”ValidationChange.WebForm1″%>
<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.0 Transitional//EN”>
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name=”GENERATOR” content=”Microsoft Visual Studio .NET 7.1″>
<meta name=”CODE_LANGUAGE” content=”Visual Basic .NET 7.1″>
<meta name=”vs_defaultClientScript” content=”JavaScript”>
<meta name=”vs_targetSchema” content=”http://schemas.microsoft.com/intellisense/ie5″>
</HEAD>
<body MS_POSITIONING=”GridLayout”>
<form id=”Form1″ method=”post” runat=”server” autocomplete=”off”>
<TABLE id=”Table1″ cellSpacing=”1″ cellPadding=”1″ width=”300″ border=”1″>
<TR>
<TD>
<asp:Label id=”Label1″ runat=”server”>First:</asp:Label></TD>
<TD>
<asp:TextBox id=”txtFirstName” runat=”server”></asp:TextBox></TD>
</TR>
<TR>
<TD>
<asp:Label id=”Label2″ runat=”server”>Last:</asp:Label></TD>
<TD>
<asp:TextBox id=”txtLastName” runat=”server”></asp:TextBox></TD>
</TR>
<TR>
<TD></TD>
<TD>
<asp:Button id=”Button1″ runat=”server” Text=”Test”></asp:Button></TD>
</TR>
<TR>
<TD></TD>
<TD>
<asp:RequiredFieldValidator id=”rfLastName” runat=”server” ErrorMessage=”Please enter a last name” ControlToValidate=”txtLastName”
Enabled=”False” Display=”None”></asp:RequiredFieldValidator></TD>
</TR>
<TR>
<TD></TD>
<TD>
<asp:ValidationSummary id=”vsPageValid” runat=”server” DisplayMode=”List” ShowMessageBox=”True” ShowSummary=”False”></asp:ValidationSummary></TD>
</TR>
</TABLE>
</form>
<script language=”javascript”>

function EnableLastNameValidation()
{
if(document.Form1.txtFirstName.value.length > 0)
ValidatorEnable(rfLastName, true);
else
ValidatorEnable(rfLastName, false);
}
</script>
</body>
</HTML>

*****************************************************************************
SECOND CODE-BEHIND
*****************************************************************************
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
‘Put user code to initialize the page here

With txtFirstName
.Attributes.Add(“onblur”, “EnableLastNameValidation()”)
End With
End Sub
*****************************************************************************

As you can see, I simply call the ASP.NET built in ValidatorEnable function that comes with .NET. This will prevent you from having to “post back” to the server to access validator’s properties.

Post a Comment

Your email is never published nor shared. Required fields are marked *
*
*