473,651 Members | 2,790 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 (selectedindexc hanged).

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 13042
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 (selectedindexc hanged).

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.dt d">

<script runat="server">

Protected Sub DropDownList1_S electedIndexCha nged _
(ByVal sender As Object, ByVal e As System.EventArg s)
Label1.Text = DropDownList1.S electedValue.To String
End Sub

Protected Sub Page_Load _
(ByVal sender As Object, _
ByVal e As System.EventArg s)
DropDownList1.A ttributes.Add _
("OnChange", "if (!confirm('Chan ge this?')){return };")
End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Confir m Dropdownlist</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:dropdownli st id="DropDownLis t1" runat="server"
autopostback="T rue"
onselectedindex changed="DropDo wnList1_Selecte dIndexChanged">
<asp:listitem selected="True" >Red</asp:listitem>
<asp:listitem>G reen</asp:listitem>
<asp:listitem>B lue</asp:listitem>
</asp:dropdownlis t><br />
<br />
<asp:label id="Label1" runat="server"> </asp:label>&nbsp ;</div>
</form>
</body>
</html>
<ro****@orisoft .co.ukwrote in message
news:11******** **************@ e3g2000cwe.goog legroups.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 (selectedindexc hanged).

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.goog legroups.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 (selectedindexc hanged).

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.dt d">

<script runat="server">

Protected Sub DropDownList1_S electedIndexCha nged _
(ByVal sender As Object, ByVal e As System.EventArg s)
Label1.Text = DropDownList1.S electedValue.To String
End Sub

Protected Sub Page_Load _
(ByVal sender As Object, _
ByVal e As System.EventArg s)
DropDownList1.A ttributes.Add _
("OnChange", "if (!confirm('Chan ge this?')){return };")
End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Confir m Dropdownlist</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:dropdownli st id="DropDownLis t1" runat="server"
autopostback="T rue"
onselectedindex changed="DropDo wnList1_Selecte dIndexChanged">
<asp:listitem selected="True" >Red</asp:listitem>
<asp:listitem>G reen</asp:listitem>
<asp:listitem>B lue</asp:listitem>
</asp:dropdownlis t><br />
<br />
<asp:label id="Label1" runat="server"> </asp:label>&nbsp ;</div>
</form>
</body>
</html>
<ro****@orisoft .co.ukwrote in message
news:11******** **************@ e3g2000cwe.goog legroups.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 (selectedindexc hanged).

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
1769
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 confirmation box to appear for the user to select whether or not to continue. I've tried adding an onselectedindexchanged event handler in the Page.Load, I've tried Page.RegisterStartupScripts, but I can't figure it out. Here's my code: ASPX Page:
4
5471
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 dropdownlist to the screen for selection. This continues until there are no children, and then it checks for a help article list based on that last selection and displays actual articles for display. Adding the controls and getting everything...
7
6175
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
2709
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 they want to proceed. I have written a javascript function named confirm_duplicate and it works as expected - it checks/unchecks checkboxes depending on user response.
11
5805
by: Santosh | last post by:
Dear all , i am writting following code. if(Page.IsPostBack==false) { try { BindSectionDropDownlist();
3
5274
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 until the first has been changed The definitions are below.
0
1081
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 validation error that i get is this: # Error Line 263 column 86: there is no attribute "language".
3
10572
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 second of which resides within an UpdatePanel control which responds to a change in the first DropDownList. I also have a hidden button nested inside the UpdatePanel to force a postback via JS. The user uses these two DropDownList controls to create...
0
1935
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 almost everything. but it whas made in Visual Studio 7 and i converted it to 2005. in version 7 it works almost perfect but in 2005 i am having some errors. becase all the code i am using is very long i am trying to post the nesecery code only if...
0
8357
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8277
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8700
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8465
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7298
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4144
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2701
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1910
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1588
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.