简体   繁体   English

ASP.Net C#变量不存储在网页上

[英]ASP.Net C# Variables not storing on web page

So i have the code.. 所以我有代码..

int var1 = 0;

protected void cmdvar1_Click(object sender, EventArgs e)
    {
        var1= var1+ 10;
        lblvar1.Text = var1.ToString();
    }

And when clicking this, its great.. it takes the int, adds 10 to it, then displays it.. however it won't display any more than 10, did some playing around and came to the conclusion, that its not because the label isnt updating, it just simply isnt adding 10 to the previous 10 on the variable. 当点击这个,它很棒..它需要int,加上它,然后显示它..然而它不会显示任何超过10,做了一些游戏并得出结论,这不是因为标签没有更新,它只是简单地没有在变量上添加10到前10。 What am i doing wrong? 我究竟做错了什么? What am I mising? 我在做什么? Do i need to store the variable info in a cookie or something? 我是否需要将变量信息存储在cookie或其他内容中?

This is due to the lifecycle of ASP.NET. 这是由于ASP.NET的生命周期。 Storing private fields behind the web page isn't the same as how it works with WinForms. 在网页后面存储私有字段与使用WinForms的方式不同。 If you want to persist information across post backs you need to store it in some persistent storage ie Session / ViewState / Database etc. 如果你想在帖子后面保留信息,你需要将它存储在一些持久存储中,即Session / ViewState / Database等。

private int var1 = 0;

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        // load var1 from cache
        Int32.TryParse((Session["var1"] ?? 0).ToString(), out var1);
    }
}

protected void cmdvar1_Click(object sender, EventArgs e)
{
    var1 += 10;
    Session["var1"] = var1; // cache var1
    lblvar1.Text = var1.ToString();
}

So I would strongly suggest looking into a different platform. 所以我强烈建议寻找一个不同的平台。 Perhaps ASP.NET MVC... However you can use something like the following to get arround your problem. 也许ASP.NET MVC ......但是你可以使用类似下面的内容来解决你的问题。

private int MyNum
{
   get{ return (int)ViewState["MyNum"] ?? 0; }
   set { ViewState["MyNum"] = value; }
}

Then just use MyNum as your integer your incrementing. 然后使用MyNum作为递增的整数。

Use Session body, HTTP is a stateless Protocol, once you postback you loose the current variable value, 使用Session主体,HTTP是无状态协议,一旦你回发松散当前变量值,

Solution : 方案

int var1=0;
protected void cmdvar1_Click(object sender, EventArgs e)
    {

if(Session["var1"]!=null)
var1=int.Parse(Session["var1"].ToString());
else
var1=0;
        var1= var1+ 10;
        lblvar1.Text = var1.ToString();
Session["var1"]=var1;
    }

Assuming that lblvar1 is a Label control then you can do the following. 假设lblvar1是Label控件,那么您可以执行以下操作。 The reason this will work is because .NET automatically take care of the state of UIControl 这将起作用的原因是因为.NET自动处理UIControl的状态

protected void cmdvar1_Click(object sender, EventArgs e)
    {
        var var1 = Convert.ToInt32(lblvar1.Text);
        var1= var1+ 10;
        lblvar1.Text = var1.ToString();
    }

As suggested in the comments, you have to wrap your universe around statelessness before you will get to producing meaningful web applications. 正如评论中所建议的那样,在创建有意义的Web应用程序之前,必须将Universe包装在无状态中。

The ASP.NET equivalent to accomplish state-like behavior is to use the Session collection which is available to every web page. ASP.NET等效于完成类似状态的行为是使用可用于每个网页的Session集合。

protected void cmdvar1_Click(object sender, EventArgs e)
{
   int var1 = (int)Session["yourInteger"];
   var1 += 10;
   Session["yourInteger"] = var1;
   lblvar1.Text = var1.ToString();
}

You are obviously setting an initial value for Session["yourInteger"] somewhere else, just one time. 你显然是在其他地方设置Session [“yourInteger”]的初始值,只是一次。

The problem with Session is that it makes your application potentially buggy and somewhat unscalable. Session的问题在于它使您的应用程序可能出错并且有些不可扩展。 The more you use it, the worse on both accounts. 您使用的越多,两个帐户的情况就越糟糕。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM