private DataTable createProductDT() { DataTable dtProducts = new DataTable(); DataColumn productColumn = new DataColumn(); productColumn.DataType = System.Type.GetType("System.Int32"); productColumn.ColumnName = "id"; productColumn.Unique = true; dtProducts.Columns.Add(productColumn); productColumn = new DataColumn(); productColumn.DataType = System.Type.GetType("System.String"); productColumn.ColumnName = "thumb"; dtProducts.Columns.Add(productColumn); productColumn = new DataColumn(); productColumn.DataType = System.Type.GetType("System.String"); productColumn.ColumnName = "name"; dtProducts.Columns.Add(productColumn); productColumn = new DataColumn(); productColumn.DataType = System.Type.GetType("System.Double"); productColumn.ColumnName = "price"; dtProducts.Columns.Add(productColumn); productColumn = new DataColumn(); productColumn.DataType = System.Type.GetType("System.Int32"); productColumn.ColumnName = "quantity"; dtProducts.Columns.Add(productColumn); //使"id"成为主键 DataColumn[] pkColumns = new DataColumn[1]; pkColumns[0] = dtProducts.Columns["id"]; dtProducts.PrimaryKey = pkColumns; return dtProducts; } |
private void populateProducts() { //创建基本结构 DataTable dtProducts = createProductDT(); //把产品添加到其上 //创建初始的行 DataRow aProduct = dtProducts.NewRow(); aProduct["id"] = 11; aProduct["thumb"] = "images/widget0.jpg"; aProduct["name"] = "Red Widget"; aProduct["price"] = 19.99; dtProducts.Rows.Add(aProduct); //重用该行以添加新产品 aProduct = dtProducts.NewRow(); aProduct["id"] = 22; aProduct["thumb"] = "images/widget1.jpg"; aProduct["name"] = "Green Widget"; aProduct["price"] = 50.99; dtProducts.Rows.Add(aProduct); //把DataTable绑定到产品GridView gvProducts.DataSource = dtProducts; gvProducts.DataBind(); //把产品存储到Session Session["dtProducts"] = dtProducts; } |
gvProducts.DataSource = dtProducts; gvProducts.DataBind(); |
DataKeyNames="id" |
EmptyDataText="~Basket is empty~" |
图2.示例程序中实现的购物篮。 |
protected void shopBuy_OnServerClick(object source, EventArgs e) { int index = ((GridViewRow)((HtmlInputButton)source).Parent.NamingContainer).RowIndex; addToBasket(Convert.ToInt32(gvProducts.DataKeys[index].Value)); } protected void addToBasket(int productID) { DataTable dtBasket = getBasketDt(); //循环遍历购物篮并检查是否该项已经存在 bool found = false; for(int i = 0; i < dtBasket.Rows.Count; i++) { if(Convert.ToInt32(dtBasket.Rows[i]["id"]) == productID) { //增加数量并且标记为已发现 dtBasket.Rows[i]["quantity"] = Convert.ToInt32(dtBasket.Rows[i]["quantity"]) + 1; found = true; //当我们已经找到一项时跳出循环 break; } } //如果该项没有找到,则把它添加为一个新行 if(!found) { DataTable dtProducts = getProductsDt(); DataRow drProduct = dtProducts.Rows.Find (productID); //现在,我们已经从数据源中得到了需要的数据,那么我们将把一个新行添加到购物篮中 DataRow newRow = dtBasket.NewRow(); newRow["id"] = drProduct["id"]; newRow["name"] = drProduct["name"]; newRow["price"] = drProduct["price"]; newRow["quantity"] = 1; dtBasket.Rows.Add(newRow); } //把新更新的购物篮存储回会话中 Session["dtBasket"] = dtBasket; //更新购物篮,也即是"重新绑定它" updateShopBasket(); } |
图3.实际使用中的购物篮。 |
//从Quantity文本框中读取数据 HtmlInputText itQuant = (HtmlInputText)row.FindControl("itProductQuantity"); //把该值转换成一个整数 try { int quant = Convert.ToInt32(itQuant.Value); /*如果该值成功转换成一个整数,那么我们还 需要检查它不是一个负数;否则的话,我们可能欠 顾客钱!*/ if(quant > 0) { drProduct["quantity"] = quant; } else { drProduct.Delete(); } } catch { //如果我们不能把它转换成整数,那么我们不作什么改变。 } |
private void updateShopBasket() { gvBasket.DataSource = getBasketDt(); gvBasket.DataBind(); ibEmptyBasket.Visible = ibUpdateBasketQuantities.Visible = ibBasketCheckout.Visible = gvBasket.Rows.Count > 0; } |
"UPDATE tbl_basket SET quantity = " + quantity.Text + " WHERE user_id = " + user_id; |
UPDATE tbl_basket SET quantity = 6 WHERE user_id = 230; |
" 1 WHERE 1 = 1; DROP tbl_users; --" |
UPDATE tbl_basket SET quantity = 1 WHERE 1 = 1; DROP tbl_users; -- WHERE user_id =; |
protected void shopBasketCheckout_OnServerClick(object source,EventArgs e) { string postData = ""; postData += "currency_code=GBP"; postData += "&cmd=_cart"; postData += "&[email protected]"; postData += "&upload=1"; postData += "&cancel_return=www.davidmillington.net"; DataTable dtBasket = getBasketDt(); double total = 0.00; for(int i = 0; i < dtBasket.Rows.Count; i++) { postData += "&item_name_" + (i + 1) + "=" + dtBasket.Rows[i]["name"]; postData += "&quantity_" + (i + 1) + "=" + dtBasket.Rows[i]["quantity"]; postData += "&amount_" + (i + 1) + "=" + Convert.ToDouble(dtBasket.Rows[i]["price"]); total += (Convert.ToDouble(dtBasket.Rows[i] ["price"]) * Convert.ToInt32(dtBasket.Rows[i]["quantity"])); if(i == dtBasket.Rows.Count - 1) { postData += "&shipping_" + (i + 1) + "=" + calcDeliveryCost(total); } else { postData += "&shipping_" + (i + 1) + "=0.00"; } postData += "&shipping2_" + (i + 1) + "=0.00"; postData += "&handling_" + (i + 1) + "=0.00"; } postData += "&handling=" + calcDeliveryCost(total); byte[] data = Encoding.ASCII.GetBytes(postData); HttpWebRequest ppRequest = (HttpWebRequest) WebRequest.Create("https://www.paypal.com/cgi-bin/webscr");; ppRequest.Method = "POST"; ppRequest.ContentType = "application/x-www-form- urlencoded"; ppRequest.ContentLength = data.Length; //发送 Stream ppStream = ppRequest.GetRequestStream(); ppStream.Write(data, 0, data.Length); ppStream.Close(); //接收 HttpWebResponse ppResponse = (HttpWebResponse)ppRequest.GetResponse(); StreamReader sr = new StreamReader(ppResponse.GetResponseStream()); string strResult = sr.ReadToEnd(); sr.Close(); //输出到屏幕 Response.Clear(); Response.Write(strResult); Response.End(); } |
本文出自 “青峰” 博客,转载请与作者联系!