tags:

views:

713

answers:

3

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
+1  A: 

User Controls (.ascx files) are rendered during the containing page life cycle, just as you mention. When you do a button click postback on the containing page, upon the postback load the User Control will be completely rerendered. If you require the User Control to contain different information upon this load there are many things you can do:

  • In your User Control, go to a data store and return the information required during its load event or any other proper event (databind of a control, etc.)
  • In the Page Load of your containing page, assign values to properties of your UserControl that you use within different aspects of the User Control. These values could come from a data store or querystring or whatever.

There are plenty of other options as well. If you're talking about partial postbacks or AJAXy type stuff, then you'll probably have to use JavaScript to update the different parts of your User Control (unless you're using UpdatePanels, which you shouldn't because they are the devil).

Phairoh
this doesn't tell me how to re-render the user control after info has been saved outside the page_load function...
Jason
+1  A: 

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();    
}
CAbbott
A: 

I came up with what I believe to be an imperfect, yet useable solution. All I did was make the Page_Load function in my .ascx file Public and then called it after my info was saved. This took care of my problem.

If anyone has a more elegant solution, please let me know!

Jason
You shouldn't have to do this. As Phairoh mentions above, the user controls follow the page lifecycle of the containing page. You could expose public properties and methods that modify your user control before it is rendered.
CAbbott
would i just create a public method called `ReRender` that just calls the `Page_load` function?
Jason
I modified my answer to address what I believe you're trying to accomplish. It's basically a public method within the user control that can be called to rebuild the control however you want.
CAbbott
your edit is pretty much what i did... just create a public method that calls my page_load function. i like that solution better, as it makes more logical sense. thanks!
Jason
If that's what you like, then fine. One thing to be aware of though is that if your Page_Load logic ever changes to handle things not directly involved with the rendering of your control then there's a possiblilty of getting unwanted results from those external public calls.
CAbbott
exactly. that's why i accepted your answer. that abstraction, while right now all i need is the pageload function, later i may need to do other stuff too. thanks again :)
Jason