0

Ok so my problem is everytime I click my button it re-populates my dropdownlist of my entries, but it also keeps the entries from my previous page_load therefore i get double entries within my dropdownlist. How do I solve this problem as I cannot seem to find where my problem is:

Code can be seen below:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace PartD
{
    public partial class About : Page
    {
        List<Beverage> account;
        string[] arr;
        protected void Page_Load(object sender, EventArgs e)
        {


            arr = (string[])Session["arr"];
            for (int i = 0; i < arr.Length; i++)
            {
                DropDownList1.Items.Add(arr[i].ToString());
            }

            if (Session["bubble"] == null)
            {
                account = new List<Beverage>();
                Session["bubble"] = account;
            }
            else
            {
                account = (List<Beverage>)Session["bubble"];
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            ListBox1.Items.Clear(); // clears ListBox1
            Beverage f = new Beverage(DropDownList1.Text, int.Parse(TextBox1.Text),
       int.Parse(TextBox2.Text), CheckBox1.Checked); // gets text from textbox5, then converts textbox2,textbox3 string to int and will execute of checkbox1 is checked
            account = (List<Beverage>)Session["bubble"]; // displays results in listbox1
            account.Add(f); // adds items to listbox
            foreach (var item in account)
            {
                ListBox1.Items.Add(item.getFruitInfo());
            }
        }
    }
}
1
Contributor
1
Reply
2
Views
5 Years
Discussion Span
Last Post by TIM_M_91
0

Fixed my problem by using the following:

if (!Page.IsPostBack)
            {
                arr = (string[])Session["arr"];
                for (int i = 0; i < arr.Length; i++)
                {
                    DropDownList1.Items.Add(arr[i].ToString());
                }

            }
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.