Create WebPartZones Programmatically

Discussion in 'ASP .Net' started by tmeynink@gmail.com, Jun 13, 2006.

  1. Guest

    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
     
    , Jun 13, 2006
    #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. Matt
    Replies:
    1
    Views:
    830
    Sherif ElMetainy
    Nov 10, 2003
  2. Jonah Olsson
    Replies:
    1
    Views:
    444
    Eric Veltman
    Dec 8, 2003
  3. Replies:
    0
    Views:
    353
  4. karthick

    WebPart and WebPartZones Usage

    karthick, Mar 22, 2006, in forum: ASP .Net Web Controls
    Replies:
    2
    Views:
    256
    karthick
    Apr 6, 2006
  5. barry

    moving webpartzones

    barry, Feb 25, 2008, in forum: ASP General
    Replies:
    0
    Views:
    103
    barry
    Feb 25, 2008
Loading...

Share This Page