简体   繁体   English

隐藏字段值未定义ASP.NET

[英]ASP.NET undefined from hiddenfield value

I'm trying to get the value from a hiddenfield but I'm getting an undefined alert. 我正在尝试从hiddenfield获取值,但是我收到了未定义的警报。 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());

You could try setting ClientIDMode to static if you're .net 4+. 如果您是.net 4+,则可以尝试将ClientIDMode设置为static。 You'll want to check that it is defined first. 您将要检查它是否首先定义。 If you want/need the js to be on master page. 如果您希望/需要将js显示在主页上。

<script type="text/javascript">
        $().ready(function () {
            alert($('#hdnPersonId').val());
        });
</script>
<asp:HiddenField ID="hdnPersonId" Value="1" runat="server" ClientIDMode="Static" />

It will not work from master page. 从母版页将无法使用。 You need to call it from Default.aspx or try 您需要从Default.aspx调用它或尝试

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

on master page but other pages that uses this master page should not have any control that contains hiddenPersonId in its id 在母版页上,但使用此母版页的其他页上的ID不得包含任何包含hiddenPersonId控件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM