Eyad
Eyad

Reputation: 14229

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.

Upvotes: 0

Views: 1081

Answers (1)

Ann L.
Ann L.

Reputation: 13965

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.

Upvotes: 2

Related Questions