Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

I have an asp.net dropdownlist with autopostback false.

I have written js code to display confirm message while dropdown value changed.
JavaScript
function ConfirmAbsent(ddlPresence) {
            if (ddlPresence != null) {
                var myValue = ddlPresence.options[ddlPresence.selectedIndex].text;
                var blnAbsent = confirm("This will mark as "+ myValue +". Please confirm clicking OK.");

                if (blnAbsent) {
                    return true;
                }
                else {
                    return false;
                }
            }
        }


And add this attribute to dropdownlist like below

C#
ddlUserPresence.Attributes.Add("onchange", "if (!ConfirmAbsent(" + ddlUserPresence.ClientID
                        + ")) return; else __doPostBack('"
                        + ddlUserPresence.UniqueID + "', '');");


When i click on ok it postback the page working correctly, but when i click on cancel button it is not showing me the previous value instead showing the current value.

So when i click on cancel button it should show me the previous value.Please help.
I have tried hidden field also.But still its not working.
Posted
Comments
Malli_S 27-Sep-12 7:19am    
Use 'OnSelectedIndexChanged' instead of 'onchange'.
bbirajdar 27-Sep-12 9:45am    
He is using javascript.. 'OnSelectedIndexChanged' is a server side event

1 solution

If you wish to do this solely on the client side with javascript, you'll have to create a temporary placeholder variable in the javascript to hold the current value. When the page loads, set this value to the initial value of the dropdownlist. When someone clicks Cancel on the confirmation, you just have to reset the selected value of the dropdownlist to the temporary placeholder value. When a user accepts confirmation, you just have to update the placeholder to the new value.
 
Share this answer
 
Comments
akee seth 28-Sep-12 1:14am    
I have tried it.But still not working.Its showing undefined value.
fjdiewornncalwe 28-Sep-12 8:40am    
If you have an undefined value, then either you have not set the temporary placeholder value on page load, or there is no value on page load. In either case, when you want to reset the value of the dropdown from the temporary placeholder, you should check if the value is undefined (yourValue == null) and then take action to set the value to an appropriate default value.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900