!ispostback在每次加载页面时都无法正常工作 [英] !ispostback is not work well its work every time when page is load

查看:62
本文介绍了!ispostback在每次加载页面时都无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

protected void Page_Load(object sender, EventArgs e)
   {
       if (!IsPostBack)
       {
        int i=0;
        //some code related to i//
       }
   }


protected void _lblnext_Click(object sender, EventArgs e)
{
 i+=1;
 //code related to i//
}


每当页面处于(!ispostback)代码中时,我总是以0进行初始化.


I always initialized with 0 when page is load whenever it is in (!ispostback) code

推荐答案

嗨Ashish,

请了解静态变量的概念.
默认情况下,它的值设置为0(零),并且它的值保留在两次函数切换调用之间.


但是在这种情况下,您已经在类中声明了静态变量,并在page_load事件中分配了值,因此您希望在回发请求中访问相同的值.对于静态变量,这是不可能的.在下一个回发请求中,i的值将不可用.

要实现此目的,您可以使用会话变量或viewstate.

使用以下代码:
Hi Ashish,

Please understand the concept of static variable.
By default its value set to 0(zero), and its value retain in between function switch call.


But here in your case you have declared static variable into the class and assigning the value in page_load event, you want to access same value in postback request. This is not possible with static variable the value of i will not be available in next postback request.

To implement this you can use session variable or viewstate.

use below code:
protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
         Session["abc"] = 0
         //some code related to i//
        }
    }
 
 protected void _lblnext_Click(object sender, EventArgs e)
 {
  int i=0;
  if(Session["abc"] !=null)
      i= Convert.ToInt32(Session["abc"]);
  else 
  {
    //return some error
  }
i+=1;
  //code related to i//
Session["abc"]=i;
 }


告诉我如何在_lblnext_Click()中访问局部变量i?解释你想要什么?
Tell me how can u access local variable i in _lblnext_Click()? Explain what u want ?


这篇关于!ispostback在每次加载页面时都无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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