Asp.net"全球及QUOT;变量 [英] Asp.net "Global" variables

查看:175
本文介绍了Asp.net"全球及QUOT;变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写在ASP.NET页面时遇到下列初始化上回发的周期问题:

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....
         }
    }
}

和正在发生的事情是,我的code拿起passedString就好了,但由于某些原因,在回发,其重置为默认值 - 即使我放在Page_Init $默认的分配C $Ç......这使我想知道这是怎么回事。

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..

任何帮助吗?

推荐答案

您的类成员变量不住就曾经响应发送到浏览器。尝试使用Session对象,而不是:

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....
         }
    }
}

这篇关于Asp.net"全球及QUOT;变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆