简体   繁体   English

使用jquery在asp.net winforms中设置隐藏字段的问题

[英]Issue with setting hidden field in asp.net winforms using jquery

I have some code to set the value of a hidden field so I can access it in the code behind but the value is always empty in the code behind. 我有一些代码来设置隐藏字段的值,所以我可以在后面的代码中访问它,但后面的代码中的值总是为空。 The value for the effectiveDate is being set but I doesn't look like the hidden field property Value is being set. 正在设置effectiveDate的值,但我看起来不像隐藏的字段属性正在设置Value

<input id="appEffectiveDate" type="text" />
<label id="effectiveDateLabel" for="appEffectiveDate">App Effective Date</label>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<asp:HiddenField ID="appEffectiveDateToSetForUserRole" runat="server" Value="" Visible="false" />
<script>
$(function () {
    var SelectedDates = {};
    $('#appEffectiveDate').datepicker({
        beforeShowDay: function (date) {
            var Highlight = SelectedDates[date];
            if (Highlight) {
                return [true, "Highlighted", Highlight];
            }
            else {
                return [true, '', ''];
            }
        }
    });
    $("#effectiveDateLabel").hide();
    $("#appEffectiveDate").hide();
    $('input[value="85"]').click(function () {
        if($(this).is(':checked'))
        {
            $("#effectiveDateLabel").show();
            $("#appEffectiveDate").show();
        }
    });
    $("#appEffectiveDate").change(function () {
        var effectiveDate = $("#appEffectiveDate").val();
        $(":asp(appEffectiveDateToSetForUserRole)").prop('value', effectiveDate);
    });
});
</script> 

In the code behind the value is empty for the hidden field: 在后面的代码中隐藏字段的值为空:

if (!string.IsNullOrEmpty(appEffectiveDateToSetForUserRole.Value))
{
    // this is never called because .Value is empty
}

If Visible is set to false , the control will not be rendered by ASP.NET in the markup at all, which means that jQuery won't be able to find it because it doesn't exist. 如果Visible设置为false ,则ASP.NET中的标记根本不会呈现该控件,这意味着jQuery将无法找到它,因为它不存在。 Just remove the visible=false part. 只需删除visible = false部分即可。 It'll stay hidden. 它会隐藏起来。

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

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