我正在爲我的網頁項目編寫購物車功能。在購物車頁面上,它會在Cookie中檢查您的購物車ID,然後獲取數據庫中的所有商品。所以,Cart表中的每個項目都是動態創建的。在.aspx頁面上,創建了一個空表。在代碼隱藏中,爲每個項目創建一個新行和單元格。使用動態創建的文本框更新數據庫
我在更新某個項目的數量時遇到問題。對於購物車中的每件商品,都會爲數量動態創建文本框和按鈕並更新數量。數量設置爲頁面加載時數據庫中的數量。我似乎無法獲得文本框的值,例如從1到2.
下面是如何填充表的摘要。我縮短了很多,只是爲了展示我如何將數據加載到表中。
protected void Page_Load(object sender, EventArgs e){
cartpopulate(cartID);
}
private void cartpopulate(int cartID){
//for each item in database where cartID is paramater cartID
//create a row
//get the product name, image, price, quantity, options, and make a cell for each
//calculate total price from quantity and price
//add in delete and quantity update buttons
爲了方便,讓剛剛假設所有的分位數爲1。因此,在Page_Load中,將所有的數量設置爲1。我想我的問題是,下面的代碼將文本框的值無論數量是多少時填充,這將是1.當您將其更改爲2或其他值時,該值已設置,並且2不執行任何操作。數據庫更新部分工作,我只是無法獲得第二個值'2'。
這裏的量更新代碼...
if (prod.Quantity == null) { quantitylabel.Value = 1.ToString(); }
else { quantitylabel.Value = prod.Quantity.ToString() ; }
quantityinput.ID = "quantity" + i.ToString();
quantityinput.Width = 20;
quantityinput.Text = quantitylabel.Value.ToString();
quantitycell.Controls.Add(quantityinput);
quantitycell.CssClass="cartitem";
row.Cells.Add(quantitycell);
Button btnUpdate = new Button();
btnUpdate.Text = "Update";
string arg = quantityinput.Text;
btnUpdate.CommandName = "update";
btnUpdate.CommandArgument = arg;
btnUpdate.Command += (s, e) =>
{
string quanupdate = Convert.ToString(e.CommandArgument);
int quanint = Convert.ToInt32(quanupdate);
prod.Quantity = quanint;
db.SubmitChanges();
Response.Redirect("cart.aspx");
};
我已經看了幾個不同的解決方案,但他們都不工作。我懷疑的兩件事是問題會做的事情!IsPostBack,或重新創建回發動態創建的控件。我在網絡編程方面自學成才,所以我不是100%確定該怎麼做,但我認爲其中之一就是問題所在。
在此先感謝