0

I am working on Add To Cart Functionality, and I have added a GridView control with paging enabled. In the GridView, I show the quantity in a text box and handle the OnTextChanged event for that textbox. Now the problem, is how can I keep the changed quantity text in session or view state, and in which row, so that I can update my GridView and bind that data again to the GridView?

Here I took GridView with 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;
}

I want to show changed quantity value in grid with paging enabled.

Brad Larson
  • 170,088
  • 45
  • 397
  • 571
Chirayu
  • 1
  • 2

2 Answers2

0

I can hardly understand what your code really does. However, such information like the state of a control shouldn't be kept in Session. Use ViewState instead.

dzendras
  • 4,721
  • 1
  • 25
  • 20
  • In the gridview i used paging and hence when i retrieve rows on text changed event it returns only those rows on which, pageindex i have changed. so i set paging false and bind that gridview from session. now the problem is how i can bind changed text value on that row. that can be possible by keeping that value in viewstate or session (inproc). So my question is how can i save that value in view state and how i can retrieve info about the row in which i made change ? – Chirayu Jan 02 '11 at 12:38
-1

Add C# Asp.net tags to your question. Also look up n-tier programming/mvc with asp.net , it will help you improve your code many folds

jimjim
  • 2,414
  • 2
  • 26
  • 46
  • why voting down without a reason? – jimjim Jan 05 '11 at 09:41
  • 1
    I wasn't the one downvoting it, but I can see why. The first bit of your answer you could've edited in yourself or put in the comments field and the second part is just a polite, but broad RTFM and by no means helpful. – Jules Aug 05 '11 at 11:31
  • @Jules : I be gratefull if you edited my answer accordingly. Or if you think it is better to be deleted. I was merely trying to point out where to look, not to RTFM. When I was starting if somebody told me to go and chase up 'these keywords' I would considered it all the help I needed, rather than them trying to explain all the details to me. – jimjim Aug 05 '11 at 12:46