Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Using ArrayList in Web Design

Using ArrayList in Web Design

Scheduled Pinned Locked Moved ASP.NET
csharpdatabasedesignhelp
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    ChrisDM
    wrote on last edited by
    #1

    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 definition protected 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 screen private 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

    C N P 3 Replies Last reply
    0
    • C ChrisDM

      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 definition protected 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 screen private 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

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      My 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

      1 Reply Last reply
      0
      • C ChrisDM

        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 definition protected 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 screen private 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

        N Offline
        N Offline
        Not Active
        wrote on last edited by
        #3

        Cache 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.

        1 Reply Last reply
        0
        • C ChrisDM

          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 definition protected 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 screen private 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

          P Offline
          P Offline
          Paul Watson
          wrote on last edited by
          #4

          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 Africa

          Ray Cassick wrote:
          Well I am not female, not gay and I am not Paul Watson

          C 1 Reply Last reply
          0
          • P 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 Africa

            Ray Cassick wrote:
            Well I am not female, not gay and I am not Paul Watson

            C Offline
            C Offline
            ChrisDM
            wrote on last edited by
            #5

            Paul, Thank you for the information, I will change the code to reflect this information and see what happens. At present I am playing around but once I get it working well I will indeed create an article for it. Regards Chris

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups