2013-05-27 3 views
0

J'ai gridview suivant:événement Gridview OnRowCommand pas de tir

<asp:Panel ID="pnlScroll" runat="server" ScrollBars="Auto"> 
    <asp:UpdatePanel ID="UpdatePanel1" runat="server"> 
     <ContentTemplate> 
      <asp:GridView ID="GVCart2" runat="server" AutoGenerateColumns="False" GridLines="Vertical" onrowcommand="CartUpdate"> 
       <AlternatingRowStyle BackColor="#CCCCCC" /> 
       <Columns> 
        <asp:BoundField DataField="Product_Name" HeaderText="Product Name" /> 
        <asp:BoundField DataField="Product_ID" HeaderText="Product ID" /> 
        <asp:BoundField DataField="ItemQTY" HeaderText="ItemQTY" /> 
        <asp:TemplateField> 
         <ItemTemplate> 
          <asp:Button ID="IncreaseCartQty" runat="server" CommandArgument="<%#((GridViewRow)Container).RowIndex %>" CommandName="IncreaseCartQty" Text="+" /> 
         </ItemTemplate> 
        </asp:TemplateField> 
        <asp:BoundField DataField="Price" HeaderText="Price" /> 
        <asp:BoundField DataField="TotalPrice" HeaderText="TotalPrice" /> 
       </Columns> 
       <FooterStyle BackColor="#CCCCCC" /> 
       <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" /> 
       <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" /> 
       <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" /> 
       <SortedAscendingCellStyle BackColor="#F1F1F1" /> 
       <SortedAscendingHeaderStyle BackColor="#808080" /> 
       <SortedDescendingCellStyle BackColor="#CAC9C9" /> 
       <SortedDescendingHeaderStyle BackColor="#383838" /> 
      </asp:GridView> 
     </ContentTemplate> 
    </asp:UpdatePanel> 
    </asp:Panel> 

GridView a un bouton pour augmenter le montant de l'article. Le bouton a le nom de la commande "IncreaseCartQty". OnRowCommand J'essaie d'appeler "CartUpdate". Mais le problème est que l'événement OnRowCommand est et ne déclenche pas.

La page derrière le code est le suivant:

Sur la page charge:

protected void Page_Load(object sender, EventArgs e) 
    { 
     if (Session["ItemsCount"] != null) 
     { 
     CartDT = (DataTable)Session["cart"]; 
     GVCart2.DataSource = CartDT; 
     GVCart2.DataBind(); 
     } 
    } 

La fonction que je veux faire appel à gridview OnRowCommand est:

public void CartUpdate(object sender, GridViewCommandEventArgs e) 
     { 
      CartDT = (DataTable)Session["cart"]; 
      if (e.CommandName == "IncreaseCartQty") 
      { 
       DataRow DR = CartDT.NewRow(); 
       int rowIndex = Convert.ToInt32(e.CommandArgument.ToString()); 
       int qty = Convert.ToInt32(CartDT.Rows[rowIndex][2].ToString()); 
       DR[0] = CartDT.Rows[rowIndex][0]; 
       DR[1] = CartDT.Rows[rowIndex][1]; 
       DR[2] = ++qty; 
       DR[3] = CartDT.Rows[rowIndex][3]; 
       DR[4] = qty * double.Parse(CartDT.Rows[rowIndex][3].ToString()); 
       CartDT.Rows.RemoveAt(rowIndex); 
       CartDT.Rows.InsertAt(DR, rowIndex); 
       CartDT.AcceptChanges(); 
       Session["cart"] = CartDT; 
       GVCart2.DataSource = CartDT; 
       GVCart2.DataBind(); 
       TotalOrderAmt(); 
       TotalItemsCount(); 
      } 

     } 

Les fonctions pour calculer l'ordre amt et le nombre d'éléments est:

public void TotalOrderAmt() 
     { 
      double t = 0; 
      for (int i = 0; i < CartDT.Rows.Count; i++) 
      { 
       t = t + double.Parse(CartDT.Rows[i][4].ToString()); 
      } 

      Session["TotalOrderAmt"] = t; 
     } 

     public void TotalItemsCount() 
     { 
      double t = 0; 
      for (int i = 0; i < CartDT.Rows.Count; i++) 
      { 
       t = t + double.Parse(CartDT.Rows[i][4].ToString()); 
      } 

      Session["ItemsCount"] = t; 
     } 

Maintenant, je ne suis pas en mesure de comprendre pourquoi l'événement OnRowCommand est pas de tir. Rien ne se passe en cliquant sur le bouton dans gridview.

Faites-moi savoir si exactement c'est incorrect.

Répondre

1

Le problème que vous avez est sur la méthode de chargement de la page. Parce que vous avez besoin de la méthode Ispostback.

Remplacer que (voir ci-dessous le code)

protected void Page_Load(object sender, EventArgs e) 
    { 
     if (Session["ItemsCount"] != null) 
     { 
     CartDT = (DataTable)Session["cart"]; 
     GVCart2.DataSource = CartDT; 
     GVCart2.DataBind(); 
     } 
    } 

pour cela (voir ci-dessous le code)

protected void Page_Load(object sender, EventArgs e) 
    { 
     if (!IsPostBack) 
     { 
     if (Session["ItemsCount"] != null) 
     { 
     CartDT = (DataTable)Session["cart"]; 
     GVCart2.DataSource = CartDT; 
     GVCart2.DataBind(); 
     } 
     } 
    } 

Si vous n'utilisez la méthode !Ispostback, il renvoie une erreur et n'exécute pas l'argument Onrowcommand.

J'espère que cela aide.

EDIT:

Pour plus d'aide Invalid Postback on Grid

+0

Hey ... Ce monstre Thanx travaille tout va bien maintenant. –

+0

Vous êtes les bienvenus :) –

Questions connexes