Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
C#
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//
}

I always initialized with 0 when page is load whenever it is in (!ispostback) code
Posted
Updated 3-Oct-12 23:09pm
v2
Comments
Sergey Alexandrovich Kryukov 16-Feb-13 1:15am    
Please stop posting non-answers as "solution". It can give you abuse reports which eventually may lead to cancellation of your CodeProject membership.
—SA

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:
C#
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;
 }
 
Share this answer
 
v4
Tell me how can u access local variable i in _lblnext_Click()? Explain what u want ?
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900