简体   繁体   中英

Asp.net DropDownList not binding inside a ListView

I need to bind an Asp.net DropDownList inside an ItemTemplate of a ListView. I am using LINQ to query data from using the LINQ db context as follow:

.cs

protected void ListView_AllTickets_ItemDataBound(object sender, ListViewItemEventArgs e)
{
    DataClassesDataContext db = new DataClassesDataContext();
    DropDownList ddl_spList = (DropDownList)e.Item.FindControl("DropDownList_SpList");

    //Getting all service providers users
    var spusers = (from x in db.User1s where x.usertype == "200" select x);

    ddl_spList.DataSource = spusers;

    ddl_spList.DataTextField = "email";

    ListView_AllTickets.DataBind();

}

.aspx

<asp:DropDownList ID="DropDownList_SpList" runat="server" class="form-control" ClientIDMode="AutoID"> </asp:DropDownList>

Notice how I am finding the control then binding it to the result of the LINQ query. When I use the debugger, the data is retrieved successfully and the "email" field exists in the returned data. However, and for some reason, the ListView_AllTickets will have an item count of 0 even after the DataBind() statment.

You'll need to add this line:

 ddl_spList.DataBind();

You're rebinding ListView_AllTickets , but that's the parent object and is already being bound (hence the event you're handling with this method). Is that a typo?

Bind ddl_spList instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM