我有一个简单的表格:
<%@ 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 请帮忙!
最佳答案
A new instance of the Web page class is created each time the page is posted to the server. In traditional Web programming, this would typically mean that all information associated with the page and the controls on the page would be lost with each round trip. For example, if a user enters information into a text box, that information would be lost in the round trip from the browser or client device to the server.
引自ASP .NET State Management .我完全建议您了解 ASP .NET 的状态管理功能。
此外,所有状态管理功能并不意味着存储所有内容。 This is another article ,这将推荐在哪种情况下使用哪种状态管理功能。
关于c# - 为什么回发后我的数据丢失了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5531294/