如何命名,并找到在C#中动态创建器WebControls [英] How to name and find dynamically created webcontrols in c#

查看:132
本文介绍了如何命名,并找到在C#中动态创建器WebControls的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想唯一的名称添加到我加入到表中的每个文本框。

I am trying to add a unique name to each textbox that I'm adding to a table.

我试过:

TableRow someRow = new TableRow();
TableCell someCell = new TableCell();
TextBox someTextbox = new TextBox();

someTextbox.Attributes.Remove("name");
someTextbox.Attributes.Add("name",itsId);

someCell.Controls.Add(someTextBox);
someRow.Cells.Add(someCell);
theTable.Rows.Add(someRow);

该HTML生成包括我的名字和自动生成的名称作为文本框的属性。

The html generated includes both my name and the autogenerated name as attributes of the textbox.

不幸的是,当我运行一个的FindControl ,我的名字,我得到一个空引用,尽管它仍然工作在自动生成的名称找到它。

Unfortunately, when I run a FindControl, by my name, I get a null reference, even though it still works to find it by the autogenerated name.

什么我需要做的,找到我的名字的控制?当/为什么它autogenerating名字我的控制?

What do I need to do to find the control by my name? When / why is it autogenerating names for my controls?

成功code:

Successful code:

TextBox tb = (TextBox)FindControl(autogeneratedID);
WriteToSomeOtherDiv(tb.Text);

不成功code:

Unsuccessful code:

TextBox tb = (TextBox)FindControl(myId);
WriteToSomeOtherDiv(tb.Text);

推荐答案

这取决于ASP.Net的什么版本。从历史上看,你没有在ID和控件的名字控制。在ASP.Net 4.0,这改变了。您可以控制​​标识的渲染方式。为什么不使用这个功能呢?

It depends on what version of ASP.Net. Historically you did not have control over the Id's and the names of the controls. In ASP.Net 4.0, this changed. You can control how the Id's are rendered. Why not use this feature instead?

下面是在.NET 4.0中的新功能的文章:的http://www.dotnetfunda.com/articles/article893-control-over-client-ids--aspnet-40-.aspx

Here is an article on the new feature in .Net 4.0 : http://www.dotnetfunda.com/articles/article893-control-over-client-ids--aspnet-40-.aspx

是否有一个原因,你的目标的name属性?

Is there a reason you are targeting the name attribute?

如果您使用的是旧版本(3.5),这是不那么容易。该只的FindControl寻找控制,不是名称的标识。

If you are using an older version (3.5) this isn't so easy. The FindControl only looks for the id of the control, not the name.

这篇关于如何命名,并找到在C#中动态创建器WebControls的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆