【发布时间】:2015-06-25 05:32:44
【问题描述】:
我需要在该部分中制作一个这样的购物车系统表格,我将添加金额,然后我选中复选框,然后结帐,这些值将与总和一起转到下一页:
我已经制作了这段代码:
<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>
这是代码:
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");
}
当转到下一页时,我将如何检索在文本框中输入的总金额并显示它?
【问题讨论】:
-
您的标记与设计不同。您是否使用文本框来获取金额?
-
您可以使用会话和其他状态管理技术维护页面之间的值。
-
是的,我将在 gridview 的文本框中输入金额,并将所有值存储在会话中,当我单击结帐按钮时,我将转到下一页,我想要的是值的总和在文本框中添加
-
这与MVC无关(标签已移除)
-
你会在谷歌上找到很多例子。请参考这个code.tutsplus.com/tutorials/…