Using ArrayList in Web Design
-
I have modified the Web Site which is created when following the "Walkthrough: Creating a Web Application Using Visual C# or Visual Basic", using Visual C#. I have added a new class called MyBasket which has the following structure
using System; using System.Collections; namespace MyWebForm { /// /// /// public class MyBasket : IEnumerable { public MyBasket() { } public IEnumerator GetEnumerator() { return items.GetEnumerator(); } public bool AddItem(string titleId, string title, double price) { // Create Item MyItem myItem = new MyItem(); myItem.TitleId = titleId; myItem.Title = title; myItem.Price = price; // Add Item items.Add(myItem); return true; } public int ItemCount { get { return items.Count; } } public double TotalPrice { get { double total = 0; foreach (MyItem item in items) { total += item.Price; } return total; } } private ArrayList items = new ArrayList(); } }
I have declared this as follows within the WebForm1.asp.cs within the class definitionprotected MyBasket myBasket = new MyBasket();
I have created a button on the form which adds the current selected item when clicked. The problem I am experiencing is that the MyBasket class does not keep the information, therefore whenever I click the Add button there is only every one item in the ArrayList. I don't understand why this is not working and would value some advice. The following code is used to add the items and refresh the screenprivate void buttonAdd_Click(object sender, System.EventArgs e) { // Add Selected Item to basket int index = DataGrid1.SelectedIndex; // myBasket.AddItem(Label1.Text, Label2.Text, Convert.ToDouble(Label8.Text)); // Refresh Basket refreshBasket(); } public void refreshBasket() { // try { Label7.Text = myBasket.ItemCount.ToString(); Label9.Text = string.Format("{0:C}", myBasket.TotalPrice); } catch { Label7.Text = ""; Label9.Text = ""; } }
Chris -
I have modified the Web Site which is created when following the "Walkthrough: Creating a Web Application Using Visual C# or Visual Basic", using Visual C#. I have added a new class called MyBasket which has the following structure
using System; using System.Collections; namespace MyWebForm { /// /// /// public class MyBasket : IEnumerable { public MyBasket() { } public IEnumerator GetEnumerator() { return items.GetEnumerator(); } public bool AddItem(string titleId, string title, double price) { // Create Item MyItem myItem = new MyItem(); myItem.TitleId = titleId; myItem.Title = title; myItem.Price = price; // Add Item items.Add(myItem); return true; } public int ItemCount { get { return items.Count; } } public double TotalPrice { get { double total = 0; foreach (MyItem item in items) { total += item.Price; } return total; } } private ArrayList items = new ArrayList(); } }
I have declared this as follows within the WebForm1.asp.cs within the class definitionprotected MyBasket myBasket = new MyBasket();
I have created a button on the form which adds the current selected item when clicked. The problem I am experiencing is that the MyBasket class does not keep the information, therefore whenever I click the Add button there is only every one item in the ArrayList. I don't understand why this is not working and would value some advice. The following code is used to add the items and refresh the screenprivate void buttonAdd_Click(object sender, System.EventArgs e) { // Add Selected Item to basket int index = DataGrid1.SelectedIndex; // myBasket.AddItem(Label1.Text, Label2.Text, Convert.ToDouble(Label8.Text)); // Refresh Basket refreshBasket(); } public void refreshBasket() { // try { Label7.Text = myBasket.ItemCount.ToString(); Label9.Text = string.Format("{0:C}", myBasket.TotalPrice); } catch { Label7.Text = ""; Label9.Text = ""; } }
ChrisMy guess is you're doing a trip to the server to find out what was entered, but you're not sending back the info about what was already in there, so each time you start from scratch and add one item. The ASP way to do this is to have a lot of hidden fields with your values in them, not sure what the ASP.NET way is. Christian No offense, but I don't really want to encourage the creation of another VB developer. - Larry Antram 22 Oct 2002 Hey, at least Logo had, at it's inception, a mechanical turtle. VB has always lacked even that... - Shog9 04-09-2002 During last 10 years, with invention of VB and similar programming environments, every ill-educated moron became able to develop software. - Alex E. - 12-Sept-2002
-
I have modified the Web Site which is created when following the "Walkthrough: Creating a Web Application Using Visual C# or Visual Basic", using Visual C#. I have added a new class called MyBasket which has the following structure
using System; using System.Collections; namespace MyWebForm { /// /// /// public class MyBasket : IEnumerable { public MyBasket() { } public IEnumerator GetEnumerator() { return items.GetEnumerator(); } public bool AddItem(string titleId, string title, double price) { // Create Item MyItem myItem = new MyItem(); myItem.TitleId = titleId; myItem.Title = title; myItem.Price = price; // Add Item items.Add(myItem); return true; } public int ItemCount { get { return items.Count; } } public double TotalPrice { get { double total = 0; foreach (MyItem item in items) { total += item.Price; } return total; } } private ArrayList items = new ArrayList(); } }
I have declared this as follows within the WebForm1.asp.cs within the class definitionprotected MyBasket myBasket = new MyBasket();
I have created a button on the form which adds the current selected item when clicked. The problem I am experiencing is that the MyBasket class does not keep the information, therefore whenever I click the Add button there is only every one item in the ArrayList. I don't understand why this is not working and would value some advice. The following code is used to add the items and refresh the screenprivate void buttonAdd_Click(object sender, System.EventArgs e) { // Add Selected Item to basket int index = DataGrid1.SelectedIndex; // myBasket.AddItem(Label1.Text, Label2.Text, Convert.ToDouble(Label8.Text)); // Refresh Basket refreshBasket(); } public void refreshBasket() { // try { Label7.Text = myBasket.ItemCount.ToString(); Label9.Text = string.Format("{0:C}", myBasket.TotalPrice); } catch { Label7.Text = ""; Label9.Text = ""; } }
ChrisCache your cart object in either a session variable or cache. Otherwise your it get created new each time the page is created, just you are experiencing.
-
I have modified the Web Site which is created when following the "Walkthrough: Creating a Web Application Using Visual C# or Visual Basic", using Visual C#. I have added a new class called MyBasket which has the following structure
using System; using System.Collections; namespace MyWebForm { /// /// /// public class MyBasket : IEnumerable { public MyBasket() { } public IEnumerator GetEnumerator() { return items.GetEnumerator(); } public bool AddItem(string titleId, string title, double price) { // Create Item MyItem myItem = new MyItem(); myItem.TitleId = titleId; myItem.Title = title; myItem.Price = price; // Add Item items.Add(myItem); return true; } public int ItemCount { get { return items.Count; } } public double TotalPrice { get { double total = 0; foreach (MyItem item in items) { total += item.Price; } return total; } } private ArrayList items = new ArrayList(); } }
I have declared this as follows within the WebForm1.asp.cs within the class definitionprotected MyBasket myBasket = new MyBasket();
I have created a button on the form which adds the current selected item when clicked. The problem I am experiencing is that the MyBasket class does not keep the information, therefore whenever I click the Add button there is only every one item in the ArrayList. I don't understand why this is not working and would value some advice. The following code is used to add the items and refresh the screenprivate void buttonAdd_Click(object sender, System.EventArgs e) { // Add Selected Item to basket int index = DataGrid1.SelectedIndex; // myBasket.AddItem(Label1.Text, Label2.Text, Convert.ToDouble(Label8.Text)); // Refresh Basket refreshBasket(); } public void refreshBasket() { // try { Label7.Text = myBasket.ItemCount.ToString(); Label9.Text = string.Format("{0:C}", myBasket.TotalPrice); } catch { Label7.Text = ""; Label9.Text = ""; } }
ChrisAs the others mentioned you need some way to persist your MyBasket collection between server round trips. One way is to use the object caching, or you can use Session or Application variables. Another of course is to persist it to a database, though naturally the performance of that won't be as good (though if you use Session vars then you need to remember you will have trouble when your website moves onto a server farm.) Using Session and Application Objects in ASP .NET[^] You can even persist your collection by serialising it and adding it to the viewstate of the page. This article explains how to do it for a DataSet[^], would not be too hard to take that and do it for your object. Google has a nice list of articles explaining how to persist objects[^] Good luck and write an article about it once you have done it :)
Paul Watson
Bluegrass
Cape Town, South AfricaRay Cassick wrote:
Well I am not female, not gay and I am not Paul Watson -
As the others mentioned you need some way to persist your MyBasket collection between server round trips. One way is to use the object caching, or you can use Session or Application variables. Another of course is to persist it to a database, though naturally the performance of that won't be as good (though if you use Session vars then you need to remember you will have trouble when your website moves onto a server farm.) Using Session and Application Objects in ASP .NET[^] You can even persist your collection by serialising it and adding it to the viewstate of the page. This article explains how to do it for a DataSet[^], would not be too hard to take that and do it for your object. Google has a nice list of articles explaining how to persist objects[^] Good luck and write an article about it once you have done it :)
Paul Watson
Bluegrass
Cape Town, South AfricaRay Cassick wrote:
Well I am not female, not gay and I am not Paul Watson