开发者

User control losing state

I have a user control that I am explicitly calling from an aspx page. In page_load of the aspx page I have the following:

myControl = (DynamicTable)Page.LoadControl("../inc/DynamicTable.ascx");

Then in my code where I want it to execute the control, I have this:

pnlESDDEnrolled.Controls.Add(myControl);

where pnlESDDEnrolled is the panel I am loading it into for display.

So, I execute the aspx page, it links off to the user control, 开发者_JS百科populates the control, returns back to the aspx page and the page displays with the user control in the middle of it. All is well.

The problem comes in when updates are made on the user control. Keep in mind, that other data is updated on the page as well, and the update button resides on the page, not the control. Anyway, when the update button is pushed, the button_click event is fired on the page, but the updates that I made on the user control are lost. Since the page loaded the user control and then the usercontrol executed the page unload method, the page has no knowledge of the user control anymore. Thus, when the update button on the page is pushed, I guess I am not really sure what happens with the updated data on the user control. All I know is that it is lost. I have been working on this for a huge amount of time, any help would be much appreciated


You need to check for IsPostBack in your user control, too.

As your page loads, the Page's Page_Load event starts, then your UserControl's Page_Load will begin, execute, then its events, then controls returns to the page's Page_Load, then all of its events. Of course, if you are using Pre_Init on the page or overriding OnInit in your controls, those events execute before the Page_Load.

So, if you are dynamically creating items on your UserControl, check for IsPostBack and places those events an override OnInit function in the UserControl. Then add your programmatic reference to that UserControl in the Page_Init of the page.


I guess while you're talking about user control state these are realised as Properties in the code?

If so, then you're probably going to have to use Viewstate to persist the UserControl's state so that when the page comes back, the UserControl will be re-populated first with what it previously had when it was being rendered. This is as simple as changing any public state properties to use ViewState as the backer.

I'm not a great fan of viewstate, but sometimes it has to be used.

However there may be more at work here - as your instincts are telling you, this might also be a problem with the fact that this usercontrol is loaded dynamically.

In which case, it all depends at which part of the page lifecycle you're doing it. You need to get this control into the page's tree before render (pre-render might be possible), as it's at this point that that page 'captures' its controls collection for the purposes of maintaining state.

Try and move the insertion of this control into the page structure as early as you can in the lifecycle. The earlier the better - so Page_load would be fine.

Equally - it might be you're already doing this - in which case I'd need more information (perhaps a reproducable scenario) in order to help fix it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜