Click here to Skip to main content
16,021,417 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear All,

In this code Dynamically Creating TextBoxes in table.
how to create Lables left side of TextBox. Dynamically
how to store values in SQL Database table.

C#
private int numOfRows = 0;
private int numOfColumns = 0;
Here’s the code block for the generating the Tables with TextBoxes.

private void GenerateTable(int colsCount, int rowsCount){

        
        Table table = new Table();
        table.ID = "Table1";
        Page.Form.Controls.Add(table);

         
        for (int i = 0; i < rowsCount; i++){
            TableRow row = new TableRow();
            for (int j = 0; j < colsCount; j++){
                TableCell cell = new TableCell();
                TextBox tb = new TextBox();

                tb.ID = "TextBoxRow_" + i + "Col_" + j;
                
                cell.Controls.Add(tb);
                
                row.Cells.Add(cell);
            }

            
            table.Rows.Add(row);
        }
}


protected void Page_Load(object sender, EventArgs e){
         
         GenerateTable(numOfColumns, numOfRows);
}


protected void Button1_Click(object sender, EventArgs e){
        
        if (int.TryParse(TextBox1.Text.Trim(), out numOfColumns) && int.TryParse(TextBox2.Text.Trim(), out numOfRows))
        {
            
            GenerateTable(numOfColumns, numOfRows);

            
            ViewState["cols"] = numOfColumns;
            ViewState["rows"] = numOfRows;
        }
        else
        {
            Response.Write("Values are not numeric!");
        }
}


protected void Button2_Click(object sender, EventArgs e){
    
    if (ViewState["cols"] != null && ViewState["rows"] != null)
    {
        
        Table table = (Table)Page.FindControl("Table1"); 
        if (table != null)
        {
           s
            GenerateTable(int.Parse(ViewState["cols"].ToString()), int.Parse(ViewState["rows"].ToString()));

            
            for (int i = 0; i < int.Parse(ViewState["rows"].ToString()) ; i++)
            {
                for (int j = 0; j < int.Parse(ViewState["cols"].ToString()); j++)
                {
                    
                    if (Request.Form["TextBoxRow_" + i + "Col_" + j] != string.Empty)
                    {
                        Response.Write(Request.Form["TextBoxRow_" + i + "Col_" + j] + "<br />");
                    }
                }
            }
        }
    }
}
Posted
Updated 2-Jul-12 6:10am
v5
Comments
keshawa.mahi 29-Dec-12 5:19am    
Its really helpfull

Just before textbox, create a label too!

Something like:
C#
TableCell cell = new TableCell();
// Add label
Label lb = new Label();
lb.ID = "LabelRow_" + i + "Col_" + j; 
cell.Controls.Add(lb);

// Add textbox
TextBox tb = new TextBox(); 
tb.ID = "TextBoxRow_" + i + "Col_" + j;                
cell.Controls.Add(tb);
   
row.Cells.Add(cell);
 
Share this answer
 
how to save this data in database ???can u explain
 
Share this answer
 

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