473,407 Members | 2,312 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

AutoPostBack & Validation

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?
Nov 18 '05 #1
4 8991
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-***@BADSPAMsnet.net> wrote in message
news:uB*************@tk2msftngp13.phx.gbl...
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?

Nov 18 '05 #2
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.
-----Original Message-----
If I put RequiredFiledValidators on a page and set them up withcorresponding TextBoxes everything works just fine. If I add a DropDownListand set its AutoPostBack to True, I am able to post data to the serverWITHOUT the validators invoking their validation.

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

How can I invoke the client-side validations when a DropDownList causes thePostBack?
.

Nov 18 '05 #3
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.
-----Original Message-----
If I put RequiredFiledValidators on a page and set themup with
corresponding TextBoxes everything works just fine. If

Iadd a DropDownList
and set its AutoPostBack to True, I am able to post datato the server
WITHOUT the validators invoking their validation.

It seems that the easiest solution would have been for

MSto 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?
.

.

Nov 18 '05 #4
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.
--------------------
Content-Class: urn:content-classes:message
From: "Suresh" <an*******@discussions.microsoft.com>
Sender: "Suresh" <an*******@discussions.microsoft.com>
References: <uB*************@tk2msftngp13.phx.gbl> <06****************************@phx.gbl> Subject: re: AutoPostBack & Validation
Date: Fri, 12 Dec 2003 09:33:02 -0800
Lines: 47
Message-ID: <00****************************@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.
-----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.
-----Original Message-----
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?
.

.


Nov 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Luklrc | last post by:
Hi, I'm having to create a querysting with javascript. My problem is that javscript turns the "&" characher into "&amp;" when it gets used as a querystring in the url EG: ...
0
by: dotnet beginner | last post by:
I have a textbox in a webform which is validated by a compare validator. Also the textbox has autopostback property set to true. When I work this way, the page immediately posts back, thus the...
1
by: Mad Scientist Jr | last post by:
I'm having this issue where textbox A on my form is recalculated every time textbox B, C, or D is changed. To complicate this, if dropdown D = 1, for example, A = B + C, if dropdown D = 2, A = C *...
5
by: Hans Kesting | last post by:
Hi, I have a pulldown with both AutoPostback set to true and a validator. When I select an "invalid" value from the pulldown, the validator message shows briefly, then a postback occurs and the...
2
by: Bob Peek | last post by:
I have created a master page with a single asp:button that posts the page without page validation. On the content page, I have a variety of asp controls including text box, drop down list...
4
by: Sarita Sai Bhushan | last post by:
Hi, While I was developing a active desktop web page in ASP.Net using server controls I faced a problem with the autopostback property of server control. The sever control I used in my aspx page...
2
by: Tom Edelbrok | last post by:
Question: Why does a button event (ie: Button1_Click) get executed on the first click for a textbox control which has 'autopostback=false', but doesn't get executed until a second click when...
3
by: Brad | last post by:
The first text on my form is a numeric field. I have a javascript that runs on this field for onkeyup (validate the key strokes and modifies fields on the screen) but when I do this and have the...
4
by: =?Utf-8?B?UmVuYXVkIExhbmdpcw==?= | last post by:
Hello, I have a strange yet very simple problem with the asp.net Textbox web control. On an empty asp.net page, add a single asp:TextBox control with Autopostback=false with nothing else on...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.