By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,899 Members | 1,316 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,899 IT Pros & Developers. It's quick & easy.

Distionary in Webpage help

P: n/a
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.
Jul 14 '08 #1
Share this Question
Share on Google+
6 Replies


P: n/a
=== original message ===
from: Dale A Siar
date: 14.07.2008 16:20
[description but no code]
A little code might be helpful to be helpful ;)
Jul 14 '08 #2

P: n/a
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);
}
Jul 14 '08 #3

P: n/a
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" wrote:
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);
}
Jul 14 '08 #4

P: n/a
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" wrote:
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" wrote:
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);
}
Jul 14 '08 #5

P: n/a
Dale A Siar wrote:
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.

--
Göran Andersson
_____
http://www.guffa.com
Jul 14 '08 #6

P: n/a
Thanks for the advise, I was pretty sure that this was the issue.

"Ciaran O''Donnell" wrote:
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" wrote:
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" wrote:
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);
}
Jul 14 '08 #7

This discussion thread is closed

Replies have been disabled for this discussion.