Hi all,
I have a simple page on which I've place a gridview which is bound to a generic List that contains ShoppingCartItem objects.
the code for my aspx file looks like this:
<%@ Page Title="" Language="C#" MasterPageFile="~/Customer/CustomerZone_SelfService_ThreeColumns.Master"
AutoEventWireup="true" EnableEventValidation="false" CodeBehind="PrimeShopCheckout.aspx.cs" Inherits="Tw.Sites.PrimeHome.Customer.PrimeShopCheckout" %>
<asp:Content ID="Content3" ContentPlaceHolderID="ArticleContent" runat="server">
<asp:Label ID="lblShoppingCartStatus" runat="server" Text=""></asp:Label>
<br />
<asp:GridView OnRowEditing="gridCart_RowEditing"
OnRowCancelingEdit="gridCart_RowCancelingEdit"
OnRowUpdating="gridCart_RowUpdating"
OnRowCommand="OnRowCommand"
OnRowUpdated="gridCart_RowUpdated"
ID="gridCart" runat="server"
AutoGenerateColumns="false" EnableViewState="true">
<Columns>
<asp:BoundField ReadOnly="true" DataField="Description" HeaderText="Product"
HtmlEncode="False">
<ItemStyle Width="300px" />
</asp:BoundField>
<asp:BoundField ReadOnly="true" DataField="Price" HeaderText="Price"
HtmlEncode="False">
<ItemStyle Width="100px" />
</asp:BoundField>
<asp:BoundField ReadOnly="false" DataField="Quantity" HeaderText="Quantity"
HtmlEncode="False">
<ItemStyle Width="100px" />
</asp:BoundField>
<asp:CommandField ButtonType="Link" EditText="Modify" ShowEditButton="true" CausesValidation="false" />
</Columns>
</asp:GridView>
</asp:Content>
My code behind code looks like this:
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
namespace Customer
{
public partial class PrimeShopCheckout : System.Web.UI.Page
{
private List<ShoppingCartItem> ShoppingCartItems
{
get
{
if (ViewState["shoppingCartItems"] != null)
{
return (List<ShoppingCartItem>)ViewState["shoppingCartItems"];
}
return new List<ShoppingCartItem>();
}
set
{
ViewState["shoppingCartItems"] = value;
}
}
private void BindGrid()
{
gridCart.DataSource = ShoppingCartItems;
gridCart.DataBind();
}
protected void gridCart_RowEditing(object sender, GridViewEditEventArgs e)
{
gridCart.EditIndex = e.NewEditIndex;
BindGrid();
}
protected void gridCart_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
gridCart.EditIndex = -1;
BindGrid();
}
protected void gridCart_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
// get the information
int rowIndex = e.RowIndex;
var a = gridCart.Rows.Count;
}
protected void gridCart_RowUpdated(object sender, GridViewUpdatedEventArgs e)
{
e.KeepInEditMode = false;
}
protected void OnRowCommand(object sender, GridViewCommandEventArgs e)
{
var aaa = gridCart.Rows.Count;
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Hashtable shoppingCart = (Hashtable)WebProfile.Current.ShoppingCart;
List<ShoppingCartItem> shoppingCartItems = new List<ShoppingCartItem>();
IDictionaryEnumerator enumerator = shoppingCart.GetEnumerator();
while (enumerator.MoveNext())
{
var item = new ShoppingCartItem();
Product product = (Product)enumerator.Key;
int quantity = (int)enumerator.Value;
item.Description = product.Description;
item.Price = product.Price;
item.Quantity = quantity;
shoppingCartItems.Add(item);
}
ShoppingCartItems = shoppingCartItems;
gridCart.DataSource = ShoppingCartItems;
gridCart.DataBind();
}
}
}
[Serializable]
public class ShoppingCartItem
{
public string Description { get; set; }
public decimal Price { get; set; }
public int Quantity { get; set; }
}
}
The problem is that when I click the Update button to change the number of items for a specific product, in the RowUpdating event handler I get the number of rows as 0!!!!
I've read plenty of docs on the gridview, searched google for answers to my problem, saw implementations for the functionality I use .... according to what I read so far, everything should work fine. But it's not!
Can anyone let me know why the number of rows is zero in the row updating event?
Where am I losing the data in the gridview?
I need help with this as I am completly stuck at this moment - I don't have any clue what else to try....
Thanks in advance!
Andrei