Re: ASP global variable

K

Kevin Spencer

Hi Joseph,

Every time your page posts back it reloads the Page class. Take a look at
the code you have just under your Class declaration:
Public Class WebForm1 Inherits System.Web.UI.Page
Dim list as new ListItemCollection()

The New constructor creates a new empty instance of a class. That is why
your ListItemCollection is empty. You made it empty.

Of course, unless you re-create the ListItemCollection with every PostBack,
it won't exist. Leaving off the "New" won't help, as the variable will be
uninitialized. So, the first thing to do would be to remove the "New" from
your variable declaration. You need to figure out where and how to
initialize it. It needs to be declared (Dimmed). But how you initialize it,
persist it, and add items to it is where your problem is.

Your question Title indicates that you have some understanding of the
problem. But be careful of the word "global." That is an word which can mean
any of a number of things. The term "global" refers to the "scope" of a
variable. In an ASP.Net application, variables can have a wide variety of
scopes, and a fairly large number of "global" scopes. A variable can be
global to a page, to a class in a page, to an Assembly, to a User Session,
or to the entire web application. What we need to do is identify the proper
scope of your variable, and find the appropriate mechanism for making it
such.

As this variable is page-specific, and you have declared it as a private
field in the Page class, it is already global to the Page. But what you need
to do is either rebuild it with every PostBack, or persist it somewhere.
There are several possibilites for this: ViewState and Session spring to
mind. ViewState is maintained by passing the data back and forth between
client and server.On the server it represents a "StateBag" type of
Collection, similar to a Dictionary object. If you want to store your
Collection in ViewState, the easiest thing for you to do with it would be to
store it in an ArrayList or HashTable, which can easily and automatically be
serialized into ViewState.

You can also store it in Session State. However, be careful about using
Session State. It can time out and disappear after a (default) 20-minute
interval of inactivity on the part of the client. In addition, Session State
is global to all pages of a user's Session, and you may run into
complications with other data in Session State, as well as memory issues
(there is a separate Session memory space for every client on the server, so
the amount that you store in Session is multiplied by the number of
concurrent User Sessions).

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
http://www.takempis.com
Neither a follower nor a lender be.
 
J

Joseph George

Thanks Kevin. ViewState works fine!
Joseph

Kevin Spencer said:
Hi Joseph,

Every time your page posts back it reloads the Page class. Take a look at
the code you have just under your Class declaration:


The New constructor creates a new empty instance of a class. That is why
your ListItemCollection is empty. You made it empty.

Of course, unless you re-create the ListItemCollection with every PostBack,
it won't exist. Leaving off the "New" won't help, as the variable will be
uninitialized. So, the first thing to do would be to remove the "New" from
your variable declaration. You need to figure out where and how to
initialize it. It needs to be declared (Dimmed). But how you initialize it,
persist it, and add items to it is where your problem is.

Your question Title indicates that you have some understanding of the
problem. But be careful of the word "global." That is an word which can mean
any of a number of things. The term "global" refers to the "scope" of a
variable. In an ASP.Net application, variables can have a wide variety of
scopes, and a fairly large number of "global" scopes. A variable can be
global to a page, to a class in a page, to an Assembly, to a User Session,
or to the entire web application. What we need to do is identify the proper
scope of your variable, and find the appropriate mechanism for making it
such.

As this variable is page-specific, and you have declared it as a private
field in the Page class, it is already global to the Page. But what you need
to do is either rebuild it with every PostBack, or persist it somewhere.
There are several possibilites for this: ViewState and Session spring to
mind. ViewState is maintained by passing the data back and forth between
client and server.On the server it represents a "StateBag" type of
Collection, similar to a Dictionary object. If you want to store your
Collection in ViewState, the easiest thing for you to do with it would be to
store it in an ArrayList or HashTable, which can easily and automatically be
serialized into ViewState.

You can also store it in Session State. However, be careful about using
Session State. It can time out and disappear after a (default) 20-minute
interval of inactivity on the part of the client. In addition, Session State
is global to all pages of a user's Session, and you may run into
complications with other data in Session State, as well as memory issues
(there is a separate Session memory space for every client on the server, so
the amount that you store in Session is multiplied by the number of
concurrent User Sessions).

--
HTH,

Kevin Spencer
Microsoft MVP
.Net Developer
http://www.takempis.com
Neither a follower nor a lender be.
 
J

Joseph George

Here is a small example: The WebForm has a TexBox1, a ListBox1 and
AddButton.



Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load


If IsNothing(ViewState("list")) Then

ViewState("list") = myArray

End If

End Sub

Private Sub AddButton_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles AddButton.Click

myArray = ViewState("list")

myArray.Add(TextBox1.Text)

ViewState("myAL") = myArray

ListBox1.DataSource = myArray

ListBox1.DataBind()

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top