本文介绍了我应该在此代码中添加什么来从另一个页面获取产品ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 public partial class AddToCart:System.Web.UI.Page { decimal total; DataTable cart = null ; 受保护 void Page_Load( object sender,EventArgs e) { if (会话[ currentUId] == null ) Response.Redirect( 〜/ Account / Login.aspx); if (会话[ cart]!= null ) { // Response.Write(cart.Rows [0] [p_id]。ToString()); /// 如果存在,则从会话中读取购物车 cart =(DataTable)会话[ cart]; // GridView4.DataSource = cart; // GridView4.DataBind(); // total = Convert.ToDecimal(Session [total]。ToString()); } else { // 创建一个空的DataTable并向其添加一些列 cart = new DataTable(); cart.Columns.Add( p_id, typeof ( int )); cart.Columns.Add( p_name, typeof ( string )); cart.Columns.Add( p_price, typeof ( decimal )); cart.Columns.Add( p_quantity, typeof ( int )); cart.Columns.Add( item_total, typeof ( decimal )); // total = 0; } refreshTotal(); if (请求[ DelID]!= null ) { for ( int i = 0 ; i < cart.Rows.Count; i ++) { if (cart.Rows [i] [ p_id]。ToString()==请求[ DelID]。ToString()) cart.Rows.Remove(cart.Rows [i]); } 会话[ cart] = cart; refreshTotal(); } if (请求[ ID]!= null ) { // DataTable中的搜索项 bool Found = 假; for ( int i = 0 ; i < cart.Rows.Count; i ++) { if (cart.Rows [i] [ p_id]。ToString()== Request [ ID]。ToString()) Found = 真; } // 如果客户尚未添加,则添加到购物篮 if (Found == false ) { int pid = Convert.ToInt16(请求[ ID ]); DataTable dt = new DataTable(); 使用(SqlConnection conn = new SqlConnection(SqlDataSource1.ConnectionString.ToString())) { 使用(SqlCommand comm = new SqlCommand( 从产品中选择ProductName,UnitCost,其中ProductID = + pid,conn)) { conn。打开(); 使用(SqlDataReader myReader = comm.ExecuteReader()) { dt.Load(myReader); } conn.Close(); } } // sqldatareader只读取一行,因为产品有唯一的p_id 字符串 name =( string )dt.Rows [ 0 ] [ ProductName]; decimal price =( decimal )dt.Rows [ 0 ] [ UnitCost]; // Response.Write(产品:+名称+价格: +价格+< br />); { cart.Rows.Add( new object [] {Request [ ID], name,price,( int ) 1 ,( decimal )价格* 1 }); } } 会话[ cart] =购物车; refreshTotal(); } if (IsPostBack == false ) { GridView4.DataSource = cart; GridView4.DataBind(); refreshTotal(); } refreshTotal(); } 受保护 void refreshCart() { for ( int i = 0 ; i < GridView4.Rows.Count; i ++) { TextBox Tb =(TextBox)GridView4.Rows [i] .FindControl( TextBoxCount); cart.Rows [i] [ p_quantity] = Convert.ToInt16(Tb。文本); // Response.Write(quantity:+ Tb.Text +:); decimal qxp =( decimal )((Convert.ToInt32( Tb.Text)*( decimal )cart.Rows [i] [ p_price])); cart.Rows [i] [ item_total] = qxp; // Response.Write(>> Total+ qxp.ToString()+< ; br />); } refreshTotal(); Response.Redirect( 〜/ Products.aspx); } 受保护 void refreshTotal() { total =( decimal ) 0 ; for ( int i = 0 ; i < cart.Rows.Count; i ++) { // Response.Write(total); total + =( decimal )(购物车) .Rows [i] [ item_total]); } Label1.Text = total.ToString(); 会话[ total] = total; } 受保护 void Button1_Click( object sender,EventArgs e) { refreshCart(); // Response.Write(total.ToString()); // Response.Write((string)GetMaxRequest()。ToString()); } protected int GetMaxRequest() { 使用(SqlConnection conn = new SqlConnection(SqlDataSourceRequest.ConnectionString.ToString())) { SqlCommand maximum = new SqlCommand( SELECT MAX(R_Id) as FROM FROM [Requests],conn); conn.Open(); int m = Convert.ToInt32(maximum.ExecuteScalar()); conn.Close(); return m + 1 ; } } 受保护 void InsertOrder( int r_id, int p_id, int p_quantity, decimal item_total) { 使用(SqlConnection conn = new SqlConnection(SqlDataSourceOrder.ConnectionString.ToString())) { SqlCommand cmd = new SqlCommand ( INSERT INTO [Orders](R_Id,ProductID,quantity,Items_total)VALUES( + r_id + , + p_id + , + p_quantity + , +项目_ total + ),conn); conn.Open(); cmd.ExecuteNonQuery(); conn.Close(); } } 受保护 void InsertRequest( string UId, string sdate, decimal total ) { 使用(SqlConnection conn = new SqlConnection(SqlDataSourceRequest.ConnectionString。 ToString())) { SqlCommand cmd = new SqlCommand( INSERT INTO请求(C_Id,R_Time,R_total)VALUES(' + UId + ',' + sdate + ', +总计+ ),conn); conn.Open(); cmd.ExecuteNonQuery(); conn.Close(); } } 受保护 void Button2_Click( object sender,EventArgs e) { int r_id = GetMaxRequest(); for ( int i = 0 ; i < cart.Rows.Count; i ++) { InsertOrder(( int )r_id,Convert.ToInt16(cart.Rows [i] [ p_id ]),Convert.ToInt16(cart.Rows [i] [ p_quantity]),转换。 ToDecimal(cart.Rows [i] [ item_total])); } InsertRequest(会话[ currentUId]。ToString(), DateTime.Now.ToString(),( decimal )total); // 会话[ctrl] = pnl1; ClientScript.RegisterStartupScript( this .GetType(), onclick, < script language = javascript> window.open('Print.aspx',' PrintMe, '身高= 300像素,宽300像素=,滚动条= 1');< /脚本>中); // 会话[total] = null; 会话[ cart] = null ; 会话[ r_id] = r_id; Response.Write( < div class = response>感谢您购买我们的产品< / div> ); } } 解决方案 在新页面的页面加载中你必须像这样添加 if(!IsPostBack) { if(Request.QueryString.Count> 0如果(Request.QueryString [x]!= null) { = Request.QueryString [x]。ToString(); } } 其他 { - 什么东西---- } } public partial class AddToCart : System.Web.UI.Page{ decimal total; DataTable cart = null; protected void Page_Load(object sender, EventArgs e) { if (Session["currentUId"] == null) Response.Redirect("~/Account/Login.aspx"); if (Session["cart"] != null) { //Response.Write(cart.Rows[0]["p_id"].ToString()); ///read cart from session if exist cart = (DataTable)Session["cart"]; //GridView4.DataSource = cart; //GridView4.DataBind(); //total = Convert.ToDecimal(Session["total"].ToString()); } else { //create an empty DataTable and Add some columns to it cart = new DataTable(); cart.Columns.Add("p_id", typeof(int)); cart.Columns.Add("p_name", typeof(string)); cart.Columns.Add("p_price", typeof(decimal)); cart.Columns.Add("p_quantity", typeof(int)); cart.Columns.Add("item_total", typeof(decimal)); //total = 0; } refreshTotal(); if (Request["DelID"] != null) { for (int i = 0; i < cart.Rows.Count; i++) { if (cart.Rows[i]["p_id"].ToString() == Request["DelID"].ToString()) cart.Rows.Remove(cart.Rows[i]); } Session["cart"] = cart; refreshTotal(); } if (Request["ID"] != null) { //search item in DataTable bool Found = false; for (int i = 0; i < cart.Rows.Count; i++) { if (cart.Rows[i]["p_id"].ToString() == Request["ID"].ToString()) Found = true; } //add to basket if not yet added by customer if (Found == false) { int pid = Convert.ToInt16(Request["ID"]); DataTable dt = new DataTable(); using (SqlConnection conn = new SqlConnection(SqlDataSource1.ConnectionString.ToString())) { using (SqlCommand comm = new SqlCommand("select ProductName,UnitCost from Products where ProductID = " + pid, conn)) { conn.Open(); using (SqlDataReader myReader = comm.ExecuteReader()) { dt.Load(myReader); } conn.Close(); } } // the sqldatareader reads a single row only since products have a unique p_id string name = (string)dt.Rows[0]["ProductName"]; decimal price = (decimal)dt.Rows[0]["UnitCost"]; // Response.Write("Product : " + name + " of price: " + price + "<br/>"); { cart.Rows.Add(new object[] { Request["ID"], name, price, (int)1, (decimal)price * 1 }); } } Session["cart"] = cart; refreshTotal(); } if (IsPostBack == false) { GridView4.DataSource = cart; GridView4.DataBind(); refreshTotal(); } refreshTotal(); } protected void refreshCart() { for (int i = 0; i < GridView4.Rows.Count; i++) { TextBox Tb = (TextBox)GridView4.Rows[i].FindControl("TextBoxCount"); cart.Rows[i]["p_quantity"] = Convert.ToInt16(Tb.Text); //Response.Write("quantity : " + Tb.Text + " : "); decimal qxp = (decimal)((Convert.ToInt32(Tb.Text) * (decimal)cart.Rows[i]["p_price"])); cart.Rows[i]["item_total"] = qxp; //Response.Write(" >> Total" + qxp.ToString() + "<br/>"); } refreshTotal(); Response.Redirect("~/Products.aspx"); } protected void refreshTotal() { total = (decimal)0; for (int i = 0; i < cart.Rows.Count; i++) { //Response.Write(total); total += (decimal)(cart.Rows[i]["item_total"]); } Label1.Text = total.ToString(); Session["total"] = total; } protected void Button1_Click(object sender, EventArgs e) { refreshCart(); //Response.Write(total.ToString()); //Response.Write((string)GetMaxRequest().ToString()); } protected int GetMaxRequest() { using (SqlConnection conn = new SqlConnection(SqlDataSourceRequest.ConnectionString.ToString())) { SqlCommand maximum = new SqlCommand("SELECT MAX(R_Id) as maximum FROM [Requests]", conn); conn.Open(); int m = Convert.ToInt32(maximum.ExecuteScalar()); conn.Close(); return m + 1; } } protected void InsertOrder(int r_id, int p_id, int p_quantity, decimal item_total) { using (SqlConnection conn = new SqlConnection(SqlDataSourceOrder.ConnectionString.ToString())) { SqlCommand cmd = new SqlCommand("INSERT INTO [Orders](R_Id, ProductID, quantity,Items_total) VALUES (" + r_id + "," + p_id + "," + p_quantity + "," + item_total + ")", conn); conn.Open(); cmd.ExecuteNonQuery(); conn.Close(); } } protected void InsertRequest(string UId, string sdate, decimal total) { using (SqlConnection conn = new SqlConnection(SqlDataSourceRequest.ConnectionString.ToString())) { SqlCommand cmd = new SqlCommand("INSERT INTO Requests(C_Id, R_Time,R_total) VALUES ('" + UId + "','" + sdate + "'," + total + ")", conn); conn.Open(); cmd.ExecuteNonQuery(); conn.Close(); } } protected void Button2_Click(object sender, EventArgs e) { int r_id = GetMaxRequest(); for (int i = 0; i < cart.Rows.Count; i++) { InsertOrder((int)r_id, Convert.ToInt16(cart.Rows[i]["p_id"]), Convert.ToInt16(cart.Rows[i]["p_quantity"]), Convert.ToDecimal(cart.Rows[i]["item_total"])); } InsertRequest(Session["currentUId"].ToString(), DateTime.Now.ToString(), (decimal)total); //Session["ctrl"] = pnl1; ClientScript.RegisterStartupScript(this.GetType(), "onclick", "<script language=javascript>window.open('Print.aspx','PrintMe','height=300px,width=300px,scrollbars=1');</script>"); //Session["total"] = null; Session["cart"] = null; Session["r_id"] = r_id; Response.Write("<div class=response> Thank You For Purchasing Our Products</div>"); }} 解决方案 in pageload in newpage u have to add like thisif (!IsPostBack) { if (Request.QueryString.Count > 0) { if (Request.QueryString["x"] != null) { strproductID = Request.QueryString["x"].ToString(); } } else { --do something---- } } 这篇关于我应该在此代码中添加什么来从另一个页面获取产品ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
11-03 12:17