Disable/Enable ASP.Net Validators on client-side as well as Server-side.

The Scenario:
There are many scenarios wherein we switch the visibility of either Panel control OR div sections.
However if we have any validator say a RequiredFieldValidator in any of those hidden panel/div sections, then we need to disable those too, — both client side as well as server side. That way when the request gets executed on server side, the Page.IsValid call succeeds.
The Solution:
The main idea in here is — I use ValidatorEnable function at the client side to disabe Validation, but that doesn’t stop the server side validation.
The ASP.Net engine will still fire that required field validator & Page.IsValid will fail. So we need to disable that too on the server side.
And when my all logic is done , I again enable those validators.
Here’s a simple way to do it.
(a) Some partial relevant html
<script language=”javascript” type=”text/javascript”>
function f1() {
//Hide others
$get(‘<%= lOtherCountry.ClientID %>’).style.display = ‘none’;
$get(‘<%= lUsInfo.ClientID %>’).style.display = ‘inline’;
ValidatorEnable($get(‘<%= ReqCountryCity.ClientID %>’), false);
ValidatorEnable($get(‘<%= ReqProvince.ClientID %>’), false);

function f2() {
//Hide Current states
$get(‘<%= lUsInfo.ClientID %>’).style.display = ‘none’;
$get(‘<%= lOtherCountry.ClientID %>’).style.display = ‘inline’;
ValidatorEnable($get(‘<%= ReqZip.ClientID %>’), false);
ValidatorEnable($get(‘<%= ReqCity.ClientID %>’), false);

<asp:UpdatePanel ID=”UpdatePanel1″ runat=”server” UpdateMode=”Conditional”><ContentTemplate>
<asp:TextBox ID=”txtAddress1″ runat=”server” MaxLength=”150″></asp:TextBox>
<asp:RequiredFieldValidator ID=”ReqAddr1″ runat=”server” ErrorMessage=”Address1 Required” ControlToValidate=”txtAddress1″ ForeColor=”Red” Display=”Dynamic” Text=”*”></asp:RequiredFieldValidator>

<li class=”usinfo” id=”lUsInfo” runat=”server”>
<div class=”enterzip”><label for=”zip”>Enter Zip Code:</label>
<asp:TextBox ID=”txtZip” runat=”server” CssClass=”smfield” MaxLength=”5″></asp:TextBox>
<asp:RequiredFieldValidator ID=”ReqZip” runat=”server” ErrorMessage=”Zip Required” ControlToValidate=”txtZip” ForeColor=”Red” Display=”Dynamic” Text=”*”></asp:RequiredFieldValidator>
<label for=”city”>City:</label>
<asp:TextBox ID=”txtCity” runat=”server”></asp:TextBox>
<asp:RequiredFieldValidator ID=”ReqCity” runat=”server” ErrorMessage=”City Required” ControlToValidate=”txtCity” ForeColor=”Red” Display=”Dynamic” Text=”*”></asp:RequiredFieldValidator>

<li class=”othercountry” id=”lOtherCountry” runat=”server” >
<li><label for=”countrycity”>City:</label>
<asp:TextBox ID=”txtCountryCity” runat=”server”></asp:TextBox>
<asp:RequiredFieldValidator ID=”ReqCountryCity” runat=”server” ErrorMessage=”City Required” ForeColor=”Red” ControlToValidate=”txtCountryCity” Display=”Dynamic” Text=”*”></asp:RequiredFieldValidator>
<label for=”province”>State/Province:</label>
<asp:TextBox ID=”txtProvince” runat=”server”></asp:TextBox>
<asp:RequiredFieldValidator ID=”ReqProvince” runat=”server” ErrorMessage=”Province Required” ForeColor=”Red” ControlToValidate=”txtProvince” Display=”Dynamic” Text=”*”></asp:RequiredFieldValidator>
<asp:Button ID=”btnsubmit” runat=”server” Text=”Hide others” OnClientClick=”f1();” onclick=”btnsubmit_Click”/>
<asp:Button ID=”Button1″ runat=”server” Text=”Hide US” OnClientClick=”f2();” onclick=”Button1_Click” />
(b)Now in codebehind file —
protected void btnsubmit_Click(object sender, EventArgs e) //hide others
if (!Page.IsValid)
int r = 3;

private void SetOtherCountryVals(bool nVisible)
ReqCountryCity.IsValid = !nVisible;
ReqCountryCity.Enabled = false;
ReqProvince.IsValid = !nVisible;
ReqProvince.Enabled = false;

private void SetUSVals(bool nVisible)
ReqZip.IsValid = !nVisible;
ReqZip.Enabled = false;
ReqCity.IsValid = !nVisible;
ReqCity.Enabled = false;

private void EnableAll()
ReqCountryCity.Enabled = true;
ReqProvince.Enabled = true;
ReqZip.Enabled = true;
ReqCity.Enabled = true;

protected void Button1_Click(object sender, EventArgs e)
if (!Page.IsValid)
int r1 = 3;
Hope this helps. Thanks for reading the post.

This entry was posted in Uncategorized. Bookmark the permalink.

One Response to Disable/Enable ASP.Net Validators on client-side as well as Server-side.

  1. cseppd says:

    Thx, it works.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s