Create WebPartZones Programmatically

T

tmeynink

Hi,

I'm trying to create a custom control in ASP.NET 2.0 that will create a
table with a user definable number of rows and columns with a
WebPartZone in each cell.

Here's the code I have at present:

public class ZoneTable : Control
{
private int _cRows;
private int _cColumns;
Table _t;

public ZoneTable()
{
_t = new Table();
}

public int Rows
{
get { return _cRows; }
set { _cRows = value; }
}

public int Columns
{
get { return _cColumns; }
set { _cColumns = value; }
}

protected override void OnInit(EventArgs e)
{
_t.ID = "MyTable" + ID;
_t.BorderWidth = 1;

for (int ixRow = 0; ixRow < Rows; ++ixRow)
{
TableRow row = new TableRow();
for (int ixCol = 0; ixCol < Columns; ++ixCol)
{
WebPartZone zone = new WebPartZone();
zone.ID = GetZoneID(ixRow, ixCol);

TableCell cell = new TableCell();
cell.ID = "cell" + zone.ID;
cell.Controls.Add(zone);
row.Cells.Add(cell);
}
_t.Rows.Add(row);
}

base.OnInit(e);
}

private string GetZoneID(int ixRow, int ixCol)
{
return string.Format("{0}_R{1}C{2}", ID, ixRow, ixCol);
}

protected override void Render(HtmlTextWriter writer)
{
_t.RenderControl(writer);
}
}

I read that WebPartZones need to be created on or before Page_Init in
the page life cycle so that's why I'm creating the table elements in
the OnInit method.

The table cells are rendered to HTML, but the WebPartZones are not. For
some reason, if I add the WebPartZones to the form instead of the
individual cells in the table, they are rendered at the bottom of the
page, but not inside the table cells were I need them!

Any help much appreciated,
Todd
 

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,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top