How can i set selected item property from the list using code behind?

1.2k views Asked by At
<div id="Div1" class="myTeams">
     <div class="listTitle">My Teams</div>
    <div id="myTeams">
        <select id="myGroupList" name="myGroupList" onchange="reloadMYUser('myIntContacts');" size="10" />
<option value="abc_group"  selected >abc</option>
<option value="def_group" >def</option>
<option value="ghi_group" >ghi</option>
<option value="jkl_group" >jki</option>
 </select>

    </div>
</div>

i would like to search any group from the above list if it finds my group it should apply selected property for that list item. how can i do this from the codebehind?

1

There are 1 answers

1
Shoham On BEST ANSWER

Are you using ASP.net? if so you can use asp control... DropDownList (instead of simple select)

If you can't, try something like this: add runat="server" to myGroupList on .aspx file. then on .cs file:

 for (int i = 0; i < myGroupList.Items.Count; i++)
    {
        if (element.Children[i].InnerText == "abc_group")
        {
            element.SetAttribute("selected", "selected");
            break;
        }
    }