Question about Controls collection, CreateChildControls and Render methods

Discussion in 'ASP .Net Building Controls' started by Henke, Nov 1, 2005.

  1. Henke

    Henke Guest

    Hi,

    Consider this code (simplified) example:

    public class Person : Microsoft.SharePoint.WebPartPages.WebPart
    {
    private ImageButton _action = new ImageButton();

    protected override void CreateChildControls()
    {
    this.Controls.Add(_action);
    _action.Click +=new ImageClickEventHandler(action_Click);
    }

    protected override void RenderWebPart(HtmlTextWriter w)
    {
    Table tbl = new Table();
    TableRow row = new TableRow();
    TableCell cell = new TableCell();

    cell.Controls.Add(_action);
    row.Cells.Add(cell);
    tbl.Rows.Add(row);
    tbl.RenderControl(w)
    }
    }

    When the above code is used the action_Click() event is not triggered
    because I move the "_action" object in the controls collection tree from
    this.Controls to cell.Controls. So instead of using the Table, TableRow and
    TableCell objects for creating HTML I will have to use the code below
    to render the HTML and hook up the _action OnClick event correctly.

    protected overrid void RenderWebPart(HtmlTextWriter w)
    {
    w.Write("<table>");
    w.Write("<tr>");
    w.Write("<td>");
    _action.RenderControl(w);
    w.Write("</td>");
    w.Write("</tr>");
    w.Write("</table>");
    }

    Can this be solved by some other approach when working with custom controls?

    Regards,

    // Henke
     
    Henke, Nov 1, 2005
    #1
    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. Arulraja
    Replies:
    3
    Views:
    1,565
    William F. Robertson, Jr.
    Oct 17, 2003
  2. Tomas
    Replies:
    1
    Views:
    3,432
    Teemu Keiski
    Feb 8, 2004
  3. Lau Lei Cheong

    Page.Render do not render complete page

    Lau Lei Cheong, May 15, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    2,339
    Lau Lei Cheong
    May 15, 2004
  4. Replies:
    0
    Views:
    593
  5. Øyvind Isaksen
    Replies:
    1
    Views:
    1,267
    Øyvind Isaksen
    May 18, 2007
  6. Ralf Müller
    Replies:
    3
    Views:
    241
    John Saunders
    Dec 9, 2004
  7. Glenn Gillen
    Replies:
    0
    Views:
    491
    Glenn Gillen
    Nov 17, 2006
  8. Kenneth McDonald
    Replies:
    5
    Views:
    645
    Kenneth McDonald
    Sep 26, 2008
Loading...