Hye all..
In my aspx page, I've declared
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
From code behind, I added controls (eg:checkboxes, gridviews,panels) into placeholder.
I know each time page is postback, all dynamic controls will disappear. I found that there are 2 ways to avoid those controls disappearing.
1) Recreate all dynamic controls and add into placheholder.
2) Render all controls inside placeholder and convert to string as HTML.
I prefer method #2 as method #1 will takes longer time to execute.
For chosen method, I've done this on a button click:
Dim cb as New Checkbox
cb.ID = "cb_745"
PlaceHolder1.Controls.Add(cb)
Dim html As String = RenderControl(PlaceHolder1)
Private Function RenderControl(control As Control) As String
Dim sb As New StringBuilder()
Dim sw As New StringWriter(sb)
Dim writer As New HtmlTextWriter(sw)
control.RenderControl(writer)
Return sb.ToString()
End Function
When run the code, error that I get is
Control 'MainContent_cb_745' of type 'CheckBox' must be placed inside a form tag with runat=server.
I am using masterpage.
Any help would be appreciated. Thank you.