Render HTML Fragment?

Last post 04-19-2007 8:40 PM by mkb137. 2 replies.

Sort Posts:

  • Render HTML Fragment?

    04-17-2007, 10:33 PM
    • Member
      point Member
    • mkb137
    • Member since 04-17-2007, 10:25 PM
    • Posts 2

    Is there a way to make an ASP.NET 2.0 page render only a fragment of a page?

    In other words, instead of

    <html><head></head><body><form id="(autogenerated runat="server" form)"><asp:Whatever page contents/></form></body><html>

    I just want the

    <asp:Whatever page contents/>

     One thing I've tried is to create a page master that has the following:

    protected override void Render( System.Web.UI.HtmlTextWriter writer ) {

         if ( !this.DesignMode ) {

              // Directly render the content, skipping the page itself (content form is the main <form id="ContentForm" runat="server"> form)

              this.ContentForm.RenderControl( writer );

          } else {

              // Render everything to keep visual studio happy.

         base.Render( writer );

         }

    }

    This still renders the <form> and <input> tags, which I don't want. 

    <form name="aspnetForm" method="post" action="MyPage.aspx" id="aspnetForm">
    <div>
    <input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="(randomstring)" />
    </div>...
    Rendering the ContentPlaceHolder directly fails when the content contains some <asp:> elements. 

     Is there a better way?

  • Re: Render HTML Fragment?

    04-18-2007, 9:14 PM
    Answer

    Hi,

    Use this code to render what ever control you want to


        public string GetHtmlCode(Control c)
        {
            System.IO.StringWriter sw = new System.IO.StringWriter();
            HtmlTextWriter hw = new HtmlTextWriter(sw);
            c.RenderControl(hw);
            return sw.ToString();
        }
        public override void VerifyRenderingInServerForm(Control control)
        {
          
        }

      Then if you want to render a TextBox with ID TextBox1, just use 

    string htmlCode=GetHtmlCode(TextBox1)

    Hope it helps,

    Jessica
      

    Jessica Cao
    Sincerely,
    Microsoft Online Community Support


    “Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread. ”
  • Re: Render HTML Fragment?

    04-19-2007, 8:40 PM
    • Member
      point Member
    • mkb137
    • Member since 04-17-2007, 10:25 PM
    • Posts 2

    Thank you.  This:

        public override void VerifyRenderingInServerForm(Control control)
        {
          
        }

    fixed the problem exactly.

Page 1 of 1 (3 items)