Bruce
Bruce

Reputation: 8849

data binding to dropdownlist in asp.net?

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>

Upvotes: 1

Views: 13807

Answers (3)

Raghubar
Raghubar

Reputation: 2788

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

Upvotes: 0

Chris L
Chris L

Reputation: 2292

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();

Upvotes: 0

Mych
Mych

Reputation: 2553

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

Upvotes: 5

Related Questions