为什么我的数据在回发后丢失了

发布于 2024-10-29 05:34:24 字数 1470 浏览 1 评论 0原文

我有一个简单的表单:

        <%@ 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

ぃ弥猫深巷。 2024-11-05 05:34:24

每次打开页面时都会创建一个网页类的新实例
发布到服务器。在传统的
Web 编程,这通常是
意味着所有相关的信息
与页面和控件
每轮都会丢失页面
旅行。例如,如果用户输入
信息输入文本框,即
信息将全面丢失
来自浏览器或客户端设备的旅程
到服务器。

引自 ASP .NET 状态管理 。我强烈建议您浏览一下 ASP .NET 的状态管理功能。

此外,所有状态管理功能并不意味着存储所有内容。 这是另一篇文章,它将建议在何种情况下使用哪种状态管理功能。

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.

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.

我的黑色迷你裙 2024-11-05 05:34:24

事情就是这样(HTTP 是无状态的)。您必须

    string myVariable;
    protected void Page_Load(object sender, EventArgs e)
    {
           myVariable = "abc";
    }

每次都初始化变量。

或者将其保存到 Session 集合中。

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Session["myVariable"] = "abc";
        }
    }

并使用 Session["myVariable"] 作为字符串

That is just the way it is(HTTP is stateless). You'll have to do

    string myVariable;
    protected void Page_Load(object sender, EventArgs e)
    {
           myVariable = "abc";
    }

where you are initializing your variable everytime.

or save it to the Session collection.

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Session["myVariable"] = "abc";
        }
    }

and use Session["myVariable"] as String

万劫不复 2024-11-05 05:34:24

对象实例在页面加载过程中并不持久。将向服务器发出的每个请求视为页面类的全新实例。为了跨多个页面请求持久保存数据,您需要将其存储在页面类的范围之外。您有多种选择:

  • 将其存储在会话状态中
  • 将其存储在应用程序状态中
  • 将其存储在数据库中
  • 将其存储在文件
  • 中等。

但它需要在页面类的范围之外,因为该类是通过以下方式重新实例化的:每个页面请求。

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:

  • Store it in Session State
  • Store it in Application State
  • Store it in a database
  • Store it in a file
  • etc.

But it needs to be outside the scope of the page's class, because the class is re-instantiated with each page request.

草莓味的萝莉 2024-11-05 05:34:24

当您声明:
字符串 myVariable;

您在 Page 类的范围内声明它。然而,想想这里的过程。

1) 在 Page 类的开头声明它没有任何值。
2)您检查Page_Load以查看它是否是回发。如果不是,给它一个值,abc。

现在,当您提交表单时,它是一个回发。因此,Page_Load中的赋值块不会运行!因此它保留为与首次加载页面时相同的值...即 NULL。

有道理吗?

尝试这样的事情:

protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                myVariable = "abc";
            }
else
{
// hey im a postback, i need a value though!
myVariable = "xyz";
}
        }

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:

protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                myVariable = "abc";
            }
else
{
// hey im a postback, i need a value though!
myVariable = "xyz";
}
        }

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文