Common Methods for JavaScript dropdownlist operations, from ASP. NET Forum, nc01
<% @ Page Language = " C # " Autoeventwireup = " True " Codefile = " DDL. aspx. CS " Inherits = " DDL " %>
<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
< Html Xmlns = "Http://www.w3.org/1999/xhtml" >
< Head Runat = "Server" >
< Title > Untitled page </ Title >
< Script Type = "Text/JavaScript" >
<! --
Function Setdropdownlist (elementref, valuetosetto)
{
VaR Isfound = False ;
For ( VaR I = 0 ; I < Elementref. Options. length; I ++ )
{
If (Elementref. Options [I]. Value = Valuetosetto)
{
Elementref. Options [I]. Selected = True ;
Isfound = True ;
}
}
If(Isfound= False)
Elementref. Options [0]. Selected= True;
}
Function Setdatoscga (DDL, strestadocarta)
{
Setdropdownlist (document. getelementbyid (DDL), strestadocarta );
}
Function Button1_onclick (){
Setdatoscga ( ' <% = Dropdownlist1.clientid %> ' , ' CC ' );
}
//-->
</Script>
</ Head >
< Body >
< Form ID = "Form1" Runat = "Server" >
< Div >
< ASP: dropdownlist ID = "Dropdownlist1" Runat = "Server" >
< ASP: listitem > AA </ ASP: listitem >
< ASP: listitem > Bb </ ASP: listitem >
< ASP: listitem > CC </ ASP: listitem >
< ASP: listitem > Dd </ ASP: listitem >
< ASP: listitem > EE </ ASP: listitem >
</ ASP: dropdownlist >
< Input ID = "Button1" Type = "Button" Value = "Button" Onclick = "Return button1_onclick ()" />
</ Div >
</ Form >
</ Body >
</ Html >