我在aspx
页面上有以下隐藏的输入字段:
<asp:HiddenField ID="MySecretValue" runat="server" />
<asp:Button ID="Submit" runat="server" Text="Save" OnClick="submit_Click" CssClass="btn btn-primary" />
代码背后的代码:
protected void Page_Load(object sender, EventArgs e)
{
}
protected void submit_Click(object sender, EventArgs e)
{
// Do some stuff
}
隐藏的输入是通过一些jQuery脚本填充的。当用户提交页面时,我会在submit_Click
方法中执行一些操作并重新显示页面。但是当页面重新显示时,隐藏的输入字段为空。如何在重新显示页面时确保隐藏字段保留值?
答案 0 :(得分:0)
此代码在回发后不会失去价值...试试这个
在页面指令或web.config中启用EnableViewState="true"
的.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="StackOverflow_Solve.Others.WebForm1" EnableViewState="true" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="../js/jquery.js" type="text/javascript"></script>
<script>
$(function () {
//First time loaded value
console.log($('#MySecretValue').val());
$('#setHiddenValue').click(function() {
$('#MySecretValue').val('1');
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:HiddenField ID="MySecretValue" runat="server" ClientIDMode="Static" />
<asp:Button ID="Submit" runat="server" Text="Save" CssClass="btn btn-primary"
onclick="Submit_Click" />
</div>
<div id="setHiddenValue">setHiddenValueByClick me</div>
</form>
</body>
</html>
和.aspx.cs
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Submit_Click(object sender, EventArgs e)
{
//do all stuff
String a = MySecretValue.Value;
var dd = a;
}
}
此链接可以帮助您Link