Friday, January 4, 2013

Creating Dynamic Table on button click in ASP.Net




Server Side code:

protected void btnShow_Click(object sender, EventArgs e)
    {
        CreateDynamicTable();
    }
    private void CreateDynamicTable()
    {
        p1.Controls.Clear();

        // Fetch the number of Rows and Columns for the table
        // using the properties
        int tblRows = 4;
        int tblCols = 4;
        // Create a Table and set its properties
        Table tbl = new Table();
        // Add the table to the placeholder control
        p1.Controls.Add(tbl);
        // Now iterate through the table and add your controls
        for (int i = 0; i < tblRows; i++)
        {
            TableRow tr = new TableRow();
            for (int j = 0; j < tblCols; j++)
            {
                TableCell tc = new TableCell();
                TextBox txtBox = new TextBox();
                txtBox.Text = "RowNo:" + i + " " + "ColumnNo:" + " " + j;
                // Add the control to the TableCell
                tc.Controls.Add(txtBox);
                // Add the TableCell to the TableRow
                tr.Cells.Add(tc);
            }
            // Add the TableRow to the Table
            tbl.Rows.Add(tr);
        }   
    }

OutPut :


No comments:

Post a Comment