Connecting Tech Pros Worldwide Forums | Help | Site Map

AutoPostBack & Validation

Scott M.
Guest
 
Posts: n/a
#1: Nov 18 '05
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?



Maxim V. Karpov
Guest
 
Posts: n/a
#2: Nov 18 '05

re: AutoPostBack & Validation


Scott,
It is limitation of the Framework read my post on this

http://ipattern.com/simpleblog/PermLink.aspx?entryid=22

I hope it helps, Maxim

"Scott M." <s-mar@BADSPAMsnet.net> wrote in message
news:uBO2k9LwDHA.540@tk2msftngp13.phx.gbl...[color=blue]
> If I put RequiredFiledValidators on a page and set them up with
> corresponding TextBoxes everything works just fine. If I add a[/color]
DropDownList[color=blue]
> 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[/color]
the[color=blue]
> DropDownList control a "CausesValidation" property, but it does not have
> one.
>
> How can I invoke the client-side validations when a DropDownList causes[/color]
the[color=blue]
> PostBack?
>
>[/color]


Suresh
Guest
 
Posts: n/a
#3: Nov 18 '05

re: AutoPostBack & Validation


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.
[color=blue]
>-----Original Message-----
>If I put RequiredFiledValidators on a page and set them[/color]
up with[color=blue]
>corresponding TextBoxes everything works just fine. If I[/color]
add a DropDownList[color=blue]
>and set its AutoPostBack to True, I am able to post data[/color]
to the server[color=blue]
>WITHOUT the validators invoking their validation.
>
>It seems that the easiest solution would have been for MS[/color]
to simply give the[color=blue]
>DropDownList control a "CausesValidation" property, but[/color]
it does not have[color=blue]
>one.
>
>How can I invoke the client-side validations when a[/color]
DropDownList causes the[color=blue]
>PostBack?
>
>
>.
>[/color]
Suresh
Guest
 
Posts: n/a
#4: Nov 18 '05

re: AutoPostBack & Validation


I just tried this and it doesn't seem to work.
[color=blue]
>-----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.
>[color=green]
>>-----Original Message-----
>>If I put RequiredFiledValidators on a page and set them[/color]
>up with[color=green]
>>corresponding TextBoxes everything works just fine. If[/color][/color]
I[color=blue]
>add a DropDownList[color=green]
>>and set its AutoPostBack to True, I am able to post data[/color]
>to the server[color=green]
>>WITHOUT the validators invoking their validation.
>>
>>It seems that the easiest solution would have been for[/color][/color]
MS[color=blue]
>to simply give the[color=green]
>>DropDownList control a "CausesValidation" property, but[/color]
>it does not have[color=green]
>>one.
>>
>>How can I invoke the client-side validations when a[/color]
>DropDownList causes the[color=green]
>>PostBack?
>>
>>
>>.
>>[/color]
>.
>[/color]
Mike Moore [MSFT]
Guest
 
Posts: n/a
#5: Nov 18 '05

re: AutoPostBack & Validation


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.


--------------------[color=blue]
> Content-Class: urn:content-classes:message
> From: "Suresh" <anonymous@discussions.microsoft.com>
> Sender: "Suresh" <anonymous@discussions.microsoft.com>
> References: <uBO2k9LwDHA.540@tk2msftngp13.phx.gbl>[/color]
<062101c3c0cf$c40b1610$a001280a@phx.gbl>[color=blue]
> Subject: re: AutoPostBack & Validation
> Date: Fri, 12 Dec 2003 09:33:02 -0800
> Lines: 47
> Message-ID: <003b01c3c0d5$fe8238e0$a401280a@phx.gbl>
> MIME-Version: 1.0
> Content-Type: text/plain;
> charset="iso-8859-1"
> Content-Transfer-Encoding: 7bit
> X-Newsreader: Microsoft CDO for Windows 2000
> X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
> thread-index: AcPA1f6CeWVgxS2URQaYb3EqDB4QuQ==
> Newsgroups: microsoft.public.dotnet.framework.aspnet
> Path: cpmsftngxa07.phx.gbl
> Xref: cpmsftngxa07.phx.gbl microsoft.public.dotnet.framework.aspnet:196048
> NNTP-Posting-Host: tk2msftngxa12.phx.gbl 10.40.1.164
> X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
>
> I just tried this and it doesn't seem to work.
>[color=green]
> >-----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.
> >[color=darkred]
> >>-----Original Message-----
> >>If I put RequiredFiledValidators on a page and set them[/color]
> >up with[color=darkred]
> >>corresponding TextBoxes everything works just fine. If[/color][/color]
> I[color=green]
> >add a DropDownList[color=darkred]
> >>and set its AutoPostBack to True, I am able to post data[/color]
> >to the server[color=darkred]
> >>WITHOUT the validators invoking their validation.
> >>
> >>It seems that the easiest solution would have been for[/color][/color]
> MS[color=green]
> >to simply give the[color=darkred]
> >>DropDownList control a "CausesValidation" property, but[/color]
> >it does not have[color=darkred]
> >>one.
> >>
> >>How can I invoke the client-side validations when a[/color]
> >DropDownList causes the[color=darkred]
> >>PostBack?
> >>
> >>
> >>.
> >>[/color]
> >.
> >[/color]
>[/color]

Closed Thread