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. 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. Arulraja
    Replies:
    3
    Views:
    1,354
    William F. Robertson, Jr.
    Oct 17, 2003
  2. Tomas
    Replies:
    1
    Views:
    3,103
    Teemu Keiski
    Feb 8, 2004
  3. Replies:
    0
    Views:
    472
  4. Øyvind Isaksen
    Replies:
    1
    Views:
    930
    Øyvind Isaksen
    May 18, 2007
  5. Ralf Müller
    Replies:
    3
    Views:
    153
    John Saunders
    Dec 9, 2004
Loading...

Share This Page