我正在开发购物车类的东西。为此,在选择产品时,它们会被添加到临时表中,依此类推。然后会打开一个弹出窗口,根据页面加载事件中临时表中的行数加载动态标签,文本框和链接按钮。我想允许用户更改文本框字段,即产品的数量,并使用动态创建的链接按钮立即在数据库中更新。
我尝试使用Viewstate,但我无法根据需要进行操作。此外,我在堆栈溢出和其他博客上阅读了许多其他文章和帖子,但无法解决。
这是我的代码: -
protected void Page_Load(object sender, EventArgs e)
{
Session["date"] = DateTime.Now.ToShortDateString();
productListing();
LabelCost.Text = total.ToString("F");
}
public void productListing()
{
//...................................Product Listing...............................
con2.Close();
con2.Open();
SqlDataAdapter adp12 = new SqlDataAdapter("select * from db_temporary_cart where(IP_address='" + Request.QueryString["ip"].ToString() + "' AND date='" + Session["date"] + "') ORDER BY counter DESC", con2);
DataSet ds12 = new DataSet();
adp12.Fill(ds12, "db_temporary_cart");
int count12 = ds12.Tables[0].Rows.Count;
if (count12 > 0)
{
totalProductRpwsincategory = count12;
//................storing products id of the category.....................
arr_product_id = new string[count12];
arr_product_name = new string[count12];
arr_product_qty = new int[count12];
arr_product_price = new double[count12];
arr_product_subtotal = new double[count12];
for (int i = 0; i < count12; i++)
{
arr_product_id[i] = ds12.Tables[0].Rows[i]["product_id"].ToString();
arr_product_qty[i] = Convert.ToInt32(ds12.Tables[0].Rows[i]["quantity"]);
}
con2.Close();
for (int i = 0; i < arr_product_id.Length; i++)
{
con2.Close();
con3.Close();
con3.Open();
SqlDataAdapter adp123 = new SqlDataAdapter("select * from db_product_management where(product_id='" + arr_product_id[i].ToString() + "' AND product_stock_availability='" + "In-Stock" + "')", con3);
DataSet ds123 = new DataSet();
adp123.Fill(ds123, "db_product_management");
int count123 = ds123.Tables[0].Rows.Count;
if (count123 > 0)
{
arr_product_name[i] = ds123.Tables[0].Rows[0]["product_name"].ToString();
arr_product_price[i] = Convert.ToDouble(ds123.Tables[0].Rows[0]["product_price"]);
arr_product_subtotal[i] = Convert.ToDouble((arr_product_price[i])*(arr_product_qty[i]));
total = total + arr_product_subtotal[i];
con3.Close();
}
con3.Close();
}
//................storing products id of the category.....................
Table table = new Table();
var collection = new List<string>();
for (int i = 0; i < totalProductRpwsincategory; i++)
{
TableRow row = new TableRow();
row.CssClass = "dynamicMain";
System.Web.UI.HtmlControls.HtmlGenericControl createDivMain = new System.Web.UI.HtmlControls.HtmlGenericControl("DIV");
createDivMain.Attributes["class"] = "dynamicMain";
ImageButton imgLink = new ImageButton();
imgLink.Attributes.Add("width", "150px");
imgLink.Attributes.Add("height", "104px");
imgLink.Attributes["class"] = "dynamicIMG";
imgLink.CommandArgument = arr_product_id[i];
imgLink.CommandName = arr_product_name[i];
imgLink.Command += new CommandEventHandler(this.OnDynamicLinkButtonClick);
System.Web.UI.HtmlControls.HtmlGenericControl createDiv1 = new System.Web.UI.HtmlControls.HtmlGenericControl("DIV");
createDiv1.Attributes["class"] = "sub-category-productsDisplayBlockText1_Dynamic";
Label linkProduct = new Label();
linkProduct.Text = arr_product_name[i];
LinkButton linkSaveQty = new LinkButton();
linkSaveQty.Text = arr_product_name[i];
linkSaveQty.Font.Underline = false;
linkSaveQty.ID = i.ToString();
linkSaveQty.Text = "Save";
linkSaveQty.CommandArgument = arr_product_id[i];
linkSaveQty.Attributes["class"] = "dynamicSaveQty";
linkSaveQty.Command += new CommandEventHandler(this.OnDynamicLinkButtonClickSave);
//linkSaveQty.CausesValidation = false;
System.Web.UI.HtmlControls.HtmlGenericControl createDivTb = new System.Web.UI.HtmlControls.HtmlGenericControl("DIV");
TextBox tb = new TextBox();
//tb.ID = arr_product_id[i] + Guid.NewGuid().ToString("N");
tb.ID = arr_product_id[i];
tb.Text = arr_product_qty[i].ToString();
tb.Attributes["class"] = "dynamicTB";
Label linkQty = new Label();
linkQty.Text = "Quantity";
linkQty.Attributes["class"] = "dynamicQty";
collection.Add(tb.ID);
System.Web.UI.HtmlControls.HtmlGenericControl createDiv3 = new System.Web.UI.HtmlControls.HtmlGenericControl("DIV");
createDiv3.Attributes["class"] = "sub-category-productsDisplayBlockText1_Dynamic_Price";
Label linkPrice = new Label();
linkPrice.Text = arr_product_price[i].ToString();
System.Web.UI.HtmlControls.HtmlGenericControl createDiv2 = new System.Web.UI.HtmlControls.HtmlGenericControl("DIV");
createDiv2.Attributes["class"] = "sub-category-productsDisplayBlockText1_Dynamic_SubTotal";
Label linkSubTotal = new Label();
linkSubTotal.Text = arr_product_subtotal[i].ToString();
ImageButton imgLinkDelete = new ImageButton();
imgLinkDelete.Attributes.Add("width", "25px");
imgLinkDelete.Attributes.Add("height", "25px");
imgLinkDelete.CommandArgument = arr_product_id[i];
imgLinkDelete.Command += new CommandEventHandler(this.OnDynamicLinkButtonClickDelete);
for (int j = 0; j < 1; j++)
{
imgLink.ImageUrl = "NImageMainProductImage.ashx?PhotoId=" + arr_product_id[i];
TableCell cell = new TableCell();
cell.Controls.Add(imgLink);
row.Cells.Add(cell);
cell.VerticalAlign = VerticalAlign.Top;
}
for (int j = 1; j < 2; j++)
{
TableCell cell = new TableCell();
cell.Controls.Add(createDiv1);
createDiv1.Controls.Add(linkProduct);
row.Cells.Add(cell);
cell.VerticalAlign = VerticalAlign.Top;
}
for (int j = 2; j < 3; j++)
{
TableCell cell = new TableCell();
cell.Controls.Add(linkQty);
cell.Controls.Add(new LiteralControl("<br>"));
cell.Controls.Add(tb);
cell.Controls.Add(new LiteralControl("<br>"));
cell.Controls.Add(linkSaveQty);
row.Cells.Add(cell);
cell.VerticalAlign = VerticalAlign.Top;
}
for (int j = 3; j < 4; j++)
{
TableCell cell = new TableCell();
cell.Controls.Add(createDiv3);
createDiv3.Controls.Add(linkPrice);
row.Cells.Add(cell);
cell.VerticalAlign = VerticalAlign.Top;
}
for (int j = 4; j < 5; j++)
{
TableCell cell = new TableCell();
cell.Controls.Add(createDiv2);
createDiv2.Controls.Add(linkSubTotal);
row.Cells.Add(cell);
cell.VerticalAlign = VerticalAlign.Top;
}
for (int j = 5; j < 6; j++)
{
imgLinkDelete.ImageUrl = "../../images/delete.png";
TableCell cell = new TableCell();
cell.Controls.Add(imgLinkDelete);
row.Cells.Add(cell);
cell.VerticalAlign = VerticalAlign.Top;
}
table.Rows.Add(row);
}
TextBoxIdCollection = collection;
PanelItemsCart.Controls.Add(table);
con2.Close();
}
con2.Close();
//...................................Product Listing...............................
}
这是我在页面加载事件上加载动态控件的代码。这是动态创建的链接按钮的枚举字符串和单击事件,它不起作用。
private List<string> TextBoxIdCollection
{
get
{
var collection = ViewState["TextBoxIdCollection"] as List<string>;
return collection ?? new List<string>();
}
set { ViewState["TextBoxIdCollection"] = value; }
}
void OnDynamicLinkButtonClickSave(object sender, CommandEventArgs e)
{
LinkButton clickedButton = (LinkButton)sender;
String commandArgument = clickedButton.CommandArgument;
int value = 0;
foreach (Control c in PanelItemsCart.Controls)
{
if (c.GetType() == typeof(TextBox) && c.ID == (commandArgument))
{
value = Convert.ToInt32(((TextBox)c).Text);
con4.Close();
con4.Open();
SqlDataAdapter adp = new SqlDataAdapter("select * from db_temporary_cart where((IP_address='" + Request.QueryString["ip"].ToString() + "' AND date='" + DateTime.Now.ToShortDateString() + "') AND (product_id='" + commandArgument + "'))", con4);
DataSet ds = new DataSet();
adp.Fill(ds, "db_temporary_cart");
int cd = ds.Tables[0].Rows.Count;
if (cd > 0)
{
int sendQty = value;
con4.Close();
con3.Close();
con3.Open();
SqlCommand cmd4 = new SqlCommand("UPDATE db_temporary_cart SET quantity='" + sendQty + "' where((IP_address='" + Request.QueryString["ip"].ToString() + "' AND date='" + DateTime.Now.ToShortDateString() + "') AND (product_id='" + commandArgument + "'))", con3);
cmd4.ExecuteNonQuery();
con3.Close();
}
con4.Close();
PanelItemsCart.Controls.Clear();
productListing();
}
}
}
请帮助我输入所有文本框&#39;和标签&#39;,它们是动态创建的,在动态创建链接按钮的点击事件中的数据库中。