Search code examples
asp.netascx

Refresh Content In a .ascx File


Because .ascx files are naturally rendered early in the page life cycle, if I want to update information on a form that is outside the .ascx part, how do I refresh the .ascx file to reflect the changes, say on button click (the same one that saves info)?

For instance (pseudocode):

Sub Page_load
    'user control is naturally rendered here'
End Sub

Sub Button_Click
    SaveStuff()
    ReRenderUserControl()
End Sub

Solution

  • If you're creating a user control that's being built based on data saved. What you can do is create a method that does that building and then call it within the page and user control (pseudocode):

    UserControl:

    protected Page_Load(object sender, EventArgs e)
    {
        BuildControlBasedOnData();
    }
    
    public BuildControlBasedOnData()
    {
        // Build the user control based on saved data
    } 
    

    Calling Page:

    Button_Click(object sender, EventArgs e)
    {
        UserControl1.BuildControlBasedOnData();    
    }