1

I am developping an asp.net application. I have a page called "home.aspx" which contains 2 buttons called "button1" and "button2". Each button has a onclick method, respectively called "Button1_click" and "Button2_click". My goal is to share a common variable between these 2 methods, so basically I tried to add a property to the class associated to my page like this :

public partial class Home : Syste.Web.UI.Page
{
    private int myproperty=0;

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

    protected void Button2_Click(object sender, EventArgs e)
    {
        myproperty++;  
    }
}

With this code, I was thinking that the "private int myproperty=0" would be called only the first time I load and get to the page. And then when I would be on the page, if I click on button1 or button2 the myproperty would just be incrementated by one. But when I am debugging, I see that each time I click on button1 or button2 the "private int myproperty=0" is called again which I don't understand. Is anyone has a solution?

Thanks in advance for your help.

1
  • asp.net classes like Page only exist for the lifetime of the request, each request creates a new instance. If you want to store information between requests you will have to persist it somewhere, if it is just in memory then Session is often used, but bear in mind each client will have their own session. Commented Oct 29, 2014 at 11:11

6 Answers 6

2

In ASP.NET if you want use global variables you'll have to use the Session object(if the variable is different for every single user, like count how many time a user click a Button) or Application object(If the variable is common to all the users like count how much user visit your site). Example:

//Set variable
Session["myVariable"] = value
//Get variable
var myVariable = Session["myVariable"]

The syntax is equal for Application just replace Session with Application

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

Comments

0

You can use Session as below

private int Count
{
  get 
   {
     if (Session["Count"] == null)
     {         Session["Count"] = 0; return 0;      }
     else 
        return (int)Session["Count"] ; 
   }
   set
   {
      Session["Count"] = value;
   }
} 

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

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

protected void Button2_Click(object sender, EventArgs e)
{
     Count++; 
}

Comments

0

as Ben Robinson said you have to persist that value somewhere. An easy way is to use Session:

  1. Create a "const string" variable in the class to hold the session index like:

    const string MY_PROPERTY = "MyProperty";

  2. then whenever you want to store or read the value use:

    //to store Session[Home.MY_PROPERTY] = int.parse(Session[Home.MY_PROPERTY])+1; //to read int myVariable = int.parse(Session[Home.MY_PROPERTY])

you can also create a property in your class like:

const string MY_PROPERTY = "MyProperty";
public int MyProperty
{
    get
    {
        if (Session[Home.MY_PROPERTY] == null)
        {
            return 0;
        }
        else
        {
            int iValue = 0;
            if (int.TryParse(Session[Home.MY_PROPERTY].ToString(), out iValue))
                return iValue;
            else
                return 0;
        }
    }
    set
    {
        Session[Home.MY_PROPERTY] = value;
    }
}

Comments

0

Make your variable static

Like this

    public partial class Home : Syste.Web.UI.Page
   {
    private Static int myproperty;

    protected void Button1_Click(object sender, EventArgs e)
    {
        //You can access myproperty here (same copy)
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
         //You can access myproperty here (same copy)
    }
}

4 Comments

What? Then what's the need of Session in ASP.NET?
session object is for a user session level and application object is for all user or whole application and this is for the particular page level. It will share the same copy of data.
NO, that's a wrong way of doing. You should be using any state maintenance mechanism rather storing it in static variable. Consider reading more about this.
my answer was according to the question. I know it is not the good way of storing data.Thanks
0

Only the postback check, before the increment, can resolve the problem. Add following code before increment.

if(!IsPostBack)

Comments

0

How you want to share the value of the variable myproperty?

-If you want to reuse the value of myproperty per window you need to store the value in the vewstate: http://msdn.microsoft.com/en-us/library/ms227551%28v=vs.85%29.aspx

Another way is to use a asp.net webcontrol hiddenfiled to store the value.

-If you want to reuse the value for session, you can store the value in the session (Session["myproperty"])

-If you want to share the value with all the users you can use the application: http://msdn.microsoft.com/en-us/library/94xkskdf%28v=vs.100%29.aspx

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.