简体   繁体   English

如何在ASP.NET 4中使用ClientIDMode

[英]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.

相关问题 在ASP.NET 3.5中使用ASP.NET 4功能“ ClientIDMode” - Use ASP.NET 4 feature “ClientIDMode” in ASP.NET 3.5 javascript到asp.net ClientIDMode - javascript to asp.net ClientIDMode 我们应该如何使用asp.net 4.0附带的ClientIDMode属性? - How should we use ClientIDMode property that comes with asp.net 4.0? ASP.Net-命名容器和ClientIDMode =“ Static” - ASP.Net - Naming Containers and ClientIDMode=“Static” 当clientidmode为asp.net页面的静态时,如何获取javascript中的span文本 - How to get the text of span in javascript when the clientidmode is static of a asp.net page 在项目或解决方案级别更改默认的ASP.NET ClientIDMode? - Change the default ASP.NET ClientIDMode at the project or solution level? asp.net 4.0:INPUTs的名称是否有ClientIDMode的等价物? - asp.net 4.0: is there any equivalent of ClientIDMode for the names of INPUTs? ASP.Net 4中ClientIDMode的正确设置是什么,以获得ASP.Net 2.0渲染。 - What is the correct setting of ClientIDMode in ASP.Net 4 to get ASP.Net 2.0 rendering. 在可预测的ClientIDMode中时,asp.net GridView行的ID不会更改控件clientid - asp.net GridView Row's id does not change controls clientid when in Predictable ClientIDMode ASP.NET:UpdateProgress不适用于ClientIDMode =“Static”的控件 - ASP.NET: UpdateProgress doesn't work with controls that have ClientIDMode=“Static”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM