AutoPostBack & Validation

  • Thread starter Thread starter Scott M.
  • Start date Start date
S

Scott M.

If I put RequiredFiledValidators on a page and set them up with
corresponding TextBoxes everything works just fine. If I add a DropDownList
and set its AutoPostBack to True, I am able to post data to the server
WITHOUT the validators invoking their validation.

It seems that the easiest solution would have been for MS to simply give the
DropDownList control a "CausesValidation" property, but it does not have
one.

How can I invoke the client-side validations when a DropDownList causes the
PostBack?
 
Try this..(NOTE: I've done this with ASP:Buttons but not
with drop down list but it should work the same way.)

In the codebehind in your Page_Load function add the
client side javascript to the OnChange event of your drop
down list.

myDropDownList.Attributes.Add("OnChange", "return
MyJavaScriptFunction();");

In your MyJavaScriptFunction perform your validation and
return true if it passes or false if it fails.
If you return a false that should prevent the Postback on
the OnChange event of the drop down list.

HTH,
Suresh.
 
I just tried this and it doesn't seem to work.
-----Original Message-----
Try this..(NOTE: I've done this with ASP:Buttons but not
with drop down list but it should work the same way.)

In the codebehind in your Page_Load function add the
client side javascript to the OnChange event of your drop
down list.

myDropDownList.Attributes.Add("OnChange", "return
MyJavaScriptFunction();");

In your MyJavaScriptFunction perform your validation and
return true if it passes or false if it fails.
If you return a false that should prevent the Postback on
the OnChange event of the drop down list.

HTH,
Suresh.
I
add a DropDownList MS
to simply give the
.
 
Hi Suresh,

Please try the following:

*** First my HTML
<HTML>
<HEAD>
<title>DropDownValid</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">
<script language="javascript">
<!--
function DropChange(eventTarget, eventArgument) {
if (typeof(Page_ClientValidate) == 'function')
{
Page_ClientValidate();
if (Page_IsValid)
__doPostBack(eventTarget,eventArgument);
return Page_IsValid;
}
else
{
__doPostBack(eventTarget,eventArgument);
return true;
}
}
//-->
</script>
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<asp:TextBox id="TextBox1" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator id="RequiredFieldValidator1" runat="server"
ErrorMessage="RequiredFieldValidator"
ControlToValidate="TextBox1"></asp:RequiredFieldValidator>
<asp:DropDownList id="DropDownList1" runat="server"></asp:DropDownList>
<asp:LinkButton id="LinkButton1" runat="server">test</asp:LinkButton>
</form>
</body>
</HTML>


*** My code-behind
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
DropDownList1.Items.Add("a")
DropDownList1.Items.Add("b")
DropDownList1.Items.Add("c")
DropDownList1.Attributes.Add("onchange", "return
DropChange('DropDownList1', '');")
End If
End Sub

Private Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object,
ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged
Response.Write("the index changed")
End Sub

*** How it works
- The link button causes validation & works normally.
- The drop list only posts if the page is valid.
- The code-behind DropDownList1_SelectedIndexChanged event correctly fires
because we pass the correct argument to __doPostBack.
- Notice that I set the drop list's AutoPostBack to false. That is because
AutoPostBack creates an onchange event for the drop list which might
interfere with our onchange event code. Further, our code includes the
functionality of the code that AutoPostBack would have added.

Thank you, Mike
Microsoft, ASP.NET Support Professional

Microsoft highly recommends to all of our customers that they visit the
http://www.microsoft.com/protect site and perform the three straightforward
steps listed to improve your computer’s security.

This posting is provided "AS IS", with no warranties, and confers no rights.


--------------------
 
Back
Top