Keep state of multiple usercontrols in ASP.NET
I am generating instances of a UserControl
and putting them in an UL
. In my UserControl
there is a button with an event. I wa开发者_JAVA技巧nt to ask how I can keep the state of the UserControls I generated so that they remain on the page after a postback and also to catch their events.
For i As Integer = 0 To lstFoo.Count - 1
Dim liFoo As New HtmlGenericControl("li")
Dim ucMyFoo As ucFoo = LoadControl("userControls/ucFoo.ascx")
liFoo.Controls.Add(ucMyFoo)
ucMyFoo.DoSomething()
ulFoo.Controls.Add(liFoo)
Next
It sounds like on the post, you are not running the logic listed above. Are you only running the above code on the GET request? You can make sure it is executed in the Page_Load regardless of GET or POST.
Another way you can do it is by writing a UL web user control that has the logic to add the ucFoo.ascx control builtin. Then just include this UL web user control (ascx) to your webform.
This will make your life easier, since this new web user control will always be on the page, and you're not adding the control dynamically.
精彩评论