为什么我的数据在回发后丢失了
我有一个简单的表单:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>
<!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>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1"
runat="server" Text="Button" onclick="Button1_Click" />
</div>
</form>
</body>
</html>
后面的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication1
{
public partial class _Default : System.Web.UI.Page
{
string myVariable;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
myVariable = "abc";
}
}
protected void Button1_Click(object sender, EventArgs e)
{
}
}
}
在 FormLoad 中,myVariable 被分配给“abc”。
在表单中,在文本框中输入内容并提交表单。 myVariable 的值变为“null”
为什么 myVariable 中的数据丢失了? :(
我正在使用:VS2008 SP1 请帮忙!
I have a simple form as:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>
<!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>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1"
runat="server" Text="Button" onclick="Button1_Click" />
</div>
</form>
</body>
</html>
And code behind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication1
{
public partial class _Default : System.Web.UI.Page
{
string myVariable;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
myVariable = "abc";
}
}
protected void Button1_Click(object sender, EventArgs e)
{
}
}
}
At FormLoad, the myVariable is assigned to "abc".
At form, enter something to textbox and submit form.
The myVariable's value is turned to "null"
Why my data at myVariable is lost? :(
I'm using: VS2008 SP1
Please help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
引自 ASP .NET 状态管理 。我强烈建议您浏览一下 ASP .NET 的状态管理功能。
此外,所有状态管理功能并不意味着存储所有内容。 这是另一篇文章,它将建议在何种情况下使用哪种状态管理功能。
Quoted from ASP .NET State Management. I thoroughly suggest you go through the State Management features of ASP .NET.
Also, all state management features aren't meant to store everything. This is another article, that will recommend which state management feature to use for what sort of situation.
事情就是这样(HTTP 是无状态的)。您必须
每次都初始化变量。
或者将其保存到 Session 集合中。
并使用 Session["myVariable"] 作为字符串
That is just the way it is(HTTP is stateless). You'll have to do
where you are initializing your variable everytime.
or save it to the Session collection.
and use
Session["myVariable"] as String
对象实例在页面加载过程中并不持久。将向服务器发出的每个请求视为页面类的全新实例。为了跨多个页面请求持久保存数据,您需要将其存储在页面类的范围之外。您有多种选择:
但它需要在页面类的范围之外,因为该类是通过以下方式重新实例化的:每个页面请求。
Object instances aren't persistent across page loads. Think of each request made to the server as an entirely new instantiation of page classes. In order to persist data across multiple page requests, you need to store it outside the scope of the page class. You have a number of options:
But it needs to be outside the scope of the page's class, because the class is re-instantiated with each page request.
当您声明:
字符串 myVariable;
您在 Page 类的范围内声明它。然而,想想这里的过程。
1) 在 Page 类的开头声明它没有任何值。
2)您检查Page_Load以查看它是否是回发。如果不是,给它一个值,abc。
现在,当您提交表单时,它是一个回发。因此,Page_Load中的赋值块不会运行!因此它保留为与首次加载页面时相同的值...即 NULL。
有道理吗?
尝试这样的事情:
IMO 我不认为状态管理是问题的本质,而不是变量范围。如果OP不理解范围,那么状态就有点车/马的问题。
when you declare:
string myVariable;
you declare it in the scope of the Page class. However, think about the process here.
1) You declare it with no value at the start of the Page class.
2) You check in Page_Load to see if it's a Postback. If no, give it a value, abc.
Now, when you submit the form, it IS a postback. Therefore, the assignment block in Page_Load doesn't run! So it is left as the same value it was when the Page was first loaded... which is NULL.
Make sense?
Try something like this:
IMO I don't think state management is the nature of the question, as opposed to variable scope. If the OP doesn't understand scope, then state is a bit cart/horse problem.