ListView DataBound says Items > 0 when actually Items <= 0
So 开发者_JS百科I have a ListView (assignmentsListView) in an UpdatePanel, being filtered by a DropDownList in the same UpdatePanel. The DropDownList has a list of persons in it and uses autopostback, and the ListView shows the tasks those persons are assigned to.
I am trying to use code similar to this:
protected void assignmentsListView_DataBound(object sender, EventArgs e)
{
string resFirstName = Utilities.getResourceFirstName(resDdl.SelectedValue);
if (assignmentsListView.Items.Count <= 0)
{
//Show error message
}
else
{
//Try to find the ImageButton in the ListView's header template.
ImageButton exportButton = (ImageButton)assignmentsListView.FindControl("ImageButton3");
//Register this button as a PostBack event in the Master Page
ScriptManager scriptManager = ScriptManager.GetCurrent(this.Page);
ScriptManager.RegisterPostBackControl(exportButton);
}
}
When I first load the page, the DropDownList shows the first person in the list, and the ListView correctly shows that persons tasks.
If I then select a person who I know has zero tasks, I get an error at the RegisterPostBackControl() method, saying the passed-in control cannot be null.
When debugging, at the RegisterPostBackControl method, it shows that the ListView Items collection has >0 elements in it (the number of elements matches the person selected before the current person).
What's going on? Any suggestions?
In Asp.Net Web Forms applications, the order of events are not always what you'd want. For your case, the new person selection is probably applied after this method is executed. The best thing you could do is force databindings in an earlier event (like Page_Init)
精彩评论