[英]How to use ClientIDMode in ASP.NET 4
The default value of ClientIDMode for a page is AutoID. 页面的ClientIDMode的默认值为AutoID。 The default value of ClientIDMode for a control is Inherit. 控件的ClientIDMode的默认值为Inherit。 If you do not set ClientIDMode for a page or for any controls on the page, all controls will use the AutoID algorithm. 如果您没有为页面或页面上的任何控件设置ClientIDMode,则所有控件都将使用AutoID算法。
This is from msdn. 这是从msdn。 But when I created a web application in ASP.NET 4 or even 3.5, all of the ids of the control are what I have written for them. 但是,当我在ASP.NET 4甚至3.5中创建Web应用程序时,控件的所有ID都是我为它们编写的。 They are not generated by the autoId algorithm. 它们不是由autoId算法生成的。 Then I tried to manually add clientIDMode="AutoID" to the controls, it also doesnt work what I was expected. 然后,我尝试将clientIDMode =“ AutoID”手动添加到控件,它也无法正常工作。 So what is the problem ? 那是什么问题呢? Is there any rule to make it available ? 是否有任何使其可用的规则?
Thanks in advance, 提前致谢,
EDITED EDITED
This is in .aspx page 这在.aspx页面中
<div>
<asp:Panel ID="Panel1" runat="server">
<asp:Panel ID="Panel2" runat="server">
<asp:Panel ID="Panel3" runat="server">
<asp:Panel ID="Panel4" runat="server">
(:
</asp:Panel>
</asp:Panel>
</asp:Panel>
</asp:Panel>
</div>
This is output: 输出为:
<div id="Panel1">
<div id="Panel2">
<div id="Panel3">
<div id="Panel4">
(:
</div>
</div>
</div>
</div>
The reason you are getting all your Id's coming out the way you are is because there is no reason for the .NET framework to change them. 之所以按自己的方式获取所有ID,是因为.NET框架没有理由更改它们。
If you had placed your Panel's within a Repeater control, then they would all change to avoid multiple ID's of the same name. 如果您已将面板放置在Repeater控件中,则它们都将更改以避免相同名称的多个ID。
Example (not correct markup): 示例(不正确的标记):
<asp:Repeater id="repeater1" runat="server">
<template>
<asp:Panel id="Panel1" runat="server">
<asp:Panel id="Panel2" runat="server">
</asp:Panel>
</asp:Panel>
</template>
</asp:Repeater>
Your HTML which is generated from this would show that the Panel id's have been changed to inherit from the containing control. 由此生成的HTML将显示已将Panel ID更改为从包含控件继承。 Something like repeater1_ct100_Panel1
像repeater1_ct100_Panel1
东西
The same happens when you are using Master Pages, Content Holders, and DataBound Controls. 当您使用母版页,内容所有者和DataBound控件时,也会发生相同的情况。 .NET updates the ID's to avoid multiple ID's of the same name. .NET更新ID,以避免多个同名的ID。
The differences between the different ClientIDMode values can be clearly seen if you nest one control into another; 如果将一个控件嵌套到另一个控件中,则可以清楚地看到不同ClientIDMode值之间的差异。 the AutoId mode names the control after each parent naming container, while the Static mode starts a new naming hierarchy. AutoId模式将在每个父命名容器之后命名控件,而Static模式将启动新的命名层次结构。 Predictable mode is mostly used with data binding controls, while Inherit mode causes the control to use its parent's ClientIDMode. 可预测模式主要用于数据绑定控件,而“ 继承”模式使控件使用其父级的ClientIDMode。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.