HTML name属性的ASP.net子控件生成的,而不是独特的“ID”属性, [英] HTML 'name' attribute generated for ASP.net child controls, instead of the unique 'ID' attribute

查看:108
本文介绍了HTML name属性的ASP.net子控件生成的,而不是独特的“ID”属性,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

生成的HTML code为我定制的ASP.net服务器控件生成的,而不是id属性的子控件的name属性。事情是这样的:

The generated HTML code for my custom ASP.net server control generates the name attribute for child controls, instead of the id attribute. Something like this :

<span id="GridView2_ctl02_editdis">
    <input type="text" name="GridView2$ctl02$editdis$ctl00"/>
</span>

自定义控制本身ID显然是正确的。

The ID for the custom control itself is apparently proper.

什么是我更奇怪的是,该ID并获取生成的有时的(我不知道在什么条件下)。但的FindControl()与ID返回在服务器端空。 的FindControl()的名称属性的值工作得很好。

What is even stranger for me, is that the ID does get generated sometimes (I do not know under what conditions). But a FindControl() with that ID returns null on the server side. FindControl() with the value of the name attribute works just fine.

事情是这样的:

<span class="TextBox" id="GridView2_ctl02_editdis">
    <input type="text" id="GridView2_ctl02_editdis_ctl00" name="GridView2$ctl02$editdis$ctl00"/>
</span>

针对上述情况,的FindControl(GridView2 $ ctl02 $ editdis $ ctl00)工作正常,的FindControl(GridView2_ctl02_editdis_ctl00)没有。​​

我如何确保一致性和predictable的ID?

How do I ensure consistent and predictable IDs?

推荐答案

他们是一致的。

在内部,控件是一种命名控制(即GridView控件)的儿童,有自己的用$追加父母的标识建全的ID。在网格的情况下,它gridID $ ROWID $ CELLID $ mycontrolID。这是必要的同一子控件(即mycontrolID)的多个实例之间进行区分。为什么$,而不是_?我猜是因为很多人都已经趋于来命名他们的控制my_control_something和$符号是不比任何人。

Internally, controls that are children of a naming control (i.e. GridView), have their full IDs built by appending IDs of their parents using "$". In case of a grid, it's gridID$rowID$cellID$mycontrolID. This is necessary to differentiate between multiple instances of the same child control (i.e. mycontrolID). Why "$" and not "_"? I guess because many people already tend to name their controls "my_control_something" and the "$" symbol is as good as any.

因此​​,GridView2 $ ctl02 $ editdis $ ctl00是正确的ID,这就是为什么它用作如输入控件的名称。当回发发生时,框架需要能够与适当的控制相匹配的形式键。

So, the GridView2$ctl02$editdis$ctl00 is the right ID and that's why it's used as a name for controls such as INPUT. When post back occurs, the framework needs to be able to match form keys with appropriate controls.

与标识混乱,我认为,来自于一个事实,你使用的是内部的.aspx ID和你的HTML看到ID是两回事。客户端ID是这一点。无论出于何种原因,当控制被渲染(使用ClientID属性),所有的$被替换为_。我的猜测是,这样做是为了让它的JavaScript / CSS友好。

The confusion with IDs, I think, comes from the fact that an ID you are using inside .aspx and the ID you see in HTML are two different things. The client-side IDs are just that. For whatever reason, when a control gets rendered (using ClientID property), all "$" get replaced with "_". My guess is that this was done to make it javascript/css friendly.

现在,有关的FindControl(GridView2 $ ctl02 $ editdis $ ctl00)...你真的应该尽量避免它尽可能。 FindControl已是一个递归函数,它打破GridView2 $ ctl02 $ editdis $ ctl00变成GridView2和ctl02 $ editdis $ ctl00,认定GridView2并询问是否有ctl02 $ editdis $ ctl00作为一个孩子的控制。重复该过程由$分开的每一个部分。

Now, about that FindControl("GridView2$ctl02$editdis$ctl00")... You really should try avoiding it whenever possible. FindControl is a recursive function, which breaks "GridView2$ctl02$editdis$ctl00" into "GridView2" and "ctl02$editdis$ctl00", finds GridView2 and asks if it has the "ctl02$editdis$ctl00" as a child control. The process repeats for each part separated by $.

在一个侧面说明,无论何时你发现自己打电话Page.FindControl一段深埋地下的控制,你需要检查的模式,问为什么。例如,需要什么要与GridView2 $ ctl02 $ editdis $ ctl00,极有可能需要与一个GridView2 $ ctl02 $ editdis $ ctl01,以及所做的一切。在这种情况下,它可能需要在OnItemCreated或OnItemDataBound,在那里你有机会获得一排谁知道ctl00进行处理。

On a side note, whenever you find yourself calling Page.FindControl for some deeply buried control, you need to examine the pattern and ask why. For instance, whatever needs to be done with the "GridView2$ctl02$editdis$ctl00", most likely needs to be done with a "GridView2$ctl02$editdis$ctl01" as well. In that case, it probably needs to be handled on OnItemCreated or on OnItemDataBound, where you have access to a row who "knows" about "ctl00".

这篇关于HTML name属性的ASP.net子控件生成的,而不是独特的“ID”属性,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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