ASP.NET - 某些成员变量未按预期工作?

发布于 2024-10-21 00:07:16 字数 694 浏览 1 评论 0原文

我一直在 ASP.NET 工作——刚刚开始,我有一个非常简单的问题。我有一个按钮,当您单击它时,它会向下拉框中添加一些简单的文本。代码如下:

public partial class _Default : System.Web.UI.Page
{
    private int buttonclickedtimes = 1;
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        this.DropDownList1.Items.Add(new System.Web.UI.WebControls.ListItem("Clickin' mah buttonz " + buttonclickedtimes + " times!"));
        buttonclickedtimes++;
    }
}

但是 buttonclickedtimes 始终显示为 1。如果我将变量设为静态,它就会按预期工作。但是 DropDownList 是一个成员变量,并且它显然是有状态的 - 正如我所期望的那样工作。我不明白这种行为 - 当然所有成员变量在请求之间要么保存,要么不保存?我在 VS2010 中以调试模式运行。

I've been working in ASP.NET- just starting out, and I have a pretty simple question. I've got a button, and when you click it, it adds some simple text to a drop-down box. Here's the code:

public partial class _Default : System.Web.UI.Page
{
    private int buttonclickedtimes = 1;
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        this.DropDownList1.Items.Add(new System.Web.UI.WebControls.ListItem("Clickin' mah buttonz " + buttonclickedtimes + " times!"));
        buttonclickedtimes++;
    }
}

But buttonclickedtimes always comes up as one. If I make the variable static, it works as intended. But the DropDownList is a member variable and it's clearly properly stateful- as in, working as I expect. I don't understand this behaviour- surely all member variables are either saved or not saved between requests? I'm running in debug mode in VS2010.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

标点 2024-10-28 00:07:16

不可以,成员变量在请求之间是不保存的,如果要保存,需要放到Viewstate中。

控件的状态保存在 Viewstate 中(如果启用了 viewstate),因此这就是在请求之间保存它的原因。

要保存在 Viewstate 中,请将以下内容添加到 Pre_Render 方法中:

ViewState.Add("buttonClickTracker", buttonclickedtimes);

然后访问它:

buttonclickedTimes = Viewstate[ “buttonClickTracker”];

No, member variables are not saved between requests, if you want to save it, you need to put it in Viewstate.

The Control's state is saved in Viewstate (if you have viewstate enabled) so that's why it's being saved between requests.

To save in Viewstate, add the following to a Pre_Render method:

ViewState.Add("buttonClickTracker", buttonclickedtimes);

And then access it:

buttonclickedTimes = Viewstate["buttonClickTracker"];

够运 2024-10-28 00:07:16

我假设您正在使用 asp.net webforms。
每次您向服务器发回帖子时,这基本上都是一个新请求,并且页面将被再次加载和初始化。

因此,如果您想在多个请求之间保留数据,那么您必须使用某种机制(例如会话变量)来存储它。

例如。 Session["ButtonClickedTimes"] +=1; 将在每次请求后递增它。

I assume that you are using asp.net webforms.
Here every time you make a post back to the server it's basically a new request and page will be loaded and initialized again.

So If you want to preserve data between multiple requests then you have to use some mechanism like sesison variables to store it.

for eg. Session["ButtonClickedTimes"] +=1; will increment it after every request.

剩一世无双 2024-10-28 00:07:16

这归结为页面生命周期“问题”。所发生的情况是页面被构建(并且您的按钮点击次数被设置为初始值 1)。然后它经历页面生命周期(初始化、加载、事件处理、渲染等),并且变量的内存分配被释放。但是,当变量是静态的时,服务器知道要保留该变量的内存分配,并且对其进行的任何更改都会持续存在。但是,该静态变量会保留在该页面中。这意味着对于点击您的按钮的 x 个用户,该变量将增加 x 次。

如果您希望变量在单个用户实例中持续存在,您可以使用 ViewState 或可能的 Session 状态来保存信息。

希望这有帮助。

This comes down to a page life cycle "issue". What happens is that the page gets constructed (and your buttonclickedtimes get set to the initial value of 1). Then it goes through the page lifecycle (init, load, event handeling, rendering, etc) and the memory allocation for the variable gets released. However, when the variable is static, the server knows to hang onto the memory allocation for the variable and any changes to it persist. However, that static variable is persisted for that page. Meaning that for x users who click your button, that variable will be incremented x times.

If you want the variable to persist for a single user instance, you could use ViewState or possibly Session state to save the information.

Hope this is helpful.

祁梦 2024-10-28 00:07:16

由于 Web 应用程序是无状态的......它每次重新加载页面时都会启动变量。这就是我们进行 Page.IsPostback 检查的原因。

对于状态,您应该使用视图状态或会话状态...

静态类有一个实例,因此无论页面加载如何它们都会保留该值...

我希望这对您有所帮助。

Since web application are stateless ... it initiate the variable each time it reloads the page. Thats why we have Page.IsPostback check.

For state, you should either use viewstate or session state ....

Static classes have one intance and hence they retain the value irrespective of page loads...

I hope this helps you.

森林散布 2024-10-28 00:07:16

在 ASP.NET 中每次发布​​页面时都会实例化

因此,您的局部变量为 1,因为每次单击按钮时 _Default 类都是一个新类。

如果要保存状态,可以使用ViewState对象或Session对象。

我建议您阅读 MSDN 上的一些 Dino Esposito 的 ASP.NET 概述,他擅长解释所有内容的这个。

In ASP.NET the Page is instantiated every time it is posted.

So your local variable is 1 because the _Default class is a new class each time you click the button.

If you want to save the state, you can use the ViewState object or Session object.

I recommend you read some of Dino Esposito's ASP.NET overviews on MSDN, he`s good at explaining all of this.

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