Click here to Skip to main content
15,885,010 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,
My code Is
for (int c = 0; c <3; c++)
   {
     inc += 1;
     TableCell tc3 = new TableCell();
     tc3.BorderStyle = BorderStyle.Ridge;
     tot= new TextBox();
     tot.ID = "tot" + inc.ToString();
     tot.Width = 40;
     tc3.Controls.Add(tot);
     tr1.Cells.Add(tc3);
   } 

In the above code creates three Text Boxes with unique ID's every time.
Here i need to place some strings to each Text Box when click a button.
I tried many ways but i didn't get result. Can any one help me please.

Thanks in advance.
Posted
Updated 18-Dec-10 1:42am
v3
Comments
Brij 18-Dec-10 8:11am    
Where did you try to put the string in textbox, using javascript or at server side code
tulasiram3975 18-Dec-10 8:14am    
not at java script
at code behind

Heres the solution :
Declare "System.Web.UI.WebControls.TextBox tot;" and "TableCell tc3 = new TableCell();" out of the for-loop.

C#
for (int c = 0; c < 3; c++)
{
    inc += 1;
    tc3.BorderStyle = System.Web.UI.WebControls.BorderStyle.Ridge;
    tot = new System.Web.UI.WebControls.TextBox();
    tot.ID = "tot" + inc.ToString();
    tot.Width = 40;
    tc3.Controls.Add(tot);
    tr1.Cells.Add(tc3);
}


To fill the TextBoxes enumerate the Controls.

Like this :
C#
int iIndex = 0;
List<String> lItems = new List<String>();
lItems.Add("Ram");
foreach (System.Web.UI.WebControls.WebControl wcObject in tc3.Controls)
{
    if (wcObject.ID.Contains("tot"))
    {
       ((System.Web.UI.WebControls.TextBox)wcObject).Text = lItems[iIndex];
       iIndex++;
    }
}
 
Share this answer
 
Comments
tulasiram3975 18-Dec-10 9:16am    
You Are My God Thank You Very Much sir.......
David Melcher 18-Dec-10 9:18am    
I'm glad to hear that :).
Sry for bad english...

You can fill the textBoxes at creating point.

example 1 :
for (int c = 0; c <3; c++)
{
     inc += 1;
     TableCell tc3 = new TableCell();
     tc3.BorderStyle = BorderStyle.Ridge;
     tot= new TextBox();
     tot.ID = "tot" + inc.ToString();
     tot.Text = "Some Text in here...";
     tot.Width = 40;
     tc3.Controls.Add(tot);
     tr1.Cells.Add(tc3);
} 


or you enumerate the Controls after creation.

example 2 :
foreach(Control cObject in tc3.Controls)
{
     if(cObject.Name.Contains("tot"))
     {
          cObject.Text = "Some Text in here...";
     }
}


Is that helpful?

Greetings David
 
Share this answer
 
Comments
tulasiram3975 18-Dec-10 8:17am    
text is not a same value it varies every time means if textbox1 contains david textbox2 contains ram like that
int iIndex = 0;
List<string> lItems = new List<string>();
lItems.Add("David");
lItems.Add("Ram");
lItems.Add("...");
foreach(Control cObject in tc3.Controls)
{
     if(cObject.Name.Contains("tot"))
     {
          cObject.Text = lItems[iIndex];
          iIndex++;
     }
}


Greetings
 
Share this answer
 
Comments
tulasiram3975 18-Dec-10 8:30am    
'System.Web.UI.Control' does not contain a definition for 'Name' and no extension method 'Name' accepting a first argument of type 'System.Web.UI.Control' could be found (are you missing a using directive or an assembly reference?
cObject.Name.Contains("tot") for this line Showing this exeception sir
David Melcher 18-Dec-10 8:35am    
Sry my fault.
Add the following code to the creation point of the textBox.
tot.Name = "tot" + inc.ToString();
tulasiram3975 18-Dec-10 8:37am    
i am Sorry to ask You sir Please Execuse me
David Melcher 18-Dec-10 8:39am    
Try to enumerate with the ID instead of the Name.
Like :
foreach(Control cObject in tc3.Controls)
{
if(cObject.ID.Contains("tot"))
{
cObject.Text = lItems[iIndex];
iIndex++;
}
}

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