Click here to Skip to main content
15,891,316 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Below code I am Dynamically generating Labels and textboxes, my Question is how to change the dynamic label names
C#
private int numOfRows = 0;
        private int numOfColumns = 0;

        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();
                    Label lbl = new Label();
                    lbl.Text = "label";
                    lbl.ID = "labelRow_" + i + "Col_" + j;
                    cell.Controls.Add(lbl);
                                    


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

                }

                table.Rows.Add(row);
            }
        }
Posted
Updated 4-Jul-12 1:27am
v3
Comments
bhagirathimfs 4-Jul-12 7:17am    
what's your problem??
Label name means Label's Text or ID??
2011999 4-Jul-12 7:25am    
lables Text
Jim Jos 4-Jul-12 7:39am    
You need to go thru each the tables.row.cells and verify the control type whether it is label or not and then you could get the control as label for changing the properties
[no name] 5-Jul-12 2:54am    
Please ask the question properly.
What dynamic label name you exactly want to change?

1 solution

C#
for (int j = 0; j < colsCount; j++)
                {
 
                    
                    TableCell cell = new TableCell();
                    Label lbl = new Label();
                    lbl.Text = "label" + i;
                    lbl.ID = "labelRow_" + i + "Col_" + j;
                    cell.Controls.Add(lbl);
                                    
 

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

In this way u can change the text name of the label.
If u want to give different to different labels than use switch case or if clause
 
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