user3656185
user3656185

Reputation: 29

Increment value not going above 2

I am trying to increase an integer value by 1 on a button click event, but when i click it it increments the first time and remains e.g it does not increment to 3.

public partial class Form : System.Web.UI.Page
{
    int num = 1;

    protected void Page_Load(object sender, EventArgs e)
    {

        Label_PageNumber.Text = "Page0" + num.ToString();

    }



    protected void Button1_Click(object sender, EventArgs e)
    {
        num++;

        Label_PageNumber.Text = "Page0" + num.ToString();

    }
}

Upvotes: 0

Views: 767

Answers (4)

Mojtaba
Mojtaba

Reputation: 3513

ASP.Net is stateless; it means you could not store fields like win form. As Adil said, you can use view state to store values. Another option is using Session. There are some advantages with using Session instead of ViewState.

First, your page will be lighter, because view states values are embedded in page.

Second, Session is safer, because it remains in server, however, view states values are sent to the client.

Third, you will lose the view state values when you go to another page. In fact, you will have previous page's fields in new page (it may be a disadvantage in some cases).

You should know Microsoft introduced ASP.NET MVC without ViewSate.

Upvotes: 0

Adil
Adil

Reputation: 148120

The global variable are initialized on PostBack and previous value is lost, you can use ViewState if you want the value between PostBack.

public partial class Form : System.Web.UI.Page
{       
    public int Num
    {
         get {
                if(ViewState["num"] != null)
                   return int.Parse(ViewState["num"]); 
                else
                   return 0;  
              }
         set { ViewState["num"] = value; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {    
        Label_PageNumber.Text = "Page0" + Num.ToString();    
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        Num++;    
        Label_PageNumber.Text = "Page0" + Num.ToString();    
    }
}

The value of global variables is lost because the Http is a stateless protocol.

A stateless protocol (Http) does not require the HTTP server to retain information or status about each user for the duration of multiple requests. However, some web applications implement states or server side sessions using for instance HTTP cookies or Hidden variables within web forms.

View state is a repository in an ASP.NET page that can store values that have to be retained during postback. The page framework uses view state to persist control settings between postbacks.

  • You can use view state in your own applications to do the following:

  • Keep values between postbacks without storing them in session state or in a user profile.

  • Store the values of page or control properties that you define.

Upvotes: 3

Dilip Suvagiya
Dilip Suvagiya

Reputation: 394

One Simple change in your code , Make int variable as static:

public partial class Form : System.Web.UI.Page
{
 static int num = 1;

protected void Page_Load(object sender, EventArgs e)
{

    Label_PageNumber.Text = "Page0" + num.ToString();

}



protected void Button1_Click(object sender, EventArgs e)
{
    num++;

    Label_PageNumber.Text = "Page0" + num.ToString();

}

}

Upvotes: 0

Ede Troll
Ede Troll

Reputation: 16

The problem is that when you click on the Button, a PostBack will generate, and then your counter will set to 1. Use Sessions to store your current value!

public partial class _Default : System.Web.UI.Page 
{
    int num;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            num = 1;
            Session["number"] = num;
            Label_PageNumber.Text = "Page0" + num.ToString();
        }
        else
        {
            num = (int)Session["number"];
        }      
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        num++;
        Session["number"] = num;
        Label_PageNumber.Text = "Page0" + num.ToString();
    }
}

Upvotes: 0

Related Questions