Hi...

Am stuck with a problem.....my question is how do i iterate a control for eg(a textbox) in runtime....let me make it clear if user enter value as "2" and clicks a button it has to iterate two texboxes as output.

I have done in my way but then it iterate once when i press the button ....

protected void Button1_Click(object sender, EventArgs e)
    {
        int a = 3;
           
            for (int j = 0; j < a; j++)
            {
                textb.Height = 20;
                textb.Width = 100;
                textb.ID = "name" + j.ToString();
                PlaceHolder1.Controls.Add(textb);
                PlaceHolder1.Controls.Add(new LiteralControl("<br/>"));
                
               // fun();
            }
        
      
        
        
       }

Thanks ...

Recommended Answers

All 5 Replies

<div id="divTxtbox" runat="server"></div>
protected void Button1_Click(object sender, EventArgs e)
    {
         int a = 3;

         for (int j = 0; j < a; j++)
         {
            TextBox textbox = new TextBox();
            divTxtbox.Controls.Add(textbox);
         }

    }

This should be like this:

Textbox[] tbs;

//creating buttons:
private void button1_Click(object sender, EventArgs e)
{
    int total = 0;
    if(int.TryParse(textBox1.Text, out total)
    {
        tbs = new TextBox[total];
        int x = 20;  //x location
        int y = 20;  //y location
        for(int i = 0; i < tbs.Lenght; i++)
        {
            tbs[i] = new TextBox();
            tbs[i].Name = "textBox" + i.ToString();
            tbs[i].Location = new Location(x, y);
            tbs[i].Size = new Size(100, 23);
            this.Controls.Add(tbs[i]);
            //set new location for the next textbox:
            y += 30; //textboxes will be one under another           
        }
    }
    else
        MessageBox.Show("No number inserted!");

     //to iterate through textboxes:
     if(tbs.Lenght > 0)
     {
         string text = string.Empty;
         foreach(Textbox tb in tbs)
         {
             text = tb.Text; //this is actual text of particular textbox...
         }
     }
}

Hope it helps...
bye

Watch the typos!

tbs.Lenght; should be tbs.Length;

Thx, sorry for inconveniances. I wrote the code by heart, so errors can occur.
but thx for reminding me.

Hi Everybody ..

Thanx for your respond....would try any...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.