I have a GridView inside a UserControl which I need to display a selection of data.

When the constructor of the UserControl is called, I am trying to load all the required data from a database and bind it to the gridview. However when my code gets to the point that the data is bound it throws a NullReferenceException because the gridview is null. How can I bind to this gridview from inside the constructor like this?

I have tried calling the constructor from inside Page_Load and Page_PreRender, and the result is the same.

Here is the constructor code:

public UserList()
{
    LoadUsers();
    //Other goodness...
}

public void LoadUsers()
{
    //Load the data from the db...

    gvUsers.DataSource = users;
    gvUsers.DataBind();
}

Related posts

Recent Viewed