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

时间:2021-04-19 16:50:02

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,因为页面上的其他内容发生了变化。

4 个解决方案

#1


8  

$("#<%= 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文件中。

#2


3  

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类似于toohool推荐的,并将它们分配给我在脚本导入中引用的变量。

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>

#3


2  

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

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

#4


2  

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.

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

#1


8  

$("#<%= 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文件中。

#2


3  

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类似于toohool推荐的,并将它们分配给我在脚本导入中引用的变量。

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>

#3


2  

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

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

#4


2  

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.

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