Question about Controls collection, CreateChildControls and Render methods

H

Henke

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top