ASP NET - C#: Access controls by Id

13,506

You can do this in code behind;

TextBox tb1 = (TextBox)FindControl(name_id)
Share:
13,506
Michel
Author by

Michel

Updated on June 14, 2022

Comments

  • Michel
    Michel about 2 years

    In my ASP.Net project; I'm using C# as behind code.

    One of my project functions creates some text boxes dynamically, according to user's needs. Each textbox has a different Id.

    My question is how can I access those textboxes by Id?

    Let say that were created 5 textboxes, how can I edit the code of specifically one of them?

    Bellow is the actual code that I use to generate those textboxes:

    int name_id = 1;
    foreach (WebApplication5.ServiceReference1.ClientData client in Clients)
    {
        TextBox1 = new TextBox();
        TextBox1.ID = name_id.ToString();
        TextBox1.Style["Position"] = "Absolute";
        TextBox1.Style["Top"] = y + "px";
        TextBox1.Style["Left"] = x + "px";
        TextBox1.Text = client.descricao;
        Form.Controls.Add(TextBox1);
    
        name_id++;
        x = x + 10;
        y = y + 10;
    }