Render and get html from usercontrol

Discussion in 'ASP .Net' started by John Olsen, Jun 10, 2005.

  1. John Olsen

    John Olsen Guest

    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
     
    John Olsen, Jun 10, 2005
    #1
    1. Advertisements

  2. 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.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
     
    Craig Deelsnyder, Jun 10, 2005
    #2
    1. Advertisements

  3. John Olsen

    John Olsen Guest

    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" <cdeelsny@NO_SPAM_4_MEyahoo.com> wrote in message
    news:...
    > 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.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
     
    John Olsen, Jun 10, 2005
    #3
  4. John Olsen

    hellocrowley

    Joined:
    May 28, 2007
    Messages:
    1
    Likes Received:
    0
    I have exactly the same problem too. Can anyone help?
     
    hellocrowley, May 28, 2007
    #4
  5. John Olsen

    chrisajohn

    Joined:
    Jan 9, 2008
    Messages:
    1
    Likes Received:
    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 (Text):

    <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 (Text):

    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!
     
    chrisajohn, Jan 9, 2008
    #5
  6. John Olsen

    shailendrasinh

    Joined:
    Aug 30, 2010
    Messages:
    1
    Likes Received:
    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.
     
    shailendrasinh, Aug 30, 2010
    #6
    1. Advertisements

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?WWFuaXI=?=

    Get html that Control render

    =?Utf-8?B?WWFuaXI=?=, Feb 29, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    433
    Teemu Keiski
    Mar 1, 2004
  2. Lau Lei Cheong

    Page.Render do not render complete page

    Lau Lei Cheong, May 15, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    2,345
    Lau Lei Cheong
    May 15, 2004
  3. Replies:
    1
    Views:
    1,098
    Teemu Keiski
    May 30, 2005
  4. ThunderMusic
    Replies:
    4
    Views:
    609
    ThunderMusic
    Dec 21, 2005
  5. =?Utf-8?B?RGFuaWVsIERpIFZpdGE=?=

    Render UserControl in Class wihtout access to webform?

    =?Utf-8?B?RGFuaWVsIERpIFZpdGE=?=, Aug 1, 2007, in forum: ASP .Net
    Replies:
    0
    Views:
    490
    =?Utf-8?B?RGFuaWVsIERpIFZpdGE=?=
    Aug 1, 2007
  6. Reik
    Replies:
    1
    Views:
    353
    Brock Allen
    Apr 12, 2005
  7. rabii
    Replies:
    4
    Views:
    429
    Matteo Migliore
    Jun 29, 2005
  8. Glenn Gillen
    Replies:
    0
    Views:
    496
    Glenn Gillen
    Nov 17, 2006
Loading...