使用 jQuery 查找 ASP.Net 控件的最佳方法是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/163535/
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 08:43:52  来源:igfitidea点击:

What is best method to find a ASP.Net control using jQuery?

asp.netjquerycontrols

提问by Joe Brinkman

In implementing my first significant script using jquery I needed to find a specific web-control on the page. Since I work with DotNetNuke, there is no guaranteeing the controls ClientID since the container control may change from site to site. I ended up using an attribute selector that looks for an ID that ends with the control's server ID.

在使用 jquery 实现我的第一个重要脚本时,我需要在页面上找到特定的 Web 控件。由于我使用 DotNetNuke,因此无法保证控件 ClientID,因为容器控件可能会因站点而异。我最终使用了一个属性选择器来查找以控件的服务器 ID 结尾的 ID。

$("select[id$='cboPanes']")

This seems like it might not be the best method. Is there another way to do this?

这似乎不是最好的方法。有没有另一种方法可以做到这一点?



@Roosteronacid - While I am getting the controls I want, I try to follow the idioms for a given technology/language. When I program in C#, I try to do it in the way that best takes advantage of C# features. As this is my first effort at really using jQuery, and since this will be used by 10's of thousands of users, I want to make sure I am creating code that is also a good example for others.

@Roosteronacid - 虽然我得到了我想要的控件,但我尝试遵循给定技术/语言的习语。当我使用 C# 编程时,我会尝试以最能利用 C# 特性的方式进行编程。由于这是我真正使用 jQuery 的第一次尝试,而且由于这将被成千上万的用户使用,我想确保我创建的代码对其他人来说也是一个很好的例子。

@toohool - that would definitely work, but unfortunately I need to keep the javascript in separate files for performance reasons. You can't really take advantage of caching very well if you inline the javascript since each "page" is dynamically generated. I would end up sending the same javascript to the client over and over again just because other content on the page changed.

@toohool - 这肯定会起作用,但不幸的是,出于性能原因,我需要将 javascript 保存在单独的文件中。如果您内联 javascript,您就不能真正利用缓存,因为每个“页面”都是动态生成的。我最终会一遍又一遍地向客户端发送相同的 javascript,只是因为页面上的其他内容发生了变化。



@Roosteronacid - While I am getting the controls I want, I try to follow the idioms for a given technology/language. When I program in C#, I try to do it in the way that best takes advantage of C# features. As this is my first effort at really using jQuery, and since this will be used by 10's of thousands of users, I want to make sure I am creating code that is also a good example for others.

@Roosteronacid - 虽然我得到了我想要的控件,但我尝试遵循给定技术/语言的习语。当我使用 C# 编程时,我会尝试以最能利用 C# 特性的方式进行编程。由于这是我真正使用 jQuery 的第一次尝试,而且由于这将被成千上万的用户使用,我想确保我创建的代码对其他人来说也是一个很好的例子。

@toohool - that would definitely work, but unfortunately I need to keep the javascript in separate files for performance reasons. You can't really take advantage of caching very well if you inline the javascript since each "page" is dynamically generated. I would end up sending the same javascript to the client over and over again just because other content on the page changed.

@toohool - 这肯定会起作用,但不幸的是,出于性能原因,我需要将 javascript 保存在单独的文件中。如果您内联 javascript,您就不能真正利用缓存,因为每个“页面”都是动态生成的。我最终会一遍又一遍地向客户端发送相同的 javascript,只是因为页面上的其他内容发生了变化。

回答by toohool

$("#<%= cboPanes.ClientID %>")

This will dynamically inject the DOM ID of the control. Of course, this means your JS has to be in an ASPX file, not in an external JS file.

这将动态注入控件的 DOM ID。当然,这意味着您的 JS 必须在 ASPX 文件中,而不是在外部 JS 文件中。

回答by Eric Schoonover

One thing that I have done in the past (in JavaScript not jQuery), in the above my JavaScript imports, is output the dynamic controls ID's similiar to what toohool recommends and assign them to variables that I reference in my script imports.

我过去做过的一件事(在 JavaScript 而不是 jQuery 中),在上面我的 JavaScript 导入中,输出动态控件 ID 类似于 toohoo 推荐的内容,并将它们分配给我在脚本导入中引用的变量。

Something like this, should allow you to take advantage of caching and still enable you to have the exact client IDs:

像这样的东西应该允许您利用缓存并仍然使您能够拥有确切的客户端 ID:

<head>
    <script type="text/javascript>
        var cboPanesID = <%= cboPanes.ClientID %>;
    </script>

    <!-- this JS import references cboPanesID variable declared above -->
    <script src="jquery.plugin.js"></script>
</head>

回答by cllpse

Other than being a bit more expensive, performance-wise, I can't see anything wrong with using that selector. After all; you are getting the controls you want to access.

除了在性能方面更贵一点之外,我看不出使用该选择器有什么问题。毕竟; 您将获得想要访问的控件。

回答by FlySwat

Use a marker class on the control, and select that via jQuery.

在控件上使用标记类,并通过 jQuery 选择它。