Dynamic Controls and Postback

  • Thread starter Thread starter Steve Roszko
  • Start date Start date
S

Steve Roszko

I am creating a set of dynamic text boxes on my page.

When I do a postback the dynamic controls seem to always disappear. I have
tried setting the EnableViewstate = True and still no luck. I would like
them to remain and persist any data entered by the user on postback.

Any ideas?

-Steve
 
Steve,

They'll need to be recreated everytime. The veiwstate only stores
information about the control, not the control it self.

-- Alex Papadimoulis
 
They'll need to be recreated everytime. The veiwstate only stores
information about the control, not the control it self.
-- Alex Papadimoulis


Yeah, that's what conclusion I seemed to come to but thought I'd see if I
was missing something. Here is some code for basically how I solved it.

ASPX PAGE

<body>
<form id="Form1" method="post" runat="server">
<asp:Button Runat=server ID=btnAdd Text=Add />
<asp:Button Runat=server ID=btnPost Text=Post/>
<asp:PlaceHolder ID=place Runat=server EnableViewState=True />
</form>
</body>


CODE BEHIND

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()

'--> RECREATE DYNAMIC CONTROLS IF NEEDED <--
If Request.Form("txtb") <> "" Then
Dim txtbox As New TextBox
txtbox.Text = Request.Form("txtb")
txtbox.ID = "txtb"
txtbox.EnableViewState = True

Me.place.Controls.Add(txtbox)

End If
End Sub

Private Sub btnAdd_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles btnAdd.Click
Dim txtbox As New TextBox
txtbox.Text = Now.ToString
txtbox.ID = "txtb"
txtbox.EnableViewState = True

Me.place.Controls.Add(txtbox)

End Sub


-Steve
 
Back
Top