开发者

Can't dynamically add Web User Controls

开发者 https://www.devze.com 2023-04-12 23:05 出处:网络
I understand the page lifecycle and how I need to add the dynamic controls on page_init if I want to take advantage of viewstate. And I already have read a lot of topics regarding the issue but it was

I understand the page lifecycle and how I need to add the dynamic controls on page_init if I want to take advantage of viewstate. And I already have read a lot of topics regarding the issue but it was not very useful.

I have to work with the solution where I have a set of user controls, that inherit one interface and then I add/delete them dynamically to a literal control. I can't load them at once in a page_init because later I have to delete/add them based on user actions. These controls contains a button, and its post back event handler is fired only when I generate this user control at page_init or page_load methods.

So I ha开发者_JS百科ve to somehow reinitialise/restart lifecycle of the page after needed processing in some Button_Click, (where I update the controls list) events handlers. That is not possible, as I have found. Redirecting to the same page is not a solution, because in the case I will lost my whole viewstate.

So I am stuck, could you please help me with the solution?


It sounds like you could benefit from creating a CompositeControl.

I recently answered a similar question based on dynamically creating textboxes in which I provided a fairly detailed example.

See: Dynamically add a new text box on button click

Hope this helps.


If I'm understanding this correctly, you need to instantiate the dynamic controls to retrieve their view state, but you're relying on the view state to determine how to instantiate the dynamic controls.

You could utilize the ASP.NET Session State? Use the Session State to store the information from which you generate the dynamic controls.

When you handle the button click event, destroy and recreate all of the controls, and then store all the information you need to recreate them again in the Session State.

The next time the page instantiates, the page_init function can check if there's any pertinent info in the Session State, and use this to recreate the controls.

0

精彩评论

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

关注公众号