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 ...

<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

This article has been dead for over six months. Start a new discussion instead.