Problem with gridview RowUpdating event

Last post 07-05-2009 3:36 PM by Atiq Mir. 7 replies.

Sort Posts:

  • Problem with gridview RowUpdating event

    10-22-2008, 8:54 AM
    • Participant
      944 point Participant
    • acroitoriu
    • Member since 08-07-2008, 12:04 PM
    • Limassol
    • Posts 160

     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

    http://acroitoriu.spaces.live.com
    "No complexity beyond what is necessary"
  • Re: Problem with gridview RowUpdating event

    10-26-2008, 11:18 PM

    Hi acroitoriu,

    Check ViewState["shoppingCartItems"] in RowEditing and RowUpdating events to see if it is null or not.

    Thanks,

    Qin Dian Tang
    Microsoft Online Community Support

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
  • Re: Problem with gridview RowUpdating event

    10-27-2008, 3:05 AM
    • Participant
      944 point Participant
    • acroitoriu
    • Member since 08-07-2008, 12:04 PM
    • Limassol
    • Posts 160

     Hi,

    The ViewState is fine in both methods... this was one of the first things I checked and the ViewState is not altered anywhere.

    After I press the update link, in debug mode when in postsback and it first goes to Page_Load, I still have the rows in the gridview, but when it finally reaches the RowUpdating event handler the rows are lost somehow.

    The EnableViewState property is set to true both for gridview and page. On my page I have a few user controls that call the DataBind() method in the load event and also  the page the gridview is on also calls the DataBind in the load. Tried to remove the DataBind calls, but without any result.

    I'll still lost since I don't manage to see the the gridview content is lost.

    Do anyone have any idea about what could I try?

     

    Thank you!

    Andrei

    http://acroitoriu.spaces.live.com
    "No complexity beyond what is necessary"
  • Re: Problem with gridview RowUpdating event

    10-27-2008, 3:11 AM

    Hi acroitoriu,

    Can you get the value from e.RowIndex or gridCart.EditIndex in RowUpdating event? If yes, you can try to do updating by using gridCart.Rows[e.RowIndex], then rebind GridView and check the count of rows again.

    Thanks,

    Qin Dian Tang
    Microsoft Online Community Support

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
  • Re: Problem with gridview RowUpdating event

    10-27-2008, 3:26 AM
    • Participant
      944 point Participant
    • acroitoriu
    • Member since 08-07-2008, 12:04 PM
    • Limassol
    • Posts 160

     Hi,

     

    Yes, I get the correct index of the row I am trying to update, but right after I get it, when I try to use the row with gridCart.Rows[index] I get an index out of range exception Crying

     

    Andrei

    http://acroitoriu.spaces.live.com
    "No complexity beyond what is necessary"
  • Re: Problem with gridview RowUpdating event

    04-28-2009, 2:32 PM
    • Member
      20 point Member
    • fsteveb
    • Member since 03-16-2003, 10:37 AM
    • Posts 12

    I am getting the exact same bahavior. Click on Update and in gv_RowUpdating(object sender, GridViewUpdateEventArgs e)  the gv.Rows.Count is always 0. So can't read what is on that row, since there are no rows.

    Also gv_RowUpdated never gets called. At first I was not getting gv_RowUpdating called, but read if I set CausesValidation = false then it would work, and that is true.

     

  • Re: Problem with gridview RowUpdating event

    05-16-2009, 7:19 AM
    • Member
      2 point Member
    • HerzogEH
    • Member since 05-16-2009, 11:14 AM
    • Posts 1

    If CausesValidation=true, the asp:CommandField will produce an html output like this:

    <input type="submit" name="gv$ctl02$ctl00" value="update" />

    With CausesValidation=false you will get the correct output like this:

    <input type="button" value="update" onclick="javascript:__doPostBack('gv','Update$0')" />

    greetings

     Erwin

  • Re: Problem with gridview RowUpdating event

    07-05-2009, 3:36 PM
    • Member
      4 point Member
    • Atiq Mir
    • Member since 06-08-2009, 2:58 AM
    • Broomfield, CO
    • Posts 2

    Don't you need DataKeyNames property in your grid? Add a unique "Id" property to the ShoppingCartItem and use it as the DataKey.

    It is my understanding that DataKey has to be unique? If you use Description or anyother field as DataKey then what if the two items have the same description (say Description="") - the GridView won't be able to identify which row belongs to which item.

    Let me know if this helps.


Page 1 of 1 (8 items)