简体   繁体   中英

Dropdownlist not showing previous value

I have an asp.net dropdownlist control 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 button 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.

You should return false from the event handler if you want to prevent the event:

ddlUserPresence.Attributes.Add("onchange", "return(ConfirmAbsent(" + ddlUserPresence.ClientID +"));"); 

You are also passing the ClientID of the DropDownList but you're not using it to find the control. So you should use document.GetElementById .

function ConfirmAbsent(ddlPresenceID) {
    var ddlPresence = document.GetElementById(ddlPresenceID);
    if (ddlPresence != null) {

Note that you can use defaultValue to get the initial value that was set on serverside for TextBoxes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM