Click here to Skip to main content
15,901,001 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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)



VB
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.
Posted
Updated 15-May-13 19:31pm
v4
Comments
Sergey Alexandrovich Kryukov 15-May-13 23:14pm    
Please add one more tag: ASP.NET.
—SA
jkirkerx 16-May-13 12:37pm    
Are you using the code in a webofrm code behind page, or is it like a class, user or server control in the App_Code folder?
snamyna 16-May-13 20:51pm    
it is webform code behind.

1 solution

I was searching about Print aspx page when I found in that article about error same as mine stated above. I just added this piece of code :

VB
Public Overrides Sub VerifyRenderingInServerForm(control As Control)
        ' Verifies that the control is rendered

    End Sub




And it successfully executed! The dynamic control doesnt need to be recreated, but only using HTML and retrieve from function inside HTML page.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900