简体   繁体   中英

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.

<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. Just remove the visible=false part. It'll stay hidden.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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