Asp.net“全球” 变量
我正在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一旦响应发送到浏览器,您的类成员变量就不再存在。 尝试使用 Session 对象代替:
Your class member variables do not live on once the response is sent to the browser. Try using the Session object instead:
我感受到你的痛苦,马特。 我不久前问了一个类似的问题:
为了进一步了解页面生命周期,请查看这个问题: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?