jQuery 在每个对象上设置 ClientIDMode = Static 是否有任何缺点(在母版页的 maincontent 上设置)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6057490/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 20:19:03  来源:igfitidea点击:

Is there any drawback to set ClientIDMode = Static on every object ( set on maincontent of master page)

jqueryasp.netclient-side

提问by Sarawut Positwinyu

I am working on asp.net project and each time i need to use jquery identifier $(#"objectID"). I have to change the ClientIDMode on each object to be static. Since I have noticed that the default client ID mode is Inherit so i set the MainContent Client ID mode to be static and i have found that all the object became static.

我正在做 asp.net 项目,每次我需要使用 jquery 标识符 $(#"objectID")。我必须将每个对象上的 ClientIDMode 更改为静态。因为我注意到默认的客户端 ID 模式是继承,所以我将 MainContent 客户端 ID 模式设置为静态,我发现所有对象都变成了静态。

This will sure save a lot of time when working with jquery, but i just want to know is there any drawback from this and is there any reason why shouldn't ClientIDMode set to be static at the first place ?

这肯定会在使用 jquery 时节省大量时间,但我只想知道这是否有任何缺点,是否有任何理由为什么不首先将 ClientIDMode 设置为静态?

采纳答案by Graham Clark

You want to be careful about setting the ClientIDMode to Static for things like user controls, or you could end up with multiple elements with the same ID.

对于用户控件之类的内容,您要小心将 ClientIDMode 设置为静态,否则最终可能会有多个具有相同 ID 的元素。

For data-bound controls like GridView, you'll also want to use the ClientIDRowSuffix property in order to ensure each row is differentiated.

对于像 GridView 这样的数据绑定控件,您还需要使用 ClientIDRowSuffix 属性来确保每一行都是不同的。

This posthas some good examples.

这篇文章有一些很好的例子。

回答by Krishna Tummalapalli

Another way to deal with the IDs in JavaScript would be to do something like this:

在 JavaScript 中处理 ID 的另一种方法是执行以下操作:

var something = '<%= btnId.ClientID %>';

Example: If you have a button control like this:

示例:如果您有这样的按钮控件:

<asp:Button ID="btnId" runat="server"></asp:Button>    

and the ID is translated to id="ct100_ContentPlaceHolder1_btnId"then you could use the variable somethingto access the control.

并将 ID 转换为id="ct100_ContentPlaceHolder1_btnId"然后您可以使用该变量something来访问控件。