p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/)
-   ASP.NET 1.x and 2.0 Application Design (http://p2p.wrox.com/asp-net-1-x-2-0-application-design-67/)
-   -   dropdownlist on selectedindexchanged (http://p2p.wrox.com/asp-net-1-x-2-0-application-design/60512-dropdownlist-selectedindexchanged.html)

RoniR July 26th, 2007 02:38 AM

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



RoniR July 26th, 2007 03:45 AM

i fixed it...

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


RoniR July 26th, 2007 04:24 AM

not even close[V]
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


gaurav_jain2403 August 2nd, 2007 08:04 AM

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;")

navyjax2 April 6th, 2012 05:50 PM

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
}


All times are GMT -4. The time now is 12:25 AM.

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