我有一个从MySQL查询结果创建的按钮。它看起来像这样:
1产品名称|添加到购物篮|
当我按下"添加到购物篮"按钮,产品出现在右侧的一个表(篮子)中。像这样:
1产品名称|删除|
但由于某种原因,remove
按钮点击事件会从篮子中删除所有项目。当我再次按add to basket
时,所有项目都会再次出现在购物篮中。
这是全班。谢谢你的帮助
public partial class neworder : System.Web.UI.Page
{
Basket basket;
Table BasketTable;
private int countRows = 0;
private string distance;
private string categories;
ArrayList categoryList = new ArrayList();
ArrayList productList = new ArrayList();
private string products = "";
public string Products
{
get { return products; }
set { products = value; }
}
public string Categories
{
get { return categories; }
set { categories = value; }
}
public string Distance
{
get { return distance; }
set { distance = value; }
}
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Page_Init(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
basket = new Basket();
Session["basket"] = basket;
}
populateCategories();
populateProducts();
loadBasket(); // if this is comment then i items are added fine
//to basket but cant be remove, if its not commented then i can add
//only 2 items in basket
}
private void populateCategories()
{
using (MySqlConnection cn = new MySqlConnection(Static.MysqlServer1))
{
cn.Open();
String strSQL = "SELECT category_name FROM categories;";
MySqlCommand cmdPLT = new MySqlCommand(strSQL, cn);
MySqlDataReader myReader;
myReader = cmdPLT.ExecuteReader();
while (myReader.Read())
{
string cat = myReader.GetString(0);
categoryList.Add(cat);
categories += "<a href=\"#"+cat+"\">" + cat + "</a><br />";
}
}
}
private void populateProducts()
{
Panel1.Controls.Clear();
string tem = "";
foreach (string s in categoryList)
{
Table tableTitle = new Table();
TableRow row1 = new TableRow();
tableTitle.ID = s + "Title";
tableTitle.CssClass = "rest2";
Panel1.Controls.Add(tableTitle);
TableCell cell2 = new TableCell();
Label label2 = new Label();
label2.Text = "<a name="+s+">"+s+"</a>";
label2.ID = "TitleText_" + s;
cell2.Controls.Add(label2);
row1.Cells.Add(cell2);
tableTitle.Controls.Add(row1);
Table table = new Table();
table.ID = s;
table.CssClass = "rest";
Panel1.Controls.Add(table);
using (MySqlConnection cn = new MySqlConnection(Static.MysqlServer1))
{
string name = "";
string size = "";
int id = -1;
double price = 0.0;
cn.Open();
String strSQL = "SELECT product_id, product_name, product_price, product_size FROM products join categories on categories.CATEGORY_ID = products.CATEGORY_ID where category_name ='" + s + "' order by product_order asc;";
MySqlCommand cmdPLT = new MySqlCommand(strSQL, cn);
MySqlDataReader myReader;
myReader = cmdPLT.ExecuteReader();
while (myReader.Read())
{
name = (string)myReader["product_name"];
id = (int)myReader["product_id"];
size = (string)myReader["product_size"];
price = (double)myReader["product_price"];
productList.Add(new Products(id, name, size, price));
GenerateRow(s, name, size, price, table, id);
}
if (!tem.Equals(""))
{
tem += "</table>";
}
}
products += tem;
tem = "";
}
}
private void GenerateRow(string category, string name, string size, double price, Table table,int id )
{
TableRow row1 = new TableRow();
//ROW 1 NAME
TableCell cell1 = new TableCell();
Label label1 = new Label();
label1.Text = name;
cell1.Style.Add("width", "300px !important");
label1.ID = "NameText_" + category+id;
cell1.Controls.Add(label1);
row1.Cells.Add(cell1);
//ROW 2 Size
TableCell cell2 = new TableCell();
Label label2 = new Label();
cell2.Style.Add("width", "100px !important");
label2.Text = size;
label2.ID = "SizeText_" +category+ id;
cell2.Controls.Add(label2);
row1.Cells.Add(cell2);
//ROW 1 PRICE
TableCell cell3 = new TableCell();
Label label3 = new Label();
cell3.Style.Add("width", "60px !important");
label3.Text = "" + price;
label3.ID = "Price_" + category + id;
cell3.Controls.Add(label3);
row1.Cells.Add(cell3);
//ROW 1 BUTTON
TableCell cell4 = new TableCell();
cell4.Style.Add("height", "25px !important");
Button button = new Button();
button.Text = "+";
button.Click += button_Click;
button.ID = ""+id;
cell4.Controls.Add(button);
row1.Cells.Add(cell4);
table.Controls.Add(row1);
}
private void loadBasket()
{
Basket temp = (Basket)Session["basket"];
BasketTable = new Table();
BasketTable.ID = "basket";
BasketTable.CssClass = "rest";
PanelBasket.Controls.Clear();
PanelBasket.Controls.Add(BasketTable);
foreach (Products p in temp.ProductList)
{
TableRow row1 = new TableRow();
//ROW 1 NAME
TableCell cell1 = new TableCell();
Label label1 = new Label();
label1.Text = p.Name;
cell1.Style.Add("width", "100px !important");
label1.ID = p.Name;
cell1.Controls.Add(label1);
row1.Cells.Add(cell1);
//ROW 2 Size
TableCell cell2 = new TableCell();
Label label2 = new Label();
cell2.Style.Add("width", "40px !important");
label2.Text = p.Size;
label2.ID = p.Size;
cell2.Controls.Add(label2);
row1.Cells.Add(cell2);
//ROW 1 PRICE
TableCell cell3 = new TableCell();
Label label3 = new Label();
cell3.Style.Add("width", "60px !important");
label3.Text = p.Price+"";
label3.ID = p.Price + "";
cell3.Controls.Add(label3);
row1.Cells.Add(cell3);
//ROW 1 BUTTON
TableCell cell4 = new TableCell();
cell4.Style.Add("height", "25px !important");
Button remove = new Button();
remove.Text = "-";
remove.Click += new EventHandler(button99_Click);
remove.ID = "remove" + p.Id;
cell4.Controls.Add(remove);
row1.Cells.Add(cell4);
BasketTable.Controls.Add(row1);
}
}
private void button_Click(object sender, EventArgs e)
{
Button button = (Button)sender;
string buttonId = button.ID;
foreach (Products p in productList)
{
if (p.Id == Convert.ToInt32(buttonId))
{
Basket temp = (Basket)Session["basket"];
temp.addPruduct(p);
Session["basket"] = temp;
loadBasket();
Label1.Text = temp.ProductList.Count + "";
}
}
}
protected void remove_Click(object sender, EventArgs e)
{
Label1.Text = "test";
}
}