Steven
Steven

Reputation: 18869

Load user control data after page postback?

I have a page where a user can update their user information (like their first name). I also have a user control on this page that displays a lot of information, including the user's first name.

My problem I'm having is that when I change the first name on this page, the user control isn't updated after I save the data.

Here's my front end:

<UC:LeftNav ID="leftNav" runat="server" />

<asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox>

<asp:Button ID="btnSave" runat="server" Text="Save" OnClick="btnSave_Click" />

My button click event to save the user info:

protected void btnSave_Click(object sender, EventArgs e)
{
    ...
    user.FirstName = txtFirstName.Text();
    user.Save();
    ...
}

My user control that displays the user info:

protected void Page_Load(object sender, EventArgs e)
{
    ...
    lblFirstName.Text = user.Firstname;
    ...
}

I went through the debugger, and I found that the btnSave_Click event on my page occurs after the Page_Load event of my user control.

Is there a way that I can get the btnSave_Click event to occur before I load the user data in my user control?

Upvotes: 2

Views: 1511

Answers (1)

Dave Zych
Dave Zych

Reputation: 21887

No, there is no way to change when the event is raised. But you can put your user loading code into a Bind method and call it from your btnSave_Click method.

In leftNav user control:

public void Page_Load(object sender, EventArgs e)
{
    if(!IsPostback)
    {
        BindUserInfo();
    }
}

public void BindUserInfo()
{
    //Binding code here
}

On page:

public void btnSave_Click(object sender, EventArgs e)
{
    //Do save work
    leftNav.BindUserInfo();
}

Upvotes: 2

Related Questions