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

后端 未结 4 1951
我寻月下人不归
我寻月下人不归 2021-02-07 11:53

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 control

相关标签:
4条回答
  • 2021-02-07 12:07

    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.

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

    <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>
    
    0 讨论(0)
  • 2021-02-07 12:08

    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.

    0 讨论(0)
  • 2021-02-07 12:14

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

    0 讨论(0)
  • 2021-02-07 12:20
    $("#<%= 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.

    0 讨论(0)
提交回复
热议问题