basic query on form postback

V

varkey.mathew

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,
 
K

Kevin Spencer

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.
 
S

snt

If you are using html select, then you can manually call postback by
calling the javascript function __doPostBack('cmbGroup','').

If you use asp:DropDownList, 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
 
V

varkey.mathew

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 asp:DropDownList ...

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
 
S

Steven Cheng[MSFT]

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.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top