开发者

ASP NET - C#: Access controls by Id

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;
}


You can do this in code behind;

TextBox tb1 = (TextBox)FindControl(name_id)


TextBox tb = Form.FindControl(TextBox1.ID) as TextBox

Only like this, the reson why you can't access by ID directly is they absent at *.designer.cs.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜