Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET Javascript
Hi all,
 
I have an asp.net dropdownlist with autopostback false.
 
I have written js code to display confirm message while dropdown value changed.
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
 
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 27-Sep-12 1:12am
akee seth1.6K
Comments
Malli_S at 27-Sep-12 7:19am
   
Use 'OnSelectedIndexChanged' instead of 'onchange'.
aspnet_regiis at 27-Sep-12 9:45am
   
He is using javascript.. 'OnSelectedIndexChanged' is a server side event

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Comments
akee seth at 28-Sep-12 1:14am
   
I have tried it.But still not working.Its showing undefined value.
Marcus Kramer at 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
0 Dnyaneshwar@Pune 604
1 thatraja 370
2 OriginalGriff 306
3 Gihan Liyanage 224
4 kbrandwijk 204
0 OriginalGriff 737
1 Dnyaneshwar@Pune 604
2 Kornfeld Eliyahu Peter 505
3 Sergey Alexandrovich Kryukov 405
4 thatraja 370


Advertise | Privacy | Mobile
Web02 | 2.8.140902.1 | Last Updated 27 Sep 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100