473,395 Members | 1,462 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,395 software developers and data experts.

dropdownlist autopostback with javascript confirm

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
5 13020
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: PK9 | last post by:
Please help: I have a DropdownList control that has a server-side onSelectedIndexChanged event handler. Before this server-side event handler is run, I'd like a client-side javascript...
4
by: DotNetJunky | last post by:
I have built a control that runs an on-line help system. Depending on the category you selected via dropdownlist, it goes out and gets the child subcategories, and if there are any, adds a new...
7
by: Daniel | last post by:
Is there any other way can override this event, like javascript onchange added to the attribute of this dropdownlist? Thanks
1
by: pleaseexplaintome | last post by:
I have a datagrid with checkboxes and I can check/uncheck the checkboxes to update a database by calling my oncheckchanged function. I would like to add popup asking the users if they are sure...
11
by: Santosh | last post by:
Dear all , i am writting following code. if(Page.IsPostBack==false) { try { BindSectionDropDownlist();
3
by: Iain | last post by:
Hi All I have 2 DropDownList boxes on a page. The first (id= "Operation") is populated on PageLoad with the contents of a database table. The second id="WorkStations" will not be populated...
0
by: eurorscg | last post by:
Hi, i have this dropdownlist that breaks xhtml w3c validation.because it generates a select tag with a javascript property. this only happens when autopostback is set to true. so the the...
3
by: Lohboy | last post by:
Using ASP.NET and IE7. (Sorry if I am posting in the wrong forum but my problem seemed to be more related to the JavaScript side than the ASP.NET side.) I have two DropDownList controls the...
0
by: asmx126453 | last post by:
Hey mensen I am having some big troubles here i tryd solving it myself with internet for 2 days but i kind fix it. Its about this i have a DotNet project that alrydi is online and working for...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...

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.