Click here to Skip to main content
15,920,632 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to Retrieve text from a dynamically created text box from a table inside a panel.

I have tried this. I have a list(standardProperyList) which contains some values from database.

C#
Table table = new Table();

            for (int i = 0; i <= standardProperyList.Count-1; i++)
            {

                TableRow row = new TableRow();
                TableCell cell1 = new TableCell();
                TableCell cell2 = new TableCell();

                Label lbl = new Label();
                TextBox TxtBox = new TextBox();

                lbl.ID = "lbl" + standardProperyList[i].PropertyName.ToString();
                TxtBox.ID = "txt" + standardProperyList[i].PropertyName.ToString();

                ListTestBoxIds.Add(TxtBox.ID);

               
                cell1.ID = "cellPropertyName" + i.ToString();
                cell2.ID = "cellPropertyValue" + i.ToString();

                lbl.Text = standardProperyList[i].PropertyName.ToString();
                TxtBox.Text = standardProperyList[i].PropertyValue.ToString();

                cell1.Controls.Add(lbl);
                cell2.Controls.Add(TxtBox);
                row.Cells.Add(cell1);
                row.Cells.Add(cell2);
                table.Rows.Add(row);

                //Add the labels and textboxes to the Panel.
                PanelStandardProperties.Controls.Add(table);

                TextBox tt = (TextBox)table.FindControl(TxtBox.ID);
                string tes = tt.Text;

                Tried this as well.
                //foreach (Control c in PanelStandardProperties.Controls)
                //{
                //    if (c is TextBox)
                //    {
                //        TextBox txt = (TextBox)c;
                //        string str = txt.Text;
                //    }
                //}


            }


Am getting below mentioned error

Multiple controls with the same ID 'lblARPCOMMENTS' were found. FindControl requires that controls have unique IDs.

Please Assist

Thanks in Advance
Posted
Updated 4-Nov-15 0:18am
v3
Comments
Suvendu Shekhar Giri 4-Nov-15 6:28am    
Check the source of the page if it really have multiple textboxes with same id.

If you have duplicate value in standardProperyList then
lbl.ID = "lbl" + standardProperyList[i].PropertyName.ToString();

this line will generate same Label ID. You could change something like
lbl.ID = "lbl" + standardProperyList[i].PropertyName.ToString() + i; 


Here "i" will add a number with the ID such as "lblARPCOMMENTS1"
 
Share this answer
 
Hi, make you control ids unique, refer this article,
Multiple controls with the same ID were found
 
Share this answer
 
Comments
Renjith_R 4-Nov-15 7:54am    
Thanks for the reply. It is working fine but the Id is coming like

"lblARPCOMMENTS03175846fb064556b9c100bf62de0ed1"
It should come like "lblARPCOMMENTS"

How can we achieve that ?
VR Karthikeyan 4-Nov-15 23:19pm    
The problem in your code is, your code assigns same name for all controls. That is why, I'm suggested above method, now you can use some numbers with your control name like lblARPCOMMENTS_1 instead of using guid. just define an integer variable and concatenate it with control's name and increment it continuously. For example, change your code like following,

lbl.ID = "lbl" + standardProperyList[i].PropertyName.ToString() + "_" + i;

it will gives compact name for controls like lblARPCOMMENTS_1, lblARPCOMMENTS_2, ...

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