• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 421
  • Last Modified:

Newbie question about variables

Very noob question sorry.  
I have this in my page_load object, however every time the page refreshes my variables are blank.   How do I get session variables set up for the life of the applications session?

public partial class WebForm1 : System.Web.UI.Page
    {
        public string currentTote;
        public string currentDestination;
        public LabConnectTableAdapters.entriesTableAdapter ta = new        xTableAdapters.entriesTableAdapter();
        public x.entriesDataTable dt_temp = new x.entriesDataTable();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (currentTote == "")
            {
                viewToteData.Visible = false;

            }
            else 
            {
                var test = ta.GetToteDestination(currentTote);
                
                currentDestination= Convert.ToString(ta.GetToteDestination(currentTote))
                x.entriesDataTable dt = new x.entriesDataTable();
                ta.FillByEntryID(dt_temp, currentTote);
            }


        }

Open in new window

0
Dan Flood
Asked:
Dan Flood
2 Solutions
 
Tom BeckCommented:
else
            {
               if(!IsPostBack)
               {
                var test = ta.GetToteDestination(currentTote);
               
                currentDestination= Convert.ToString(ta.GetToteDestination(currentTote))
                x.entriesDataTable dt = new x.entriesDataTable();
                ta.FillByEntryID(dt_temp, currentTote);
                }
            }

In other words, load the dt only on initial load of the page and not on postbacks.
0
 
mr_nadgerCommented:
that'll prevent the variables refreshing on a postback, but the OP mentions "How do I get session variables set up for the life of the applications session?"
Have a look at http://msdn.microsoft.com/en-us/library/ms178581(v=vs.100).aspx

If you're trying to keep a variable available across pages for a user's session on the application, these session variables will do that - if you're trying to keep the variable for that page's lifecycle, you could use a session variable or a hidden control on the page (you just need to remember to clear them up :) ).
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now