Wrox Programmer Forums
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old July 26th, 2007, 01:38 AM
Authorized User
Join Date: Jan 2007
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

Old July 26th, 2007, 02:45 AM
Authorized User
Join Date: Jan 2007
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;")

Old July 26th, 2007, 03:24 AM
Authorized User
Join Date: Jan 2007
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

Old August 2nd, 2007, 07:04 AM
Friend of Wrox
Join Date: Feb 2006
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;")
Old April 6th, 2012, 04:50 PM
Registered User
Join Date: Oct 2007
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..

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

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.