Click here to Skip to main content
14,920,205 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
function DiseaseChange() {
           var disease = document.getElementById("<%=DDLDisease.ClientID %>").selectedindex;
           if (confirm("Are You sure yow want to change disease?") == true) {

           }
           else {
               document.getElementById("<%=DDLDisease.ClientID %>").selectedindex = disease;
               return false;
           }


this is the javascript i wrote for dropdownlist

when i am clicking on cancel of the alert the value of disease dropdown should not be change...but this is not happning..value of drop down is getting change..please help me out from this problem..
Posted
Comments
bbirajdar 13-Mar-13 9:03am
   
Remove this part from the above code
else {
document.getElementById("<%=DDLDisease.ClientID %>").selectedindex = disease;
return false;
}
ZurdoDev 13-Mar-13 11:48am
   
I'm confused. Maybe I'm missing something or maybe you missed it. Your if statement says if they click Cancel then change the dropdown. Don't you mean if they click OK?

Use below code.

C#
function DiseaseChange() {
           var disease = document.getElementById("<%=DDLDisease.ClientID %>").selectedindex;
           if (confirm("Are You sure yow want to change disease?") == true) {
               document.getElementById("<%=DDLDisease.ClientID %>").selectedindex = disease;
} else{
    return false;
}
   
When I looked your code, I found a small issue.
if your calling this method onchange of the dropdown then it wont work because the selected index will be the current index selected. So i have added a code snippet below which takes the selected index on pageload itself. May be you could understand if you take a look at it. If you face any problem with this reply me.

JavaScript
function DiseaseChange(id) {
        var ddlObj = document.getElementById(id);
        var index = ddlObj.selectedIndex;
        if (confirm("Are You sure yow want to change disease?") == true) {
            //your code
        }
        else {
            ddlObj.selectedIndex = PREVIOUS_INDEX;
            //your code
        }
    }
    var PREVIOUS_INDEX = 0;
    $(document).ready(function () {
        var ddlObj = document.getElementById(id);
        PREVIOUS_INDEX = ddlObj.selectedIndex;
    });
   

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




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