Render and get html from usercontrol

J

John Olsen

Hi.

I`m building a small CMS, and want to add the possibility to include server
side code inside static html-strings that is stored in a database.

For e.g. in the string "<div><b>News></b><br>[Controls/News.ascx]</div>",
[Controls/News.ascx] should be replaced by the rendered html-outpu from a
usercontrol that prints out database content. I use regex to get the content
of the []-tags, and load the control and get the output-html with the
following code:

/// <param name="html">Static html from database whith [] tags containg
usercontrols to render</param>
private string renderIncludes(string html )
{
string pattern = @"(\[.*\])";
Match m = Regex.Match(html, pattern, RegexOptions.IgnoreCase);
if (m.Success)
{
for(int i = 0; i < m.Groups.Count; i++)
{
string search = m.Groups.Value;
string control = search.Replace("[","").Replace("]","");
Control c = LoadControl(control);
c.DataBind();
string _html = renderControl(c);
html = html.Replace(search, _html);
}
}
return html;
}

private string renderControl(Control ctrl)
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
System.IO.StringWriter tw = new System.IO.StringWriter(sb);
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
ctrl.RenderControl(hw);
return sb.ToString();
}

This works well for usercontrols with text ouput only (e.g. usercontrols
with Repeaters), but when it comes to usercontrols with input a TextBox (e.g
a contact form), I get the following exception:

System.Web.HttpException: Control 'Name' of type 'TextBox' must be placed
inside a form tag with runat=server

Source:

ctrl.RenderControl(hw);

Does anybody have a tip on what to do, or how to proceed? Is there another
way of doing what I`m trying to accomplish?

Best regards,
John
 
C

Craig Deelsnyder

John said:
Hi.

I`m building a small CMS, and want to add the possibility to include server
side code inside static html-strings that is stored in a database.

For e.g. in the string "<div><b>News></b><br>[Controls/News.ascx]</div>",
[Controls/News.ascx] should be replaced by the rendered html-outpu from a
usercontrol that prints out database content. I use regex to get the content
of the []-tags, and load the control and get the output-html with the
following code:

/// <param name="html">Static html from database whith [] tags containg
usercontrols to render</param>
private string renderIncludes(string html )
{
string pattern = @"(\[.*\])";
Match m = Regex.Match(html, pattern, RegexOptions.IgnoreCase);
if (m.Success)
{
for(int i = 0; i < m.Groups.Count; i++)
{
string search = m.Groups.Value;
string control = search.Replace("[","").Replace("]","");
Control c = LoadControl(control);
c.DataBind();
string _html = renderControl(c);
html = html.Replace(search, _html);
}
}
return html;
}

private string renderControl(Control ctrl)
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
System.IO.StringWriter tw = new System.IO.StringWriter(sb);
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
ctrl.RenderControl(hw);
return sb.ToString();
}

This works well for usercontrols with text ouput only (e.g. usercontrols
with Repeaters), but when it comes to usercontrols with input a TextBox (e.g
a contact form), I get the following exception:

System.Web.HttpException: Control 'Name' of type 'TextBox' must be placed
inside a form tag with runat=server

Source:

ctrl.RenderControl(hw);

Does anybody have a tip on what to do, or how to proceed? Is there another
way of doing what I`m trying to accomplish?

Best regards,
John


It really means what it says. Somewhere you have to have in your HTML
markup a form with id="something" and runat="server" as attributes.
Only within that are controls like Textbox valid. So in the HTML
surrounding all this dynamic content, add such a form...
 
J

John Olsen

But my really problem is that I have <form runat=server> tags in the base
..ASPX page that is supposed to display the html from the database and and
thus the output from possibly any included usercontrols ([News.ascx],
[Contact.ascx])

If I add <form runat..> in the usercontrols, I get an error because when the
rendered output is added to the ASPX page, the results contain two forms...

Any thougts...??

Craig Deelsnyder said:
John said:
Hi.

I`m building a small CMS, and want to add the possibility to include
server
side code inside static html-strings that is stored in a database.

For e.g. in the string "<div><b>News></b><br>[Controls/News.ascx]</div>",
[Controls/News.ascx] should be replaced by the rendered html-outpu from a
usercontrol that prints out database content. I use regex to get the
content
of the []-tags, and load the control and get the output-html with the
following code:

/// <param name="html">Static html from database whith [] tags containg
usercontrols to render</param>
private string renderIncludes(string html )
{
string pattern = @"(\[.*\])";
Match m = Regex.Match(html, pattern, RegexOptions.IgnoreCase);
if (m.Success)
{
for(int i = 0; i < m.Groups.Count; i++)
{
string search = m.Groups.Value;
string control = search.Replace("[","").Replace("]","");
Control c = LoadControl(control);
c.DataBind();
string _html = renderControl(c);
html = html.Replace(search, _html);
}
}
return html;
}

private string renderControl(Control ctrl)
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
System.IO.StringWriter tw = new System.IO.StringWriter(sb);
System.Web.UI.HtmlTextWriter hw = new
System.Web.UI.HtmlTextWriter(tw);
ctrl.RenderControl(hw);
return sb.ToString();
}

This works well for usercontrols with text ouput only (e.g. usercontrols
with Repeaters), but when it comes to usercontrols with input a TextBox
(e.g
a contact form), I get the following exception:

System.Web.HttpException: Control 'Name' of type 'TextBox' must be placed
inside a form tag with runat=server

Source:

ctrl.RenderControl(hw);

Does anybody have a tip on what to do, or how to proceed? Is there
another
way of doing what I`m trying to accomplish?

Best regards,
John


It really means what it says. Somewhere you have to have in your HTML
markup a form with id="something" and runat="server" as attributes. Only
within that are controls like Textbox valid. So in the HTML surrounding
all this dynamic content, add such a form...
 
Joined
Jan 9, 2008
Messages
1
Reaction score
0
How I got it to work

OK - this isn't perfect, but here is how I got around it.

First a created a "dummy" user control (lets call it UCDummy) that only contained the form tag and the control I "really" wanted to render the output of.

The reason I did this is because sometimes I use the "real" usercontrol on my website and if I were to keep the form tag on it, it would give me an error saying there are too many form tags on the page.

For example - this code is within the "dummy" user control:

Code:
<asp:form runat="server">
... THE USER CONTROL HERE
</asp:form>

Then on the page where I want to render the html (in my case I put it in the database for later use), I do the following:

Code:
StringBuilder SB = new StringBuilder();
StringWriter SW = new StringWriter(SB);
HtmlTextWriter htmlTW = new HtmlTextWriter(SW);
UCDummy.Visible = true;
UCDummy.RenderControl(htmlTW);
WhereEverIWantItToGo = SB.ToString();
UCDummy.Visible = false;

Now I have the best of both worlds. It does necessitate an extra file, but sometimes it doesn't need to be pretty, it just needs to work. :)

Hope this helps!
 
Joined
Aug 30, 2010
Messages
1
Reaction score
0
Hi John, I have also the same problem in my cms. But the thing is I have a NewsLetter control and I am tring to render it in html. It gives the error and if I put <form runat="server"> again in control, it says duplicate form tags.

One more issue, is I get html rendered but I have server side event in that control which is not executed from the page when I render the control.

Please let me know If you have found any solution for this.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,233
Members
46,820
Latest member
GilbertoA5

Latest Threads

Top