开发者

dynamically set selectedIndex of asp:dropdown

开发者 https://www.devze.com 2023-03-09 00:34 出处:网络
I am having text which is to be shown selected like \"selected\", \"unselected\" dynamically I wanted to开发者_开发技巧 set \"selected\" like

I am having text which is to be shown selected like "selected", "unselected"

dynamically I wanted to开发者_开发技巧 set "selected" like

dropdown.selectedIndex = dropdown.Items.FindByText("selected");

how to set? please guide


You are almost there

dropdown.Items.FindByText("selected").Selected = true;

EDIT

To achieve this via javascript you will have to loop through the option elements of the dropdown. something like this

function setIndexByText() 
{
   drp = document.myform.selectcontrol; //this would be your dropdown
   str = "selected";
   for (indx=0; indx < drp.options.length; indx++) 
   {
       if (drp.options[indx].text == str) 
       {
          drp.selectedIndex = indx;
       }
   }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消