By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,662 Members | 1,141 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,662 IT Pros & Developers. It's quick & easy.

dropdownlist autopostback with javascript confirm

P: n/a
I have a dropdownlist with the autopostback set to true. I want the
user to be confirm whether they do indeed want to change the value,
which on post back fires a server side event (selectedindexchanged).

I have tried adding an onchange attribute "return confirm('sure u wanna
change this?';") but it will not postback regardless of the confirm
result and the value in the list does not revert back if cancel
selected.

Is there a solution?

Oct 11 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Since OnChange event is already defined with the DropDownList control,
you need to preserve it and call your action as well.

One way to do that is with the Prototype Javascript framework.

http://joseph.randomnetworks.com/arc...ith-prototype/

But you do ont want attach a second event. You want to wrap the
current even inside a confirm block. To do that you could use
Javascript to take the value of the onclick attribute and change it
with your confirm code.

How are your skills with Javascript and DOM?

Brennan Stehling
http://brennan.offwhite.net/blog/
ro****@orisoft.co.uk wrote:
I have a dropdownlist with the autopostback set to true. I want the
user to be confirm whether they do indeed want to change the value,
which on post back fires a server side event (selectedindexchanged).

I have tried adding an onchange attribute "return confirm('sure u wanna
change this?';") but it will not postback regardless of the confirm
result and the value in the list does not revert back if cancel
selected.

Is there a solution?
Oct 11 '06 #2

P: n/a
Hi Robert,

Try the code below?

Ken
Microsoft MVP [ASP.NET]

<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

Protected Sub DropDownList1_SelectedIndexChanged _
(ByVal sender As Object, ByVal e As System.EventArgs)
Label1.Text = DropDownList1.SelectedValue.ToString
End Sub

Protected Sub Page_Load _
(ByVal sender As Object, _
ByVal e As System.EventArgs)
DropDownList1.Attributes.Add _
("OnChange", "if (!confirm('Change this?')){return};")
End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Confirm Dropdownlist</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:dropdownlist id="DropDownList1" runat="server"
autopostback="True"
onselectedindexchanged="DropDownList1_SelectedInde xChanged">
<asp:listitem selected="True">Red</asp:listitem>
<asp:listitem>Green</asp:listitem>
<asp:listitem>Blue</asp:listitem>
</asp:dropdownlist><br />
<br />
<asp:label id="Label1" runat="server"></asp:label>&nbsp;</div>
</form>
</body>
</html>
<ro****@orisoft.co.ukwrote in message
news:11**********************@e3g2000cwe.googlegro ups.com...
>I have a dropdownlist with the autopostback set to true. I want the
user to be confirm whether they do indeed want to change the value,
which on post back fires a server side event (selectedindexchanged).

I have tried adding an onchange attribute "return confirm('sure u wanna
change this?';") but it will not postback regardless of the confirm
result and the value in the list does not revert back if cancel
selected.

Is there a solution?

Oct 11 '06 #3

P: n/a
the autopostback is done by calling client script that is attached to the
onchange.only return on false;

if (!confirm('do this')) return;

note: your approach has really bad behavior for keyboard users. if they
select the downdown, and use the arrow keys to select a value, each arrow
press fires your alert.

-- bruce (sqlwork.com)

<ro****@orisoft.co.ukwrote in message
news:11**********************@e3g2000cwe.googlegro ups.com...
>I have a dropdownlist with the autopostback set to true. I want the
user to be confirm whether they do indeed want to change the value,
which on post back fires a server side event (selectedindexchanged).

I have tried adding an onchange attribute "return confirm('sure u wanna
change this?';") but it will not postback regardless of the confirm
result and the value in the list does not revert back if cancel
selected.

Is there a solution?

Oct 11 '06 #4

P: n/a
Ken,

I did not realize it worked that way. I tried your example and see
that ASP.NET appends the postback code after the Javascript set in the
attribute above so that you can cancel the postback by calling return.

I thought that once the OnChange value was set you could not change it.

Brennan

Ken Cox [Microsoft MVP] wrote:
Hi Robert,

Try the code below?

Ken
Microsoft MVP [ASP.NET]

<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

Protected Sub DropDownList1_SelectedIndexChanged _
(ByVal sender As Object, ByVal e As System.EventArgs)
Label1.Text = DropDownList1.SelectedValue.ToString
End Sub

Protected Sub Page_Load _
(ByVal sender As Object, _
ByVal e As System.EventArgs)
DropDownList1.Attributes.Add _
("OnChange", "if (!confirm('Change this?')){return};")
End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Confirm Dropdownlist</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:dropdownlist id="DropDownList1" runat="server"
autopostback="True"
onselectedindexchanged="DropDownList1_SelectedInde xChanged">
<asp:listitem selected="True">Red</asp:listitem>
<asp:listitem>Green</asp:listitem>
<asp:listitem>Blue</asp:listitem>
</asp:dropdownlist><br />
<br />
<asp:label id="Label1" runat="server"></asp:label>&nbsp;</div>
</form>
</body>
</html>
<ro****@orisoft.co.ukwrote in message
news:11**********************@e3g2000cwe.googlegro ups.com...
I have a dropdownlist with the autopostback set to true. I want the
user to be confirm whether they do indeed want to change the value,
which on post back fires a server side event (selectedindexchanged).

I have tried adding an onchange attribute "return confirm('sure u wanna
change this?';") but it will not postback regardless of the confirm
result and the value in the list does not revert back if cancel
selected.

Is there a solution?
Oct 11 '06 #5

P: n/a
I like this idea, easlier I got round it by not using autopostback at
all and just submitting the form using document.forms[0].submit().
Crude I know!

Just need to reset the selectedindex back to the original (using
variable) if the user cancels.

Later I'm going to try the atlas control toolkit's confirm control to
see how it works on the dropdownlist with autopostback....

Thanks

Oct 11 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.