Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > Other ASP.NET > ASP.NET 1.x and 2.0 Application Design
Password Reminder
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
ASP.NET 1.x and 2.0 Application Design Application design with ASP.NET 1.0, 1.1, and 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.x and 2.0 Application Design section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old July 26th, 2007, 01:38 AM
Authorized User
Join Date: Jan 2007
Location: , , .
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Default dropdownlist on selectedindexchanged

hi guys

i am using the dropdownlist onSelectedIndexChanged event in order to load the datagrid with new results...fine no problem here

the thing is that i want to prompt a message when the user selects a field from the combo...(lets say he want to change a day from Holiday to regula..the message will be "Choosing that bla bla bla will delete all ur event?"

<asp:dropdownlist id="cbDayType" runat="server" AutoPostBack="True">

in the code side

Private Sub cbDayType_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbDayType.SelectedIndexChanged

-------stuff goes here

End Sub
i added cbDayType.Attributes("Onchange") = "javascript:return confirm('Are you sure you want to update Date?')"

 in my page_load and i got the prompt when i changed the value in my drop down list but if i click lets say "ok" its not going to the OnSelectedIndexChanged event in my code.
can any1 help me out here..
thanks in advance

  #2 (permalink)  
Old July 26th, 2007, 02:45 AM
Authorized User
Join Date: Jan 2007
Location: , , .
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts

i fixed it...

cbDayType.Attributes.Add("onchange", "if (confirm('Do Are you sure you want to update Date?')) Form1.submit(); else return false;")

  #3 (permalink)  
Old July 26th, 2007, 03:24 AM
Authorized User
Join Date: Jan 2007
Location: , , .
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts

not even close
when i select new item in the dropdown i get the confirm..i click yes and it does the work but it does it twice..
and when i click cancel on the confirm it selects the new item selected in the dropdown (it shouldnt do that...it should select the item previously selected ...)
i dont know what i am doing here...it is killing me..
anyone please help

  #4 (permalink)  
Old August 2nd, 2007, 07:04 AM
Friend of Wrox
Join Date: Feb 2006
Location: noida, UP, India.
Posts: 133
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to gaurav_jain2403

It is running your code twice on clicking Ok, because you have explicitly mentioned Form1.submit() in your code of adding the attribute. Also, it returns true, which again calls the server SelectedIndexChanged event. On Cancel, it cancels your last event, means it cancels the drop down list selection so it displays the last selected value, not the current value. For first problem, i.e. on clicking of Ok, write the code as follow:

cbDayType.Attributes.Add("onchange", "if (confirm('Do Are you sure you want to update Date?')) return true; else return false;")
  #5 (permalink)  
Old April 6th, 2012, 04:50 PM
Registered User
Points: 12, Level: 1
Points: 12, Level: 1 Points: 12, Level: 1 Points: 12, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
Join Date: Oct 2007
Location: , VA, USA.
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Lightbulb Confirming the dropdown's choice or revert

Say you have an option that deletes something, running in the code-behind OnSelectedIndexChanged, but you need a confirmation "onchange" client-side event first. What you'd do is set an attribute on the asp dropdown of "CurrentStatus" and set it equal to what you will preset the dropdown text with. Then, set the onchange attribute to the function you'll run and the pass the dropdown element "(this)": onchange="myFunction(this);"

In that JavaScript function, do this:
function myFunction(elem) {
var curStatus = elem.options[elem.selectedIndex].text;
if (curStatus == "Delete") {
if (confirm('Delete this item?')) {
this.setAttribute('CurrentStatus', curStatus);
elem.options[elem.selectedIndex].text = elem.getAttribute('CurrentStatus');
elem.setAttribute('CurrentStatus', curStatus);
So this way, if they click Cancel on the confirmation dialog, the OnSelectedIndexChanged event doesn't fire because you've set the value back to what it was before and so the index was never seen to have changed by the server.

In the code-behind, for when it does run, you'll check the dropdown text and if it doesn't match the value you want it to be to delete something, you do whatever you'd do if it was one of the other options:

protected void ddlElement (object sender, EventArgs e) {
DropDownList ddl = (DropDownList)sender;
if (ddl.Text == "Delete")
// do deletion stuff here
// do other stuff here for some other dropdown list item value

Last edited by navyjax2; April 6th, 2012 at 04:53 PM..

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
DropDownList SelectedIndexChanged Event does not davej ASP.NET 2.0 Basics 3 October 12th, 2007 10:20 AM
Problem with Dropdownlist.SelectedIndexChanged haydar BOOK: Professional ASP.NET 2.0 and Special Edition; ISBN: 978-0-7645-7610-2; ISBN: 978-0-470-04178-9 0 July 20th, 2007 10:26 AM
DropDownList SelectedIndexChanged problem p_heidi ASP.NET 1.0 and 1.1 Basics 3 April 27th, 2006 10:20 AM
SelectedIndexChanged venterjo General .NET 7 December 15th, 2004 09:21 AM
Listbox selectedindexchanged event venterjo General .NET 6 December 9th, 2004 08:46 AM

All times are GMT -4. The time now is 11:39 PM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.