Asp.net“全球” 变量

发布于 2024-07-15 17:24:03 字数 729 浏览 9 评论 0原文

我正在 ASP.NET 中编写一个页面,并且在回发初始化周期之后遇到问题:

我有(类似于)以下内容:

public partial class MyClass : System.Web.UI.Page
{
    String myString = "default";

    protected void Page_Init(object o, EventArgs e)
    {
        myString = Request["passedString"];
        //note that I've tried to set the default here in Init on NULL...
    }

    protected void Page_Load(object o, EventArgs e)
    {
         if(!Postback)
         {
             //code that uses myString....
         }
         else
         {
            //more code that uses myString....
         }
    }
}

发生的情况是我的代码很好地拾取了“passedString”,但是对于出于某种原因,在回发时,它会重置为默认值 - 即使我将默认值的分配放在 Page_Init 代码中......这让我想知道发生了什么......

有什么帮助吗?

I'm writing a page in ASP.NET and am having problems following the cycle of initialization on postbacks:

I have (something akin to) the following:

public partial class MyClass : System.Web.UI.Page
{
    String myString = "default";

    protected void Page_Init(object o, EventArgs e)
    {
        myString = Request["passedString"];
        //note that I've tried to set the default here in Init on NULL...
    }

    protected void Page_Load(object o, EventArgs e)
    {
         if(!Postback)
         {
             //code that uses myString....
         }
         else
         {
            //more code that uses myString....
         }
    }
}

And what's happening is that my code picks up the "passedString" just fine, but for some reason, on postback, it resets to the default value - even if I put the assignment of the default in the Page_Init code... which makes me wonder what's going on..

Any help?

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

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

发布评论

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

评论(2

远昼 2024-07-22 17:24:03

一旦响应发送到浏览器,您的类成员变量就不再存在。 尝试使用 Session 对象代替:

public partial class MyClass : System.Web.UI.Page
{    

    protected void Page_Init(object o, EventArgs e)
    {
        Session["myString"] = Request["passedString"];
        //note that I've tried to set the default here in Init on NULL...
    }

    protected void Page_Load(object o, EventArgs e)
    {
         string myString = (string) Session["myString"];

         if(!Postback)
         {
             // use myString retrieved from session here
         }
         else
         {
            //more code that uses myString....
         }
    }
}

Your class member variables do not live on once the response is sent to the browser. Try using the Session object instead:

public partial class MyClass : System.Web.UI.Page
{    

    protected void Page_Init(object o, EventArgs e)
    {
        Session["myString"] = Request["passedString"];
        //note that I've tried to set the default here in Init on NULL...
    }

    protected void Page_Load(object o, EventArgs e)
    {
         string myString = (string) Session["myString"];

         if(!Postback)
         {
             // use myString retrieved from session here
         }
         else
         {
            //more code that uses myString....
         }
    }
}
拧巴小姐 2024-07-22 17:24:03

我感受到你的痛苦,马特。 我不久前问了一个类似的问题:

为了进一步了解页面生命周期,请查看这个问题:ASP.NET WebForm 的“页面生命周期”是什么?

I feel your pain Matt. I asked a similar question a little while ago:

For a further understanding of the Page Life Cycle check out this question: What is the 'page lifecycle' of an ASP.NET WebForm?

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