Search code examples
c#asp.netrenderhtmltextwriter

Could you explain Methods about protected override void Render(HtmlTextWriter hw)


i have some curiosities about Method name is Render.

There are some code examples about printing Page Index Number,

[Paging.cs source]
    protected override void Render(HtmlTextWriter hw)
    {
        StringBuilder sb = new StringBuilder();
        sb.Append("<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\"><tr>");
        sb.Append("<td width=\"18\" align=\"center\" valign=\"bottom\"><a onfocus=\"blur();\" 
        class=\"pager\" href=\"javascript:" 
        + Page.ClientScript.GetPostBackEventReference(this, i.ToString()) + "\">
        <span style=\"width:18px;height:15px;cursor:hand;\">" + i + "</span></a></td>");
        sb.Append("</table>");
        hw.Write(sb.ToString());
    }

[default.aspx source]
<%@ Register Assembly="Control" Namespace="Control" TagPrefix="cc1" %>
<cc1:Paging ID="pg_Addr" runat="server" OnPageChanged="pg_Addr_PageChanged" />

Could you explain easily that Methods

  1. protected override void Render(HtmlTextWriter hw) and
  2. Page.ClientScript.GetPostBackEventReference

already searching MSDN and contents I was supposed to understand, but I can't be sure.

are these methods just to using for Html Tag in behind code?

and i want to know about work procedure.


Solution

  • HtmlTextWriter is used to render HTML to ASP.Net web pages. This class encapsulates the output stream for writing content (HTML) to web page and provides many helper methods that simplify HTML writing.

    Render method send HTML in response to a web request and generate content for an ASP.NET page.

    In your snippet, your Render method is creating a HTML Table, writing it to HtmlTextWriter, which in turn will prepare the contents to be rendered on client browser.


    Page.ClientScript.GetPostBackEventReference, returns a string that can be used in a client event to cause postback to the server.

    In first argument it takes the server control that processes the postback on the server. It returns a string, which can be treated as script on the client that initiates the postback.

    More on GetPostBackEventReference here.