Umm Hani

Umm Hani

  • NA
  • 103
  • 3.8k

i want to delete row in gridview from session

Aug 21 2019 1:10 PM
In my project i have two pages.
 
In one page having a product and in another page having a gridview.iam using a class of product and for data connecting using a reference of Database.Helper.
 
i am facing a problem in my code is that when i select product from one page that product add in to cart and show in another page but when i want to delete product from gridview it is not deleted,
 
code of gridview
  1. <%@ Page Title="" Language="C#" MasterPageFile="~/Web.Master" AutoEventWireup="true" CodeBehind="cart_view.aspx.cs" Inherits="Jewelleryonlineshop.cart_view" %>    
  2. <asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">    
  3.     <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css"/>    
  4.   <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>    
  5.   <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>    
  6.     <style type="text/css">    
  7.         .auto-style2 {    
  8.             position: relative;    
  9.             min-height: 1px;    
  10.             float: left;    
  11.             width: -2147483648%;    
  12.             left: -10px;    
  13.             top: 7px;    
  14.             height: 320px;    
  15.             padding-left: 15px;    
  16.             padding-right: 15px;    
  17.             text-align: center;    
  18.         }    
  19.         .auto-style4 {    
  20.             margin-right: 31;    
  21.         }    
  22.     </style>    
  23.      <script type="text/javascript">    
  24.         function CalculateTotals() {    
  25.             var gv = document.getElementById("<%= products_gw.ClientID %>");    
  26.             var rows = gv.getElementsByTagName("tr");    
  27.             var grandTotal = 0;    
  28.             for (var i = 0; i < rows.length; i++) {    
  29.                 var tds = rows[i].getElementsByTagName("td");    
  30.                 if (rows[i].getElementsByTagName("td").length > 0) {    
  31.                     var price = rows[i].getElementsByTagName("span")[2].innerHTML;    
  32.                     var quantity = rows[i].getElementsByTagName("input")[0].value;    
  33.                     var total = parseFloat(price) * parseFloat(quantity);    
  34.                     rows[i].getElementsByTagName("span")[3].innerHTML = isNaN(total) ? 0 : total;    
  35.                     grandTotal += isNaN(total) ? 0 : total;    
  36.                 }    
  37.             }    
  38.             document.getElementById("<%= lblSumTotal.ClientID %>").innerHTML = grandTotal;    
  39.         }    
  40.     </script>    
  41. </asp:Content>    
  42. <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">    
  43. <div class="container">    
  44.         <div class="content">    
  45. <div class="row">    
  46.     <div class="auto-style2" style="margin-left:10%; height:100%" >    
  47.             
  48.     <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>    
  49.         <asp:GridView runat="server" AutoGenerateColumns="false" ID="products_gw" CellPadding="5" Class="text-center " ForeColor="Black" BackColor="#CCCCCC" BorderColor="#999999" BorderStyle="Solid" BorderWidth="3px" CellSpacing="5" Height="307px" Width="836px" CssClass="auto-style4" OnSelectedIndexChanged="products_gw_SelectedIndexChanged" OnRowDataBound="products_gw_RowDataBound" OnRowCancelingEdit="products_gw_RowCancelingEdit" OnRowDeleting="products_gw_RowDeleting" OnRowCommand="products_gw_RowCommand">    
  50.             <Columns>    
  51.       <asp:ImageField DataImageUrlField="img" />    
  52.              <%--   <asp:BoundField DataField="name" HeaderText="Product Name" />--%>    
  53.             <asp:TemplateField HeaderText="ProductID">    
  54.                 <ItemTemplate>    
  55.                     <asp:Label ID="lblProductID" runat="server"  Text='<%#Eval("id") %>'></asp:Label>    
  56.                 </ItemTemplate>    
  57.             </asp:TemplateField>    
  58.             <asp:TemplateField HeaderText="ProductName">    
  59.                 <ItemTemplate>    
  60.                     <asp:Label ID="lblProductName" runat="server" Text='<%#Eval("name") %>'></asp:Label>    
  61.                 </ItemTemplate>    
  62.             </asp:TemplateField>    
  63.             <asp:TemplateField HeaderText="Price">    
  64.                 <ItemTemplate>    
  65.                     <asp:Label ID="lblPrice" runat="server" Text='<%#Eval("price") %>'></asp:Label>    
  66.                 </ItemTemplate>    
  67.             </asp:TemplateField>    
  68.             <asp:TemplateField HeaderText="Quantity">    
  69.                 <ItemTemplate>    
  70.                     <asp:TextBox ID="txtQty" runat="server" onkeyup="CalculateTotals();"></asp:TextBox>    
  71.                 </ItemTemplate>    
  72.             </asp:TemplateField>    
  73.             <asp:TemplateField HeaderText="Total">    
  74.                 <ItemTemplate>    
  75.                     <asp:Label ID="lblTotal" runat="server" Text="0"></asp:Label>    
  76.                 </ItemTemplate>    
  77.             </asp:TemplateField>    
  78.             <asp:TemplateField HeaderText="Delete">    
  79.                 <ItemTemplate>    
  80.                     <asp:LinkButton ID="lnkDelete" runat="server" CommandName="Del" >Delete</asp:LinkButton>    
  81.                 </ItemTemplate>    
  82.             </asp:TemplateField>    
  83.         </Columns>  
  84.        <FooterStyle BackColor="#CCCCCC"></FooterStyle>  
  85.             <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White"></HeaderStyle>  
  86.             <PagerStyle HorizontalAlign="Left" BackColor="#CCCCCC" ForeColor="Black"></PagerStyle>  
  87.             <RowStyle BackColor="White" ></RowStyle>  
  88.             <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White"></SelectedRowStyle>  
  89.             <SortedAscendingCellStyle BackColor="#F1F1F1"></SortedAscendingCellStyle>  
  90.             <SortedAscendingHeaderStyle BackColor="#808080"></SortedAscendingHeaderStyle>  
  91.             <SortedDescendingCellStyle BackColor="#CAC9C9"></SortedDescendingCellStyle>  
  92.             <SortedDescendingHeaderStyle BackColor="#383838"></SortedDescendingHeaderStyle>    
  93.         </asp:GridView>    
  94.   <asp:Label ID="Cart" runat="server" Font-Size="X-Large" Text="Grand Total:"><asp:Label ID="lblSumTotal" runat="server"></asp:Label></asp:Label>          
  95.         </div>  
  96.     </div>    
  97.              <br />    
  98.      <div class="panel-footer ">    
  99.     <div style="margin-left:45%;" class="">    
  100.     <a href="checkout.aspx" Class="btn btn-success" style="width: 150px; height: 35px;">Proceed To Checkout</a><br />    
  101. </div>  
  102. </div>    
  103.             <br />    
  104.         </div>    
  105.     </div>    
  106. </asp:Content>    
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. using Jewelleryonlineshop.Models;  
  8. using System.Data.SqlClient;  
  9. using System.Data;  
  10. namespace Jewelleryonlineshop {  
  11.  public partial class cart_view: System.Web.UI.Page {  
  12.   static List < Products > cart_list = new List < Products > ();  
  13.   protected void Page_Load(object sender, EventArgs e) {  
  14.    if (!IsPostBack) {  
  15.     load_data();  
  16.    }  
  17.   }  
  18.   void load_data() {  
  19.    if (Session["cart"] != null) {  
  20.     List < Products > prods = Session["cart"as List < Products > ;  
  21.     // DataTable dt = (DataTable)Session["cart"];  
  22.     products_gw.DataSource = prods;  
  23.     products_gw.DataBind();  
  24.    }  
  25.   }  
  26.   protected void products_gw_SelectedIndexChanged(object sender, EventArgs e) {}  
  27.   protected void products_gw_RowDeleting(object sender, GridViewDeleteEventArgs e) {  
  28.    //List<Products> prods = Session["cart"] as List<Products>;  
  29.    //Session.Contents.Remove("cart");  
  30.    //Session["cart"] = prods;  
  31.    DataTable dt = new DataTable();  
  32.    //Session.Contents.Remove("cart");  
  33.    int index = products_gw.SelectedIndex;  
  34.    dt = (DataTable) Session["cart"as DataTable;  
  35.    //DataRow dr = dt.Rows[e.RowIndex];  
  36.    dt.Rows[index].Delete();  
  37.    dt.AcceptChanges();  
  38.    ////products_gw.EditIndex = -1;  
  39.    ////load_data();  
  40.    products_gw.DataSource = dt;  
  41.    products_gw.DataBind();  
  42.   }  
  43.   protected void products_gw_RowDataBound(object sender, GridViewRowEventArgs e) {  
  44.    //if (e.Row.RowType == DataControlRowType.DataRow)  
  45.    //{  
  46.    // string id = (e.Row.FindControl("lblProductID") as Label).Text;  
  47.    // foreach (LinkButton button in e.Row.Cells[2].Controls.OfType<LinkButton>())  
  48.    // {  
  49.    // if (button.CommandName == "Del")  
  50.    // {  
  51.    // button.Attributes["onclick"] = "if(!confirm('Do you want to delete " + id + "?')){ return false; };";  
  52.    // }  
  53.    // }  
  54.    //LinkButton lnkDelete = (LinkButton)e.Row.FindControl("lnkDelete");  
  55.    //if ((lnkDelete != null))  
  56.    //{  
  57.    // lnkDelete.Text = "Delete";  
  58.    // lnkDelete.CommandName = "Del";  
  59.    // lnkDelete.CommandArgument = e.Row.RowIndex.ToString();  
  60.    // lnkDelete.Attributes.Add("OnClick", "return confirm('Delete this row?');");  
  61.    //}  
  62.   }  
  63.   protected void products_gw_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) {  
  64.    products_gw.EditIndex = -1;  
  65.    load_data();  
  66.   }  
  67.   protected void products_gw_RowCommand(object sender, GridViewCommandEventArgs e) {  
  68.    //if (e.CommandName == "Del")  
  69.    //{  
  70.    // int RowsID = Convert.ToInt32(e.CommandArgument);  
  71.    // //List<Products> dt = null;  
  72.    // //Session.Contents.Remove("cart");  
  73.    // //dt = Session["cart"] as List<Products>;  
  74.    // Response.Redirect("cart_view.aspx");  
  75.    //}  
  76.   }  
  77.  }  
  78. }

Answers (3)