Reputation: 113
I need to make a cart system form like this in that section I will add the amount and then I select the checkbox and will checkout after that values will go to the next page with sum:
I have made this code:
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<asp:GridView ID="Basket" runat="server" AutoGenerateColumns="False"
GridLines="None" EnableViewState="False" ShowFooter="True"
DataKeyNames="ProductID" OnRowCreated="Basket_RowCreated">
<Columns>
<asp:TemplateField HeaderText="Remove">
<ItemTemplate>
<asp:CheckBox ID="RemovedProducts" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Donation" SortExpression="ProductName">
<ItemTemplate>
<asp:Label ID="ProductName" runat="server" Text='<%# Eval("ProductName") %>' />
</ItemTemplate>
<FooterTemplate>
<strong>
Total Price:
</strong>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Amount" SortExpression="UnitPrice">
<ItemTemplate>
<%# Eval("UnitPrice")%> aed
</ItemTemplate>
<FooterTemplate>
<strong>
<asp:Literal ID="TotalPrice" runat="server" /> AED
</strong>
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button ID="RemoveDonation" runat="server"
Text="Remove From Basket" OnClick="RemoveProduct_Click" />
<asp:Button ID="ConfirmPurchase" runat="server" Text="Confirm Donation" />
<asp:SqlDataSource ID="BasketData" runat="server"
ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>">
</asp:SqlDataSource>
</asp:Content>
This is the code:
protected void AddToCart_Click(object sender, EventArgs e)
{
var selectedProducts = Products.Rows.Cast<GridViewRow>()
.Where(row => ((CheckBox)row.FindControl("SelectedProducts")).Checked)
.Select(row => Products.DataKeys[row.RowIndex].Value.ToString()).ToList();
if (Session["Cart"] == null)
{
Session["Cart"] = selectedProducts;
}
else
{
var cart = (List<string>)Session["Cart"];
foreach (var product in selectedProducts)
cart.Add(product);
Session["Cart"] = cart;
}
foreach (GridViewRow row in Products.Rows)
{
CheckBox cb = (CheckBox)row.FindControl("SelectedProducts");
if (cb.Checked)
cb.Checked = false;
}
}
protected void Checkout_Click(object sender, EventArgs e)
{
if (Session["Cart"] != null)
Response.Redirect("Checkout.aspx");
}
When go to the next page how will I retrieve the total amount entered in the textbox and display it?
Upvotes: 0
Views: 767
Reputation: 384
In the Page_Load event of your next page you can create a label to display the total price, then you just need to assign the session value to label.text
Label TotalPrice = default(Label);
TotalPrice.text = Session["Cart"].ToString
I am use to vb.net so if my C# syntax is off I apologize
Upvotes: 1