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...
 

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

Top