Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > basic query on form postback

Reply
Thread Tools

basic query on form postback

 
 
varkey.mathew@wipro.com
Guest
Posts: n/a
 
      06-27-2005
Hi there,

I have a basic form with a postback.

The following is a line of dynamic code written so that when the user
clicks on the Button control, he will receive a confirm messagebox.

btnSubmit.Attributes.Add("onclick","return confirm('Are you sure you
want to leave without saving your change(s)?');");

This works perfectly fine.

When I tried to alter the same so that the same functionality is
achieved by a Combobox, using the "onchange" event, I seem to hit upon
an issue.

cmbGroup.Attributes.Add("onchange","return confirm('Are you sure you
want to leave without saving your change(s)?');");

Basically, when the combo box value is changed, it pops up the confirm
box, but an "OK" response will not postback the form even though the
AutoPostBack option is set to true for the Combo box control.

Please help.

Thanks and regards,

 
Reply With Quote
 
 
 
 
Kevin Spencer
Guest
Posts: n/a
 
      06-27-2005
Hi there,

The reason your Submit button works is that what a submit button does is
submit the form. When you add a JavaScript event handler that returns true
or false, you either accept or cancel the event that occurred. In the case
of a Submit button, the click event by default submits the form. That is,
ASP.Net does not add any JavaScript to the Submit button to submit the form,
because it doesn't need to. So, of course, there's a PostBack.

In the case of a ComboBox, the "select" object in an HTML form has an
"onchange" event that is fired when you change the selectedIndex of the
object. This event is what is used by the ASP.net object model to add a bit
of JavaScript that enters the information about the select object to the
hidden form fields, and programmatically submit the form, which is a
PostBack. It seems that you've overridden this event handler, so that
instead of doing the JavaScript function, it does the confirm method
instead. Since returning a value from a JavaScript event handler either
accepts or cancels the event, and the "select" object doesn't, by default,
submit the form, the form is not submitted.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Ambiguity has a certain quality to it.

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> Hi there,
>
> I have a basic form with a postback.
>
> The following is a line of dynamic code written so that when the user
> clicks on the Button control, he will receive a confirm messagebox.
>
> btnSubmit.Attributes.Add("onclick","return confirm('Are you sure you
> want to leave without saving your change(s)?');");
>
> This works perfectly fine.
>
> When I tried to alter the same so that the same functionality is
> achieved by a Combobox, using the "onchange" event, I seem to hit upon
> an issue.
>
> cmbGroup.Attributes.Add("onchange","return confirm('Are you sure you
> want to leave without saving your change(s)?');");
>
> Basically, when the combo box value is changed, it pops up the confirm
> box, but an "OK" response will not postback the form even though the
> AutoPostBack option is set to true for the Combo box control.
>
> Please help.
>
> Thanks and regards,
>



 
Reply With Quote
 
 
 
 
snt
Guest
Posts: n/a
 
      06-27-2005
If you are using html select, then you can manually call postback by
calling the javascript function __doPostBack('cmbGroup','').

If you use aspropDownList, you can add your client javascript
function as an attribute and you have to handle to cancel the post
back.

Hope this helps.

snt
http://www.onlinemall.com
http://www.flexoweb.com

 
Reply With Quote
 
varkey.mathew@wipro.com
Guest
Posts: n/a
 
      06-27-2005
Thanks Kevin & SNT for your quick responses,

I was wondering whether there is any way I can actually override the
event handler with confirm() function and then do a postback as well..
It is an aspropDownList ...

If there is any material that you can direct me to on the net, it would
be really very helpful.

Thanks once again.

Regards,

Varkey

 
Reply With Quote
 
Steven Cheng[MSFT]
Guest
Posts: n/a
 
      06-28-2005
Hi Varkey,

I think the problem is just because when you using the

cmbGroup.Attributes.Add("onchange","return confirm('Are you sure you
want to leave without saving your change(s)?');");

, the dropdownlist's "onchange" event will end right after we make the
decision on the confirm dialog , so that the sequential "__doPostBack"
function is not called. I suggest you change your code to the below:

==============
private void Page_Load(object sender, System.EventArgs e)
{
string script =
@"

if(!confirm('Are you sure to postback?')){ return false};

";

lstItems.Attributes.Add("onchange",script);
}
===============

that means we only return false when the user choose "no", but let the
event continue if "yes" is choosen. This works well on my side, please let
me know if it also works on your side.

Hope helps. Thanks,

Steven Cheng
Microsoft Online Support

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

 
Reply With Quote
 
varkey.mathew@wipro.com
Guest
Posts: n/a
 
      06-28-2005
Hi Steve,

It worked a treat!!!!

Thanks a tonne.. u r a genius..

Varkey

Steven Cheng[MSFT] wrote:
> Hi Varkey,
>
> I think the problem is just because when you using the
>
> cmbGroup.Attributes.Add("onchange","return confirm('Are you sure you
> want to leave without saving your change(s)?');");
>
> , the dropdownlist's "onchange" event will end right after we make the
> decision on the confirm dialog , so that the sequential "__doPostBack"
> function is not called. I suggest you change your code to the below:
>
> ==============
> private void Page_Load(object sender, System.EventArgs e)
> {
> string script =
> @"
>
> if(!confirm('Are you sure to postback?')){ return false};
>
> ";
>
> lstItems.Attributes.Add("onchange",script);
> }
> ===============
>
> that means we only return false when the user choose "no", but let the
> event continue if "yes" is choosen. This works well on my side, please let
> me know if it also works on your side.
>
> Hope helps. Thanks,
>
> Steven Cheng
> Microsoft Online Support
>
> Get Secure! www.microsoft.com/security
> (This posting is provided "AS IS", with no warranties, and confers no
> rights.)


 
Reply With Quote
 
Steven Cheng[MSFT]
Guest
Posts: n/a
 
      06-29-2005
You're welcome

Steven Cheng
Microsoft Online Support

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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What is the difference between Visual Basic.NET and Visual Basic 6? Jimmy Dean Computer Support 3 07-25-2005 07:05 AM
Another basic question: How to call and show one Web Form from another Web Form? Rob R. Ainscough ASP .Net Web Controls 3 06-14-2005 03:56 PM
Re: Python interpreter in Basic or a Python-2-Basic translator. rrr@ronadam.com Python 0 05-02-2005 01:48 PM
Python interpreter in Basic or a Python-2-Basic translator. Engineer Python 6 05-01-2005 10:16 PM
Upgrading Microsoft Visual Basic 6.0 to Microsoft Visual Basic .NET Jaime MCSD 2 09-20-2003 05:16 AM



Advertisments