views:

31

answers:

1

So 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?

A: 

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)

sukru
If this is true, then DataBound needs to be re-defined: "This event notifies the server control that *any data binding logic written for it has completed*." http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.basedataboundcontrol.databound.aspx
Matthew Jones
It might be the case that this event is triggered twice. Since we do not have access to rest of the Form class, the best advice I can give is setting up breakpoints at every event handler method, following the actual progress of events.
sukru