[英]ASP.Net - Naming Containers and ClientIDMode=“Static”
MSDN documentation states with regard to the ClientIdMode: MSDN文档陈述有关ClientIdMode的信息:
Static The ClientID value is set to the value of the ID property.
静态 ClientID值设置为ID属性的值。 If the control is a naming container, the control is used as the top of the hierarchy of naming containers for any controls that it contains.
如果控件是命名容器,则该控件将用作其包含的任何控件的命名容器层次结构的顶部。
source: http://msdn.microsoft.com/en-us/library/system.web.ui.control.clientidmode%28v=vs.100%29.aspx 来源: http : //msdn.microsoft.com/en-us/library/system.web.ui.control.clientidmode%28v=vs.100%29.aspx
However, i am not finding the "top of the hierarchy" business to be the case. 但是,我发现情况并非如此。 For example, I have a usercontrol:
例如,我有一个用户控件:
<uc1:WidgetsListControl runat="server" id="WidgetsListControl" ClientIDMode="Static" />
For good measure, I set the clientidmode in the control source as well although I'm not sure which one is needed: 出于很好的考虑,尽管我不确定需要哪一个,我也在控制源中也设置了clientidmode:
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WidgetsListControl.ascx.cs" Inherits="WebApplication1.WidgetsListControl" EnableViewState="false" ClientIDMode="Static" %>
Within the user control, I have a textbox: 在用户控件中,我有一个文本框:
<asp:TextBox ID="testTextBox" runat="server" />
My expectation is that the text box would be named something like WidgetsListControl$testTextBox
我的期望是该文本框将被命名为
WidgetsListControl$testTextBox
However what I find upon view source is: 但是,我发现视图源是:
<input name="ctl00$MainContent$WidgetsListControl$testTextBox" id="testTextBox" type="text"/>
What am I missing? 我想念什么? Is there a way to achieve what I'm looking for (shorter ids) without setting 'static' on every control within the user control?
有没有一种方法可以实现我要查找的内容(较短的ID),而无需在用户控件中的每个控件上都设置“静态”?
EDIT: 编辑:
Actually after looking closer, I am finding that the ID attribute is working as described in the MSDN - however the name attribute is still the full concatenation of the naming-container hierarchy. 实际上,在仔细观察之后,我发现ID属性正在按MSDN中的描述工作-但是name属性仍然是命名容器层次结构的完整串联。
Given a site of high complexity, the names and IDs of these controls start to take up the majority of the bandwidth (markup size). 给定一个高复杂性的站点,这些控件的名称和ID开始占用带宽的大部分(标记大小)。 I can't seem to find any good workaround to slim down the markup in this regard.
在这方面,我似乎找不到任何可行的方法来简化标记。
You must have this control within a Panel or something with runat="server"
and the custom control itself is a naming container for its children. 您必须在Panel或带有
runat="server"
的控件中拥有此控件,并且自定义控件本身是其子级的命名容器。 If you want identifiers to be how you name them with no change, you'll want to use the Predictable
(IIRC) client id mode. 如果希望标识符不改变其命名方式,则需要使用
Predictable
(IIRC)客户端ID模式。
Note: though looking at the documentation for Predictable
this doesn't seem to be the case, I'm sure it's what I've always used to get 'clean' .NET identifiers in ASP.NET. 注意:尽管在
Predictable
文档中似乎并非如此,但我确信这是我一直以来在ASP.NET中获取“干净的” .NET标识符的方法。
Further, if you want to apply the ClientIDMode globally, then specify it in the web.config file in the attribute of the <pages>
element. 此外,如果要全局应用ClientIDMode,请在
<pages>
元素的属性中的web.config文件中指定它。
ClientIdMode="Static" does work as described by Microsoft. ClientIdMode =“ Static”确实按照Microsoft的描述工作。
Make sure your looking at the id in the generated source, not the name. 确保您查看生成的源中的ID,而不是名称。 That's the mistake I made.
那是我犯的错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.