1

I am using ASP.net in my project.

I Declare a Variable btn_clik, tot at the beginning of Class.

 public partial class Table : System.Web.UI.Page
    {
        public int btn_click = 1, tot = 0;

I have two button, Up And Down. When I click Up button btn_click variable want to decrease. And Down Button btn_click variable want to increase.

 protected void btnUp_Click(object sender, EventArgs e)
 {
            if (btn_click != 1) { btn_click--;}              
 }

 protected void btnDown_Click(object sender, EventArgs e)
 {
       if (btn_click < tot) { btn_click++;}               
 }

But At the Page load,.. btn_click vale is 1; then I Click Down Button,.. btn_click value is 2. I repeat the Down Button Click,.. But btn_click value is still 2; I Check that with breakpoints. Each Time btn_click Variable is going to 1, At that time of Button Click.

What is the Mistake...

0

2 Answers 2

3

ASP.Net instances do not persist across HTTP requests.
Each requests gets a new instance of your page class.

You need to store the value in session state or viewstate.

Sign up to request clarification or add additional context in comments.

3 Comments

I guess adding "totally" makes something more irrelevant.
NanDri(Thanks in Tamil Language).
Where I Can Read the Basic Things of ASP.NET?.
2

You should not use Session variables in this context. Replace you code from session variables to ViewState variables. @SLaks, I hope you are agreed with this. Obviously this is doing the same job BUT with good approach

Below is the sample code...

 protected void btnUp_Click(object sender, EventArgs e)
 {
      if (ViewState["btn_click"] == null)
            btn_click = 0;
        else
            btn_click = (int)ViewState["btn_click"];
      if (btn_click != 1) { btn_click--;} 
      ViewState["btn_click"] = btn_click;

 }

Check the differences between Session and ViewState below are the References

Session Vs ViewState

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.