TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Umm Hani
NA
103
3.9k
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
<%@ Page Title=
""
Language=
"C#"
MasterPageFile=
"~/Web.Master"
AutoEventWireup=
"true"
CodeBehind=
"cart_view.aspx.cs"
Inherits=
"Jewelleryonlineshop.cart_view"
%>
<asp:Content ID=
"Content1"
ContentPlaceHolderID=
"head"
runat=
"server"
>
<link rel=
"stylesheet"
href=
"https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css"
/>
<script src=
"https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"
></script>
<script src=
"https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"
></script>
<style type=
"text/css"
>
.auto-style2 {
position: relative;
min-height: 1px;
float
: left;
width: -2147483648%;
left: -10px;
top: 7px;
height: 320px;
padding-left: 15px;
padding-right: 15px;
text-align: center;
}
.auto-style4 {
margin-right: 31;
}
</style>
<script type=
"text/javascript"
>
function
CalculateTotals() {
var
gv = document.getElementById(
"<%= products_gw.ClientID %>"
);
var
rows = gv.getElementsByTagName(
"tr"
);
var
grandTotal = 0;
for
(
var
i = 0; i < rows.length; i++) {
var
tds = rows[i].getElementsByTagName(
"td"
);
if
(rows[i].getElementsByTagName(
"td"
).length > 0) {
var
price = rows[i].getElementsByTagName(
"span"
)[2].innerHTML;
var
quantity = rows[i].getElementsByTagName(
"input"
)[0].value;
var
total = parseFloat(price) * parseFloat(quantity);
rows[i].getElementsByTagName(
"span"
)[3].innerHTML = isNaN(total) ? 0 : total;
grandTotal += isNaN(total) ? 0 : total;
}
}
document.getElementById(
"<%= lblSumTotal.ClientID %>"
).innerHTML = grandTotal;
}
</script>
</asp:Content>
<asp:Content ID=
"Content2"
ContentPlaceHolderID=
"ContentPlaceHolder1"
runat=
"server"
>
<div
class
=
"container"
>
<div
class
=
"content"
>
<div
class
=
"row"
>
<div
class
=
"auto-style2"
style=
"margin-left:10%; height:100%"
>
<asp:Label ID=
"Label1"
runat=
"server"
Text=
"Label"
></asp:Label>
<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"
>
<Columns>
<asp:ImageField DataImageUrlField=
"img"
/>
<%-- <asp:BoundField DataField=
"name"
HeaderText=
"Product Name"
/>--%>
<asp:TemplateField HeaderText=
"ProductID"
>
<ItemTemplate>
<asp:Label ID=
"lblProductID"
runat=
"server"
Text=
'<%#Eval("id") %>'
></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText=
"ProductName"
>
<ItemTemplate>
<asp:Label ID=
"lblProductName"
runat=
"server"
Text=
'<%#Eval("name") %>'
></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText=
"Price"
>
<ItemTemplate>
<asp:Label ID=
"lblPrice"
runat=
"server"
Text=
'<%#Eval("price") %>'
></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText=
"Quantity"
>
<ItemTemplate>
<asp:TextBox ID=
"txtQty"
runat=
"server"
onkeyup=
"CalculateTotals();"
></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText=
"Total"
>
<ItemTemplate>
<asp:Label ID=
"lblTotal"
runat=
"server"
Text=
"0"
></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText=
"Delete"
>
<ItemTemplate>
<asp:LinkButton ID=
"lnkDelete"
runat=
"server"
CommandName=
"Del"
>Delete</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor=
"#CCCCCC"
></FooterStyle>
<HeaderStyle BackColor=
"Black"
Font-Bold=
"True"
ForeColor=
"White"
></HeaderStyle>
<PagerStyle HorizontalAlign=
"Left"
BackColor=
"#CCCCCC"
ForeColor=
"Black"
></PagerStyle>
<RowStyle BackColor=
"White"
></RowStyle>
<SelectedRowStyle BackColor=
"#000099"
Font-Bold=
"True"
ForeColor=
"White"
></SelectedRowStyle>
<SortedAscendingCellStyle BackColor=
"#F1F1F1"
></SortedAscendingCellStyle>
<SortedAscendingHeaderStyle BackColor=
"#808080"
></SortedAscendingHeaderStyle>
<SortedDescendingCellStyle BackColor=
"#CAC9C9"
></SortedDescendingCellStyle>
<SortedDescendingHeaderStyle BackColor=
"#383838"
></SortedDescendingHeaderStyle>
</asp:GridView>
<asp:Label ID=
"Cart"
runat=
"server"
Font-Size=
"X-Large"
Text=
"Grand Total:"
><asp:Label ID=
"lblSumTotal"
runat=
"server"
></asp:Label></asp:Label>
</div>
</div>
<br />
<div
class
=
"panel-footer "
>
<div style=
"margin-left:45%;"
class
=
""
>
<a href=
"checkout.aspx"
Class=
"btn btn-success"
style=
"width: 150px; height: 35px;"
>Proceed To Checkout</a><br />
</div>
</div>
<br />
</div>
</div>
</asp:Content>
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Web;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
Jewelleryonlineshop.Models;
using
System.Data.SqlClient;
using
System.Data;
namespace
Jewelleryonlineshop {
public
partial
class
cart_view: System.Web.UI.Page {
static
List < Products > cart_list =
new
List < Products > ();
protected
void
Page_Load(
object
sender, EventArgs e) {
if
(!IsPostBack) {
load_data();
}
}
void
load_data() {
if
(Session[
"cart"
] !=
null
) {
List < Products > prods = Session[
"cart"
]
as
List < Products > ;
// DataTable dt = (DataTable)Session["cart"];
products_gw.DataSource = prods;
products_gw.DataBind();
}
}
protected
void
products_gw_SelectedIndexChanged(
object
sender, EventArgs e) {}
protected
void
products_gw_RowDeleting(
object
sender, GridViewDeleteEventArgs e) {
//List<Products> prods = Session["cart"] as List<Products>;
//Session.Contents.Remove("cart");
//Session["cart"] = prods;
DataTable dt =
new
DataTable();
//Session.Contents.Remove("cart");
int
index = products_gw.SelectedIndex;
dt = (DataTable) Session[
"cart"
]
as
DataTable;
//DataRow dr = dt.Rows[e.RowIndex];
dt.Rows[index].Delete();
dt.AcceptChanges();
////products_gw.EditIndex = -1;
////load_data();
products_gw.DataSource = dt;
products_gw.DataBind();
}
protected
void
products_gw_RowDataBound(
object
sender, GridViewRowEventArgs e) {
//if (e.Row.RowType == DataControlRowType.DataRow)
//{
// string id = (e.Row.FindControl("lblProductID") as Label).Text;
// foreach (LinkButton button in e.Row.Cells[2].Controls.OfType<LinkButton>())
// {
// if (button.CommandName == "Del")
// {
// button.Attributes["onclick"] = "if(!confirm('Do you want to delete " + id + "?')){ return false; };";
// }
// }
//LinkButton lnkDelete = (LinkButton)e.Row.FindControl("lnkDelete");
//if ((lnkDelete != null))
//{
// lnkDelete.Text = "Delete";
// lnkDelete.CommandName = "Del";
// lnkDelete.CommandArgument = e.Row.RowIndex.ToString();
// lnkDelete.Attributes.Add("OnClick", "return confirm('Delete this row?');");
//}
}
protected
void
products_gw_RowCancelingEdit(
object
sender, GridViewCancelEditEventArgs e) {
products_gw.EditIndex = -1;
load_data();
}
protected
void
products_gw_RowCommand(
object
sender, GridViewCommandEventArgs e) {
//if (e.CommandName == "Del")
//{
// int RowsID = Convert.ToInt32(e.CommandArgument);
// //List<Products> dt = null;
// //Session.Contents.Remove("cart");
// //dt = Session["cart"] as List<Products>;
// Response.Redirect("cart_view.aspx");
//}
}
}
}
Reply
Answers (
3
)
Azure IOT edge fails to create Visual Studio 2019
Csharp async await feature