How to cancel client side validation control from server when page loads

  • Thread starter Thread starter moondaddy
  • Start date Start date
M

moondaddy

I'm running vb.net 1.1 and have an aspx page with some textboxes and
required validation controls on it. Depending certain conditions (server
side) I'm setting a few of the textboxes to not visible when the page loads,
however, these textboxes still have a required validation control associated
with them. Therefore when the user updates the page and re-submits it, the
page is invalid because these hidden textboxes have no values in them. Is
there a way I can disable these validation controls on the page load event?

Thanks.
 
Yes, on the page_Load event I set this property to false and also
EnableClientScript=False. Those are the only 2 I could see that migh effect
it but neither setting prevented the validation from occuring and setting
the page to invalid.

--
(e-mail address removed)
Rick Spiewak said:
Have you tried setting the enabled property of the validation control to
false?
 
Post some code, moondaddy.


moondaddy said:
Yes, on the page_Load event I set this property to false and also
EnableClientScript=False. Those are the only 2 I could see that migh effect
it but neither setting prevented the validation from occuring and setting
the page to invalid.
 
In the page_Load event I have this code in an effor to stop the control from
validating on the client:

Me.reqFName.Enabled = False
Me.reqFName.EnableClientScript = False

and here's the html in the aspx page. Both the textbox being validated and
the validation control are in a asp control table cell:

<asp:TableRow>
<asp:TableCell VerticalAlign="Top" HorizontalAlign="Right">

<asp:TextBox runat="server" Width="285px" CssClass="clsLgnTxBx"
ID="txtFName" EnableViewState="False"></asp:TextBox>

<asp:RequiredFieldValidator runat="server" Display="None"
ControlToValidate="txtFName" ErrorMessage="First name is required"
ID="reqFName"></asp:RequiredFieldValidator>

</asp:TableCell>
</asp:TableRow>
 
Hi Moondaddy,

From the code and page template snipet you provided, you used the ASP.NET
Table control to contain the TextBoxes and Validator controls. There is a
potential problem that if you add ASP.NET table control first and then add
the TextBoxes into its cell directly by code the html tag such as
<asp:TextBox ...> in the aspx page template, then when you change to the
codebehind, the IDE won't generate page member for the Textboxes you added
in the ASP.NET Table control. I'm not sure whether this is the cause of the
problem, but you can have a check in the page's code behind to see whether
there is the member declaraion for the TextBox control which validation
control it to validate. Also, I also suggest you try the "HtmlTable"
Control just the
<table id="" runat=server> , that'll be more familiar with use, do you
think so.

As for to disable(make a validator control not work both serverside or
clientside), I think set its Enabled property as false is enougth. So if
this not work, there must be something else cause the problem. In addition,
below is a demo page I tested on my side, you can have a test on it on your
side and compare with your page to see whether it can provide any clues.
--------------------------aspx page-----------------------------
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>RequiredValidator</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>
<form id="Form1" method="post" runat="server">
<table width="100%" align="center">
<tr>
<td>
<asp:LinkButton id="lnkTabOne" runat="server">TabOne</asp:LinkButton>
</td>
<td>
<asp:LinkButton id="lnkTabTwo" runat="server">TabTwo</asp:LinkButton>
</td>
<td>
<asp:LinkButton id="lnkTabThree"
runat="server">TabThree</asp:LinkButton>
</td>
</tr>
<tr>
<td colspan="3">
<table width="100%" align="center" id="tbOne" runat="server">
<tr>
<td>
TextBox1:<asp:TextBox id="TextBox1" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator id="rfv1" runat="server"
ErrorMessage="TextBox1 Empty"
ControlToValidate="TextBox1"></asp:RequiredFieldValidator>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td colspan="3">
<table width="100%" align="center" id="tbTwo" runat="server">
<tr>
<td>
TextBox2:<asp:TextBox id="TextBox2" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator id="rfv2" runat="server"
ErrorMessage="TextBox2 Empty"
ControlToValidate="TextBox2"></asp:RequiredFieldValidator>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td colspan="3">
<table width="100%" align="center" id="tbThree" runat="server">
<tr>
<td>
TextBox3:<asp:TextBox id="TextBox3" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator id="rfv3" runat="server"
ErrorMessage="TextBox3 Empty"
ControlToValidate="TextBox3"></asp:RequiredFieldValidator>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td colspan="3">
<asp:Button id="btnSubmit" runat="server" Text="Submit"></asp:Button>
</td>
</tr>
</table>
</form>
</body>
</HTML>

------------------code beind page class-----------------------
Public Class RequiredValidator
Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()

End Sub
Protected WithEvents lnkTabOne As System.Web.UI.WebControls.LinkButton
Protected WithEvents lnkTabTwo As System.Web.UI.WebControls.LinkButton
Protected WithEvents lnkTabThree As System.Web.UI.WebControls.LinkButton
Protected WithEvents TextBox1 As System.Web.UI.WebControls.TextBox
Protected WithEvents TextBox2 As System.Web.UI.WebControls.TextBox
Protected WithEvents TextBox3 As System.Web.UI.WebControls.TextBox
Protected WithEvents btnSubmit As System.Web.UI.WebControls.Button
Protected WithEvents rfv1 As
System.Web.UI.WebControls.RequiredFieldValidator
Protected WithEvents rfv2 As
System.Web.UI.WebControls.RequiredFieldValidator
Protected WithEvents rfv3 As
System.Web.UI.WebControls.RequiredFieldValidator
Protected WithEvents tbOne As System.Web.UI.HtmlControls.HtmlTable
Protected WithEvents tbTwo As System.Web.UI.HtmlControls.HtmlTable
Protected WithEvents tbThree As System.Web.UI.HtmlControls.HtmlTable
Protected WithEvents txtServer As System.Web.UI.WebControls.TextBox
'NOTE: The following placeholder declaration is required by the Web
Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region


Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
SetCurrentTab(1)
End If
End Sub

Protected Sub SetCurrentTab(ByVal index As Integer)

tbOne.Visible = False
tbTwo.Visible = False
tbThree.Visible = False

rfv1.Enabled = False
rfv2.Enabled = False
rfv3.Enabled = False

Select Case index
Case 1
tbOne.Visible = True
rfv1.Enabled = True
Case 2
tbTwo.Visible = True
rfv2.Enabled = True
Case 3
tbThree.Visible = True
rfv3.Enabled = True
End Select
End Sub


Private Sub lnkTabOne_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles lnkTabOne.Click
SetCurrentTab(1)
End Sub

Private Sub lnkTabTwo_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles lnkTabTwo.Click
SetCurrentTab(2)
End Sub

Private Sub lnkTabThree_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles lnkTabThree.Click
SetCurrentTab(3)
End Sub

Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnSubmit.Click
Response.Write("<br>Page is submited at: " +
DateTime.Now.ToLongTimeString())
End Sub
End Class
----------------------------------------------------------------------------
-

If you have anything unclear or have any other questions, please feel free
to post here.


Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx
 
Thanks for all the code. your sample worked good and I think it will help
my code too. I'll be able to implement it later tonight.
 
Sorry for jumping in so late. There is another solution that doesn't require
any creative coding and keeps the client-side validation working fine. My
product, "Professional Validation And More" at
http://www.peterblum.com/vam/home.aspx, provides smarter validators. Each
has a property called the Enabler which lets you assign a rule that enables
the validator on the fly. For example, assign it to the VisibleCondition
rule, and it will detect if the textbox is visible before attempting to
validate. "Professional Validation And More" is designed to overcome the
numerous limitations in the existing validators that cause users to write
custom code and hacks, to solve the problems.

--- Peter Blum
www.PeterBlum.com
Email: (e-mail address removed)
Creator of "Professional Validation And More" at
http://www.peterblum.com/vam/home.aspx
 
Back
Top