Search code examples
c#listcode-behindselectedlistitem

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


<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?


Solution

  • 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;
            }
        }