Avatar of urvinayan
urvinayan

asked on 

ASP.NET Control Html Render Issue

Am getting an error as
Control 'ctl00_cph1_sa' of type 'Button' must be placed inside a form tag with runat=server.
This happens while i try to get html of a control(in fact of a div control)
The page is a content page.. and i have gone through the source html file
after i got the error..I could find only one form tag and the div is well placed
inside the form tag as well.. am wondering why am getting this..Please help
Protected Sub btnHtml_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnHtml.Click
        Dim s As String = GetHtmlFromControl(out)
        div1.InnerHtml = s
    End Sub
Private Function GetHtmlFromControl(ByVal aControl As Control) As String
        Dim SB As New StringBuilder
        Dim SW As New StringWriter(SB)
        Dim htmlTW As New HtmlTextWriter(SW)
        aControl.RenderControl(htmlTW)
        Return SB.ToString()
    End Function
 
 
 
Design page as follows
 
 
<%@ Page Language="VB" MasterPageFile="~/Users/File.master" AutoEventWireup="false"
    CodeFile="FileGeneral.aspx.vb" Inherits="FileGeneral" Title="Main" %>
 
<asp:Content ID="Content1" ContentPlaceHolderID="CPH1" runat="Server">
 
<table class="wrapper" id="tblCont" style="height: 100%; width: 100%;">
        <tr>
            <td style="height: 100%; width: 100%; vertical-align: text-top;" id="tdMainContainer"
                runat="server">
                <uc3:PatFileHeader ID="PatFileHeader1" runat="server"></uc3:PatFileHeader>
                <h2>
                    <asp:Label runat="server" ID="lblSection" Text="Defined"></asp:Label>
                </h2>
 
<div style="background-color:Green; width:90px;height:150px" id="out" runat="server">
                <asp:Button ID="sa" runat="server" Text ="sadsa" />
                <asp:Label ID="asasdas" runat="server" Text ="assadsadsadsadas"></asp:Label>
                </div>

Open in new window

ASP.NETC#Visual Basic.NET

Avatar of undefined
Last Comment
urvinayan
Avatar of abel
abel
Flag of Netherlands image

What you are trying to do is far from trivial (50p?). The RenderControl will render the control on a canvas, you cannot simply render it to an empty stream out of its context (RenderControl is usually only used as overridden member when building a custom control).

If you want to display the contents of a server control on a web page, it is probably easier to do it with javascript and show it using the innerHtml property.

In the code above you seem to want to move your control onto some other place. You can do that using the Controls collection.
Avatar of urvinayan
urvinayan

ASKER

Ur right.. I know that .. But i ve seen posts in web
saying that the above code would give the html
representation of a control thats why I tried the
above code.. and the thing seemed worked for
my friend as well.. thats why i asked.. and for the
points i know its less.. coz am running out of money
Avatar of abel
abel
Flag of Netherlands image

ok, np. I'll do some checkin' of your method and I'll get back to you.
Avatar of urvinayan
urvinayan

ASKER

one more thing.. i think the guys who got this rendering html
worked, i found , using forms not inherited from masterpage
ASKER CERTIFIED SOLUTION
Avatar of urvinayan
urvinayan

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of abel
abel
Flag of Netherlands image

I do not object to closing this question. I am, however, interested in what solution you have found. That might be of interest to future people when they get here having similar problems.
Avatar of urvinayan
urvinayan

ASKER

Hai all....
The above code would be working well if u were
using asp.net 1.1 ..But for 2.0 things are a bit
different..mY SOLUTION is as follows

public string RenderControl(Control ctrl) 
{
    StringBuilder sb = new StringBuilder();
    StringWriter tw = new StringWriter(sb);
    HtmlTextWriter hw = new HtmlTextWriter(tw);

    ctrl.RenderControl(hw);
    return sb.ToString();
}


public
 override
 void
 VerifyRenderingInServerForm(Control
 control)

Open in new window

     {
           
      }
The above overriding is needed for asp.net 2.0.. I dont know why
If anyone could explain that, then thanks in advance
Now to get things work well I have to set EnableEventvalidation="false"
Now things are done.. I had a div tag with numerous controls in it
and i successfully rendered its html.. so that i could use any pdf
tool to convert that part into pdf........

Open in new window

Avatar of urvinayan
urvinayan

ASKER

Sorry.. something had happended to the override function
some formating elements came there.. So let me add it
as a snippet


public string RenderControl(Control ctrl)
{
   StringBuilder sb = new StringBuilder();
   StringWriter tw = new StringWriter(sb);
   HtmlTextWriter hw = new HtmlTextWriter(tw);
 
   ctrl.RenderControl(hw);
   return sb.ToString();
}
 
public override void VerifyRenderingInServerForm(Control control)
 
     {
 
     }

Open in new window

ASP.NET
ASP.NET

The successor to Active Server Pages, ASP.NET websites utilize the .NET framework to produce dynamic, data and content-driven web applications and services. ASP.NET code can be written using any .NET supported language. As of 2009, ASP.NET can also apply the Model-View-Controller (MVC) pattern to web applications

128K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo