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

标签 c# jquery winforms

我有一些代码来设置隐藏字段的值,这样我就可以在后面的代码中访问它,但在后面的代码中该值始终为空。正在设置 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> 

在后面的代码中,隐藏字段的值为空:

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

最佳答案

如果 Visible 设置为 false,控件将不会在标记中由 ASP.NET 呈现,这意味着 jQuery 将无法找到它,因为它不存在。只需删除 visible=false 部分。它会保持隐藏状态。

关于c# - 使用 jquery 在 asp.net winforms 中设置隐藏字段的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29833584/

相关文章:

javascript - 使用javascript将系统中的图像粘贴到Html页面中

jquery - 单击图像不会在 chrome 中加载

c# - 在 C# 中选中和取消选中 TreeView 的所有节点

c# - 序列化在 c# 中实现 IEnumerable 的自定义泛型类型

c# - 我可以在我的 winforms c# 应用程序中使用 MapPoint

javascript - 单击要提交的行

c# - 根据 X Y 坐标更改 MouseHover 上的标签颜色

c# - AllowDrop 属性不适用于 ToolStripItems

c# - 康威的生命游戏逻辑错误

c# - TcpListener TcpClient 获取 IP 地址