1

For a Label in we bind a data using

<asp:Label ID="Label2" runat="server" Text='<%#Eval("address") %>'></asp:Label>

how to bind data to dropdownlist like that?

asp:DropDownList ID="droplist" runat="server" >
     <asp:ListItem Text="admin"></asp:ListItem>
    <asp:ListItem Text="manager"></asp:ListItem>
</asp:DropDownList>
Bruce
  • 8,609
  • 8
  • 54
  • 83

3 Answers3

5

Like this....

<asp:DropDownList ID="droplist" runat="server" SelectedValue='<%#Eval("fieldname")%>'>
     <asp:ListItem Text="admin"></asp:ListItem>
    <asp:ListItem Text="manager"></asp:ListItem>
</asp:DropDownList>

Note that intellisense will not pick SelectedValue out. You will of course need to populate the dropdown with the data... using any method that suits

Mych
  • 2,527
  • 4
  • 36
  • 65
0

Either put your datasource in the DropDownList declaration like here: populate dropdownlist

Or use Codebehind like this:

Eg: inside Page_Load():

List<string> ItemsToGoInDropDown = new List<string>{"manager", "admin", "etc"};
droplist.DataSource = ItemsToGoInDropDown;
droplist.DataBind();
Community
  • 1
  • 1
Chris L
  • 2,262
  • 1
  • 18
  • 33
0

Put the data in hidden field. Then asigen that in drop down in Gridview Rowdatabound Event.Like This.

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            HiddenField hf = (HiddenField)e.Row.FindControl("hf");
            DropDownList ddl = (DropDownList)e.Row.FindControl("ddl");
            ddl.SelectedValue = hf.Value;
        }
Raghubar
  • 2,768
  • 1
  • 21
  • 31