c# - 为什么回发后我的数据丢失了

标签 c# .net visual-studio-2008-sp1

我有一个简单的表格:

        <%@ 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/

相关文章:

c# - 有没有办法让 ValidationSummary 与客户端验证器一起工作?

c# - 为什么协变隐式转换会忽略通用约束?

c# - 在 .NET 中忽略 DST(夏令时)

visual-c++ - JPEG 提取 DCT 表

c# - 当分配给命令的连接处于挂起的本地事务中时,ExecuteNonQuery 要求命令具有事务

c# - 列出不同类型

C# ASP.Net Webforms - 字符串 - 静态与静态只读

c# - IPC——消息队列

linker - 使用 CMake 从静态库链接 Windows DLL 文件,而无需手工制作未解析的符号名称

c# - 如何使 [DebuggerDisplay] 尊重继承的类或至少使用集合?