Search code examples
c#asp.nethidden-field

ASP.NET undefined from hiddenfield value


I'm trying to get the value from a hiddenfield but I'm getting an undefined alert. What am I doing wrong?

// Masterpage
...
<body>
    <div class="container">
        <asp:ContentPlaceHolder ID="MasterContent" runat="server"></asp:ContentPlaceHolder>
    </div>
    <script>
        $(document).ready(function () {
            alert($('#hiddenPersonId').val());
        });
    </script>
</body>

// Default.aspx
<asp:Content ID="Content" ContentPlaceHolderID="MasterContent" runat="Server">
    <asp:HiddenField ID="hiddenPersonId" runat="server" Value="1" />
</asp:Content>

I tried other solutions but these are also not working:

alert($("#<%= hiddenPersonId.ClientID %>").val());

Solution

  • It will not work from master page. You need to call it from Default.aspx or try

     $('[id*="hiddenPersonId"]')
    

    on master page but other pages that uses this master page should not have any control that contains hiddenPersonId in its id