Click here to Skip to main content
15,891,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi ALL,
I have stuck up with a problem when developing a custom control.Below is the code for ref.When a using this control in my application,I need a table to be displayed.But its not happening.So please help me out.

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace TestGridControl
{
  [DefaultProperty("Text")]
  [ToolboxData("<{0}:TestGridControl  runat="server">")]
  public class TestGridControl : Table
  {

    private int nrows;
    TableCell cell;
    public HyperLink hyp = new HyperLink();
    public Label lbl = new Label();
    TableRow row;
    Table table = new Table();

    [Bindable(true)]
    [Category("Appearance")]
    [DefaultValue("")]
    [Localizable(true)]
    [Browsable(true)]
    public int NumberofRows
    { //Int32 s = (Int32)ViewState["NumberofRows"];
      get
      {
        EnsureChildControls();
        return nrows;
      }
      set
      {
        EnsureChildControls();
        nrows = value;
      }
    }

    //public void CreateTable()
    //{
    // cell = new TableCell();
    // row = new TableRow();
    // row.BackColor = System.Drawing.Color.Green;
    // row.Cells.Add(cell);
    // table.BorderColor = System.Drawing.Color.Black;
    // table.Width = 100;
    // table.Height = 100;
    // table.Rows.Add(row);

    //}
    protected override void RenderContents(HtmlTextWriter writer)
    {
      //writer.BeginRender();
      this.CreateChildControls();
      //writer.EndRender();
      ////this.Render(writer);
      base.Render(writer);
    }

    protected override void CreateChildControls()
    {
      try
      {
        table.Width = 100;
        table.Height = 100;
        for (int i = 0; i < this.NumberofRows; i++)
        {
          cell = new TableCell();
          row = new TableRow();
          cell.Controls.Add(hyp);
          cell.Controls.Add(lbl);
          row.BackColor = System.Drawing.Color.Green;
          row.Cells.Add(cell);
          table.Width = 100;
          table.BorderColor = System.Drawing.Color.Red;
          table.Height = 100;
          table.Rows.Add(row);
        }
        table.DataBind();
        this.Controls.Add(table);
      }
      catch (Exception ex)
      {
        Console.WriteLine("StackOverflow", ex.Message); ;
      }
    }
  }
}


Thanks in advance.
Posted
Updated 1-Nov-12 23:26pm
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900