Home All Groups Group Topic Archive Search About

Render and get html from usercontrol

Author
10 Jun 2005 12:22 AM
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[i].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

Author
10 Jun 2005 1:39 AM
Craig Deelsnyder
John Olsen wrote:
Show quoteHide quote
> 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[i].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...

--
Craig Deelsnyder
Microsoft MVP - ASP/ASP.NET
Are all your drivers up to date? click for free checkup

Author
10 Jun 2005 9:09 AM
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...??

Show quoteHide quote
"Craig Deelsnyder" <cdeelsny@NO_SPAM_4_MEyahoo.com> wrote in message
news:u66M30VbFHA.3464@tk2msftngp13.phx.gbl...
> John Olsen wrote:
>> 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[i].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...
>
> --
> Craig Deelsnyder
> Microsoft MVP - ASP/ASP.NET

Bookmark and Share