How to set ASP.net dropdownlist index to 0 from the front-end

前端 未结 2 1541
情话喂你
情话喂你 2021-01-29 07:25

I have the following code in ASP.net page:



        
2条回答
  •  囚心锁ツ
    2021-01-29 07:59

    There are many ways to do this but here is the solution with JavaScript:

    function setSelectedIndex(dropdownlist, selVal)
        var ddl1 = document.getElementById(dropdownlist);
        //alert(ddl1.selectedIndex); //displays the proper index...
        if(ddl1.length >= selVal)
        {
           ddl1.selectedIndex = selVal;
        }
    }
    

    Call the above function as per you required as below:

    setSelectedIndex('<%=ddl1.ClientID %>', 2);
    

    UPDATE As you said you have already set ClientIDMode, try the following updated function:

    function setSelectedIndex(selVal){
        var ddl1 = document.getElementById('ddl1');
        if(ddl1.length >= selVal)
        {
           ddl1.selectedIndex = selVal;
        }
    }
    

    and call it as:

    setSelectedIndex(0);
    

提交回复
热议问题