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