Render web custom control

Discussion in 'ASP .Net Web Controls' started by Peter Kirk, Dec 1, 2006.

  1. Peter Kirk

    Peter Kirk Guest

    Hi, if I am writing a web custom control, do I override the Render method or
    the RenderControl or the RenderContents method - and what is the difference?

    Thanks,
    Peter
    Peter Kirk, Dec 1, 2006
    #1
    1. Advertising

  2. Peter Kirk

    Teemu Keiski Guest

    Hi,

    basically the chain goes so that at render stage, Page calls into
    RenderControl of your control, which again directs this call to Render and
    that again to again RenderContents. Does your control have RenderContents
    depends on if it derives directly from System.Web.UI.WebControls.WebControl.

    Essentially the main Render methods are:

    System.Web.UI.Control
    =================

    -RenderControl
    -Render
    -RenderChildren

    They are implemented so that RenderControl calls Render if control is
    visible. Render then calls directly RenderChildren which calls RenderCotrol
    for every child control

    System.Web.UI.WebControl
    ====================

    -derives from System.Web.UI.Control so has inherited Control's rendering
    methods

    Adds following rendering methods to the implementation:

    -AddAttributesToRender
    -RenderBeginTag
    -RenderContents
    -RenderEndTag

    And what's important, WebControl overrides Render method pretty much like
    this

    protected override void Render(HtmlTextWriter writer)
    {
    RenderBeginTag(writer);
    RenderContents(writer);
    RenderEndTag(writer);
    }

    where RenderBeginTag calls AddAttributesToRender and writes out the begin
    tag by using given HtmlTextWriter. And WebControl's RenderContents calls to
    base.Render which essentially of course calls Control's Render methods
    causing child controls to be rendered.

    So what's this all about? It's tight set of predefined rendering flow in
    which you can override everything or just one small snippet of it.
    Therefore what you want to render is the driving reason to think what should
    be overridden and when. Difference with the methods you asked is pretty much
    that with RenderControl you make the control to render at any time as
    RenderControl is also a public method. Render and RenderContents are
    protected methods only callable in the class or in derived classes. Render
    is meant to control the entire rendering flow while RenderContents is meant
    to render markup between control's begin and end tags.

    --
    Teemu Keiski
    ASP.NET MVP, AspInsider
    Finland, EU
    http://blogs.aspadvice.com/joteke



    "Peter Kirk" <> wrote in message
    news:...
    > Hi, if I am writing a web custom control, do I override the Render method
    > or
    > the RenderControl or the RenderContents method - and what is the
    > difference?
    >
    > Thanks,
    > Peter
    >
    Teemu Keiski, Dec 2, 2006
    #2
    1. Advertising

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. Replies:
    0
    Views:
    707
  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,047
    Lau Lei Cheong
    May 15, 2004
  3. Replies:
    1
    Views:
    358
    Teemu Keiski
    Jul 2, 2003
  4. Billy Boy

    Web Custom Control Render Problem

    Billy Boy, Mar 2, 2004, in forum: ASP .Net Web Controls
    Replies:
    7
    Views:
    170
    Billy Boy
    Mar 6, 2004
  5. Glenn Gillen
    Replies:
    0
    Views:
    262
    Glenn Gillen
    Nov 17, 2006
Loading...

Share This Page