我正在研究添加到购物车功能,并且我添加了一个GridView
启用分页的控件。在 中GridView
,我在文本框中显示数量并处理该文本框的OnTextChanged
事件。现在的问题是,如何将更改后的数量文本保持在会话或视图状态以及在哪一行,以便我可以更新我GridView
的数据并将该数据再次绑定到GridView
?
在这里,我使用了GridView
id gvMaster。
protected void txtQuantity_TextChanged(object sender, EventArgs e)
{
gvMaster.DataSource = ProductDetailsGridMaster();
gvMaster.AllowPaging = true;
gvMaster.DataBind();
}
public DataTable ProductDetailsGridMaster()
{
DataTable dtProducts = new DataTable();
dtProducts.Columns.Add("ProductId");
dtProducts.Columns.Add("ProductName");
dtProducts.Columns.Add("ProductPrice");
dtProducts.Columns.Add("Quantity");
dtProducts.Columns.Add("Price");
gvMaster.AllowPaging = false;
if (Session["dtProducts"] != null)
{
GridView gv = new GridView();
gv.DataSource = Session["dtProducts"];
gvMaster.DataSource = gv.DataSource;
gvMaster.DataBind();
lblMessage.Text = "";
}
//GridView gvc = (GridView)Page.FindControl("gvMaster");
for (int i = 0; i < gvMaster.Rows.Count; i++)
{
Label lblProductId = (Label)gvMaster.Rows[i].Cells[0].FindControl("lblProductId");
Label lblProductName = (Label)gvMaster.Rows[i].Cells[1].FindControl("lblProductName");
Label lblProductPrice = (Label)gvMaster.Rows[i].Cells[2].FindControl("lblProductPrice");
//Label lblssno = (Label)gv.Rows[i].Cells[2].FindControl("lblSSNo");
TextBox txtQuantity = (TextBox)gvMaster.Rows[i].Cells[3].FindControl("txtQuantity");
//TextBox mastertxtQuantity = (TextBox)gvMaster.Rows[i].Cells[3].FindControl("txtQuantity");
Label lblPrice = (Label)gvMaster.Rows[i].Cells[4].FindControl("lblPrice");
var Price = decimal.Parse(lblProductPrice.Text) * decimal.Parse(txtQuantity.Text);
lblPrice.Text = Price.ToString();
DataRow dr = dtProducts.NewRow();
dr["ProductId"] = lblProductId.Text;
dr["ProductName"] = lblProductName.Text;
dr["ProductPrice"] = lblProductPrice.Text;
dr["Quantity"] = txtQuantity.Text;
dr["Price"] = lblPrice.Text;
dtProducts.Rows.Add(dr);
}
Session["dtProducts"] = dtProducts;
return dtProducts;
}
我想在启用分页的网格中显示更改的数量值。