Attack Work TM
Productivity tools to accomplish work faster
 
Username Password
   
Password help
Create new account
Skip Navigation Links.

Asp.net - Render Control and Retrieve its HTML in Code

Steal This Site [Web Developer Blog]


The common way to utilize a control is to pass in properties and let the server render and display the resulting HTML. There are times when you'd want to retrieve and manipulate the rendered HTML in code before it's displayed to the user. This function allows you to pass in the control and it will return the rendered HTML as a string.

Code:
Function ControlRender(ByVal objControl As Control) As String
    Dim sb As New StringBuilder()
    Dim sw As New System.IO.StringWriter(sb)
    Dim htw As New HtmlTextWriter(sw)
    objControl.RenderControl(htw)
    Return sb.ToString()
End Function

Subscribe in a reader
Michael Khalili | Friday, October 31, 2008
Comments
All guest comments are moderated.
Saqib (Guest) on Monday, December 15, 2008 8:07:47 PM
I tried to use the above code but I am getting the exception below:

Control 'MyControl1' of type 'MyControl' must be placed inside a form tag with runat=server.

This exception is thrown even if I use the ASP.NET button control to get its HTML.

Any solution or workaround?
MichaelApproved on Thursday, December 18, 2008 9:02:41 AM
Real-time listener feedback/comments for podcasts and webcam shows - Voxback ...
You must have the control within the following tag:

<form id="form1" runat="server"> </form>

If your page doesn't have a form tag with the runat="server" option set you need to add it. If it does have it then make sure your control is included within that tag.
Sandeep (Guest) on Friday, December 19, 2008 7:52:40 AM
I tried to use the above code but I am getting the exception below:Control 'MyControl1' of type 'MyControl' must be placed inside a form tag with runat=server.<form id="form1" runat="server"> </form> is mentioned in my page,but still i'am getting the error
MichaelApproved on Thursday, January 01, 2009 10:13:06 PM
Real-time listener feedback/comments for podcasts and webcam shows - Voxback ...
But is the control inside the form tag?

<form id="form1" runat="server"> <your control> </form>
Diaa (Guest) on Thursday, November 12, 2009 11:23:56 AM
try this :

public override void VerifyRenderingInServerForm(Control control)
{
return;
}
Post your comment
Guest Name* !
Email Address* !(Not published)
Website !
Comment*
!
*Required fields

Checkout my new project iPhone News and App Directory - Yappler.com


Terms of Service | Privacy Policy | Steal This Site
Attack Work and Voxback are trademarks of Hillside Holdings.