View Single Post
  #5 (permalink)  
Old April 6th, 2012, 05:50 PM
navyjax2 navyjax2 is offline
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);
else
elem.options[elem.selectedIndex].text = elem.getAttribute('CurrentStatus');
}
else
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
else
// do other stuff here for some other dropdown list item value
}

Last edited by navyjax2; April 6th, 2012 at 05:53 PM.
Reply With Quote