Persisting dynamic controls across postbacks....

S

Stu

Hi,

I am creating a control in a PlaceHolder like so:

Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnDelete.Click
Dim ctrl As AcceptOrCancel =
CType(Page.LoadControl("controls/AcceptOrCancel.ascx"), AcceptOrCancel)
PlaceHolder1.Controls.Add(ctrl)

Session("CurrentControl") = "AcceptOrCancel.ascx"
End Sub

When I post the page back the dynamically created control just disappears.
Is there any way of persisting a control across page loads?

This is part of an online image editor in a cms. Different controls will be
loaded in to the placeholder depending on which buton is pressed
(delete,resize,crop etc) - is there any way of detecting a control loaded in
to the placeholder and recreating it automatically? I've been trying call
this from the Page_Init event but the session isn't loaded until further in
to the page execution order:

Private Sub LoadDynamicControls()

If Session("CurrentControl") = "AcceptOrCancel.ascx" Then
Dim ctrl As AcceptOrCancel =
CType(Page.LoadControl("controls/AcceptOrCancel.ascx"), AcceptOrCancel)
PlaceHolder1.Controls.Add(ctrl)

elseif Session("CurrentControl") = "ResizeImage.ascx" Then
Dim ctrl As ResizeImage=
CType(Page.LoadControl("controls/ResizeImage.ascx"), ResizeImage)
PlaceHolder1.Controls.Add(ctrl)

End If

End Sub

Is there any way of programatically adding the control to the page viewstate
so it persists once it is created once?

Sorry for the long question but I've been stuck for hours.

Thanks in advance,

Stu
 
M

Marina

No, in the sense that you have to go through the code that will recreate the
control. Normally, with things hard coded onto the page, the asp.net runtime
does all this for you. When it's dynamic, you need to instantiate it, and
add it.

But yes, in the sense that if you have viewstate enabled, you control's
state shoudl be restored. So if it had a textbox, whatever the user typed
into the textbox should appear.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top