Search code examples
jquerymaster-pagesselectorcontent-pages

jquery best selector for pages base on master and content page (In Content Page)


i am looking for the best selector (jquery) in content pages !

i found the below selectors , but what is better and i should use that in .net 4?

        alert($('#hfFirstTimePageLoad').val()); -> NOT Ok
        $('#ContentPlaceHolder1_hfFirstTimePageLoad').val("This Is A Test"); -> Ok
        alert($('#ContentPlaceHolder1_hfFirstTimePageLoad').val()); -> Ok
        alert($('#<%= hfFirstTimePageLoad.ClientID%>').val()); -> Ok

thanks for leading me


Solution

  • In .NET 4 you can set your controls to use ClientIdMode Static and this should make the Id's appear as you define them. That's my preferred way if using .NET 4.

    You can place the property on the element itself, the master page or in the web config

    //On the Control
    <asp:Button ID="Button1" runat="server" Text="Button" ClientIDMode="Static" />
    
    //Master Page and web.config (all controls)
    <%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Site.master.cs"   
        Inherits="WebApplication2.SiteMaster" ClientIDMode="Static" %>
    
    <system.web>
        <pages clientIDMode="Static"></pages>
    </system.web>
    
    //HTML
    <input id="Button1" type="submit" value="Button" name="ctl00$MainContent$Button1">
    

    http://msdn.microsoft.com/en-us/library/system.web.ui.control.clientidmode.aspx

    https://www.west-wind.com/weblog/posts/54760.aspx