简体   繁体   English

为什么我看不到服务器端控件的生成ID?

[英]why I don't see generated IDs for server side controls?

I'm little confused here now. 我现在对此感到困惑。 Let me explain: 让我解释:

I've seen people talking about adding a button or some other control to the page in asp.net (3.5) and when the control renders it changes the Id of that control, eg. 我看到有人在谈论在asp.net(3.5)中向页面添加一个按钮或其他控件,并且当控件呈现时它会更改该控件的Id,例如。 Button1 becomes Button1_somethingsomething which prevents them from using jQuery and what they end up using is something such as <%controlId.ClientId %> Button1成为Button1_somethingsomething阻止他们使用jQuery,他们最终使用的是<%controlId.ClientId %>

So I did a little test 所以我做了一点测试

1. I added a button on the page:
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server"  Text="Button" />
<div>

2. Then I added a JavaScript and jQuery:
<script type="text/javascript">
                    $(document).ready(function() {
                    $("#Button1").click(function() {
                        alert("Hello world!");
                    });

                    });
                </script>

3. The generated html is this:
<div>
  <input type="submit" name="Button1" value="Button" id="Button1" />
<div>

Now, I don't see ASP.NET (asp.net 3.5) changing the ids. 现在,我没有看到ASP.NET(asp.net 3.5)改变了ID。 Why do I see different behavior? 为什么我会看到不同的行为?

Btw. 顺便说一句。 This does work when I hit the button! 当我按下按钮时,这确实有效!

Thanks. 谢谢。

ASP.NET only changes the IDs when the control is inside of a Naming Container . ASP.NET仅在控件位于命名容器内时更改ID。 This could include certain user controls, ContentPlaceHolders from a master page, and repeating controls (Repeater, GridView, etc.) 这可能包括某些用户控件,母版页中的ContentPlaceHolders以及重复控件(Repeater,GridView等)

您可能并不总是需要使用YourControl.ClientID,但这是一个很好的做法,以便当您的控件最终进入容器内时,您不必返回并修复它。

ASP.NET does not change the ID names by default, but does change them when they are used within a ContentPlaceHolder of a master page. 默认情况下,ASP.NET不会更改ID名称,但在母版页的ContentPlaceHolder中使用它们时会更改它们。 So the names end up being something like ctl00_ContentPlaceHolderContent_Button1. 所以名称最终会像ctl00_ContentPlaceHolderContent_Button1。 In your case, there is no master page and no change is made. 在您的情况下,没有母版页,也没有进行任何更改。

It's due to the INamingContainer Interface that certain controls (such as the asp:content control, used with master pages) implement. 由于INamingContainer接口,某些控件(例如asp:content控件,与母版页一起使用)实现。

From MSDN : 来自MSDN

Any control that implements this interface creates a new namespace in which all child control ID attributes are guaranteed to be unique within an entire application. 任何实现此接口的控件都会创建一个新的命名空间,其中所有子控件ID属性都保证在整个应用程序中是唯一的。 The marker provided by this interface allows unique naming of the dynamically generated server control instances within the Web server controls that support data binding. 此接口提供的标记允许在支持数据绑定的Web服务器控件中对动态生成的服务器控件实例进行唯一命名。 These controls include the Repeater, DataGrid, DataList, CheckBoxList, ChangePassword, LoginView, Menu, SiteMapNodeItem, and RadioButtonList controls. 这些控件包括Repeater,DataGrid,DataList,CheckBoxList,ChangePassword,LoginView,Menu,SiteMapNodeItem和RadioButtonList控件。

Your sample code does not use any of these controls. 您的示例代码不使用任何这些控件。 You do not use a Master Page with a ContentPlaceHolder. 您不使用具有ContentPlaceHolder的母版页。 Your IDs are not altered because of this. 因此,您的ID不会被更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM