回发后全局整数变量为零 [英] Global integer variable get zero after postback

查看:84
本文介绍了回发后全局整数变量为零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的asp.net应用程序中,我声明了全局变量,数据类型是int。但是当发生回发时,它无法存储以前的值。它变为零。我需要保留这些值以便以后使用。可能吗。请帮帮我。

In my asp.net application I declare global variable and datatype is int. But when a postback happened it can not store the previous value. It becomes zero. I need to keep these values to use later. Is it possible. Please help me.

推荐答案

1.网络应用程序与桌面应用程序不同。在每个回发中,重新创建管理UI事件的整个应用程序的对象,以及静态变量。



2.为了保留回发之间的值,你必须缓存他们的价值观如果是一个不依赖于当前Web用户(在应用程序级别)的全局对象,则应将其缓存在 Application 缓存中,如果该对象仅依赖于每个Web用户会话,您应该将其缓存在 Session 缓存中。



3.有关在回发之间保存数据状态的详细信息和/或页面请参阅下一个链接: http://msdn.microsoft。 com / en-us / library / 75x4ha6s(v = vs.100).aspx [ ^ ]
1.The web application is different then the desktop application. On each postback the entire application's objects that manage the UI events are recreated, so also the static variable.

2.In order the preserve the value between postbacks you have to cache their values. If is a global object that does not depends on the current web user (is at application level) you should cache it in Application cache, if the object depends only on each web user session, you should cache it on Session cache.

3.For details about saving the data state between postbacks and/or pages see the next link: http://msdn.microsoft.com/en-us/library/75x4ha6s(v=vs.100).aspx[^]


试试..



try..

public static int i = 0;
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{
    i++;
    lblMsg.Text = "Clicked"+i.ToString();
}



如果你想重置页面加载中的值然后使用..




if you want to reset the value in page load then use..

protected void Page_Load(object sender, EventArgs e)
{
 if (!IsPostBack)
 {
  i = 0;
 }

}


使用 IsPostBack [ ^ ]属性,用于添加对Page_Load方法的检查。



Use the IsPostBack [^]property to add a check on your Page_Load method.

int i = 0;

private void Page_Load()
{
    if (!IsPostBack)
    {
       //only initialise if not a post back
       i = 100;
    }
}


这篇关于回发后全局整数变量为零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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