Distionary in Webpage help

  • Thread starter Thread starter Dale A Siar
  • Start date Start date
D

Dale A Siar

I am trying to use a stringDictionary on a web page that gets populated by
clicking buttons on a page.

Each time a button is clicked the Dictionary is cleared and the previous
enteries entered into the dictionary are gone.

Excuse my ignorance if there is a simple solution as I am just, 1st time,
trying to convert a console app to a web page.

Any help will be appreciated
Thanks.
 
Not sure is the code would help - it is very basic, just create the
dictionary and add 1 entry. I think that this has to do with it being on a
webpage as the code is fine in a console app. The code is entering the If to
add the entry to the dictionary - but possibly when the page refreshes the
dictionary is reinitialized.

public StringDictionary myCart = new StringDictionary();


if (e.CommandName == "btnBuy")
{
// Convert the row index stored in the CommandArgument
// property to an Integer.
int index = Convert.ToInt32(e.CommandArgument);

// Get the last name of the selected author from the
appropriate
// cell in the GridView control.
GridViewRow selectedRow = GridView1.Rows[index];
TableCell ApplicationName = selectedRow.Cells[0];
string Application = ApplicationName.Text;

TextBox1.Text = "You selected " + Application + ".";

if (!myCart.ContainsKey(Application))
{
Response.Write("Adding App"+"-"+Application);
myCart.Add(Application, Application);
}
 
The problem is that you seem to have forgotten that a web site is not like a
windows form. The page object is destroyed when the processing is finished
and a post back for a button click is processed by creating a new page and
running the code in it.
You are not saving you dictionary anywhere. You need to investigate the
state management options in ASP.NET - Session State , ViewState , Application
State etc
Try reading something like this:
http://www.csharphelp.com/archives/archive207.html
 
Actually, the MSDN article is good with a good selection of further reading
links at the bottom:
http://msdn.microsoft.com/en-us/library/75x4ha6s.aspx

--
Ciaran O''Donnell
http://wannabedeveloper.spaces.live.com


Ciaran O''Donnell said:
The problem is that you seem to have forgotten that a web site is not like a
windows form. The page object is destroyed when the processing is finished
and a post back for a button click is processed by creating a new page and
running the code in it.
You are not saving you dictionary anywhere. You need to investigate the
state management options in ASP.NET - Session State , ViewState , Application
State etc
Try reading something like this:
http://www.csharphelp.com/archives/archive207.html

--
Ciaran O''Donnell
http://wannabedeveloper.spaces.live.com


Dale A Siar said:
Not sure is the code would help - it is very basic, just create the
dictionary and add 1 entry. I think that this has to do with it being on a
webpage as the code is fine in a console app. The code is entering the If to
add the entry to the dictionary - but possibly when the page refreshes the
dictionary is reinitialized.

public StringDictionary myCart = new StringDictionary();


if (e.CommandName == "btnBuy")
{
// Convert the row index stored in the CommandArgument
// property to an Integer.
int index = Convert.ToInt32(e.CommandArgument);

// Get the last name of the selected author from the
appropriate
// cell in the GridView control.
GridViewRow selectedRow = GridView1.Rows[index];
TableCell ApplicationName = selectedRow.Cells[0];
string Application = ApplicationName.Text;

TextBox1.Text = "You selected " + Application + ".";

if (!myCart.ContainsKey(Application))
{
Response.Write("Adding App"+"-"+Application);
myCart.Add(Application, Application);
}
 
Dale said:
I am trying to use a stringDictionary on a web page that gets populated by
clicking buttons on a page.

Each time a button is clicked the Dictionary is cleared and the previous
enteries entered into the dictionary are gone.

Excuse my ignorance if there is a simple solution as I am just, 1st time,
trying to convert a console app to a web page.

Any help will be appreciated
Thanks.

To transition from a console application to a web application, you need
to understand that the environment is completely different. You are
moving from a stateful single-user client application to a stateless
multi-user client-server application.

For every reqest to the server, a new Page object is created, that
contains the code that creates the page to return to the browser. It's
pretty much like running a program for every response that the server
creates.

When the response has been sent to the browser, the Page object is
released, and all local variables in the page is lost. Thats what
happening to your dictionary.

The easiest way for you to get your code to work for now, is to simply
recreate the dictionary for every request. Further on you might want to
look into how you store the dictionary in a session variable or an
application variable, so that you don't have to recreate it for every
request.
 
Thanks for the advise, I was pretty sure that this was the issue.

Ciaran O''Donnell said:
Actually, the MSDN article is good with a good selection of further reading
links at the bottom:
http://msdn.microsoft.com/en-us/library/75x4ha6s.aspx

--
Ciaran O''Donnell
http://wannabedeveloper.spaces.live.com


Ciaran O''Donnell said:
The problem is that you seem to have forgotten that a web site is not like a
windows form. The page object is destroyed when the processing is finished
and a post back for a button click is processed by creating a new page and
running the code in it.
You are not saving you dictionary anywhere. You need to investigate the
state management options in ASP.NET - Session State , ViewState , Application
State etc
Try reading something like this:
http://www.csharphelp.com/archives/archive207.html

--
Ciaran O''Donnell
http://wannabedeveloper.spaces.live.com


Dale A Siar said:
Not sure is the code would help - it is very basic, just create the
dictionary and add 1 entry. I think that this has to do with it being on a
webpage as the code is fine in a console app. The code is entering the If to
add the entry to the dictionary - but possibly when the page refreshes the
dictionary is reinitialized.

public StringDictionary myCart = new StringDictionary();


if (e.CommandName == "btnBuy")
{
// Convert the row index stored in the CommandArgument
// property to an Integer.
int index = Convert.ToInt32(e.CommandArgument);

// Get the last name of the selected author from the
appropriate
// cell in the GridView control.
GridViewRow selectedRow = GridView1.Rows[index];
TableCell ApplicationName = selectedRow.Cells[0];
string Application = ApplicationName.Text;

TextBox1.Text = "You selected " + Application + ".";

if (!myCart.ContainsKey(Application))
{
Response.Write("Adding App"+"-"+Application);
myCart.Add(Application, Application);
}
 

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

Back
Top