Hello everyone
I want to create Textboxes according to the entered number at runtime but with different names because I want to deal with the the data entered in this textboxes

protected void Button1_Click(object sender, EventArgs e)
    {
        int rows = 0;
        int cells = 0;
        int counter =int.Parse(TextBox1.Text);
        for (int i = 0; i <= counter; i++)
        {
            HtmlTableRow ro = new HtmlTableRow();
            HtmlTableCell ce1 = new HtmlTableCell();
            HtmlTableCell ce2 = new HtmlTableCell();
            ro.Cells.Add(ce1);
            ro.Cells.Add(ce2);
            table1.Rows.Add(ro);
            if (cells > 1)
            {
                cells = 0;
                rows++;
            }
            cells++;
            TextBox tb =new TextBox();
            this.table1.Rows[rows].Cells[cells].Controls.Add(tb);
            cells++;
        }
    }

As previus I created this textboxes but with the same name so I cant deal with the data inserted in it
I want your help Please
Thank you

Recommended Answers

All 5 Replies

Set the name of your text box.

I don't Know what do you mean but
I think that you didn't understand the idea
if you apply this code you will find as example 3 textboxes with the same name
(tb)
and you will not be able to retrieve any data from it

There is a "Name" property for your text box, set it a unique name, like "textbox1" ..

There is no "Name" property for the text box
I put the "ID" property of the text box to unique name but it also still give me one ID which is the last one

int s=1;
for (int i = 0; i < 3; i++)
        {
            TextBox tb = new TextBox();
            tb.ID = "text"+s;
            Form.Controls.Add(tb);
            s++;
         }

the 3 text boxes have the same name (tb) but it have 3 IDs (text1,text2,text3)

if (tb.ID == "text1")
        {
            Label1.Text = tb.Text;
        }
        else if (tb.ID == "text2")
        {
            Label1.Text = tb.Text;
        }
        else if (tb.ID == "text3")
        {
            Label1.Text = tb.Text;
        }

this code will print only the value of the third text box
is that mean that the application can't identify the first 2 text box

Odd, I could have sworn there was still a Name property.. odd, ok.

So, are you sure the ID is working out? Id have expect ID to have complained it needed s.ToString();

And that code probably would if you were running it in side the same original click event as you made 3 items, all called "tb" and while the form should have 3 boxes, your tb only references the last one, box 3..

You dont assign an onclick to any of those new boxes which would be the only place code similar to that would work

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.