如何将一些值从文本框网格传递到另一个页面

时间:2015-06-24 09:36:36

标签: asp.net c#-4.0

form layout with textboxes for amounts

我需要在该部分制作一个这样的购物车系统表格我将添加金额,然后我选择复选框并在结账后将结帐到下一页的总和:

form layout with total price

我已经制作了这段代码:

<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" />
  &nbsp;&nbsp;&nbsp;&nbsp;
  <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");
    }

进入下一页时,如何检索文本框中输入的总金额并显示它?

1 个答案:

答案 0 :(得分:1)

在下一页的Page_Load事件中,您可以创建一个标签来显示总价格,然后您只需要将会话值分配给label.text

Label TotalPrice = default(Label);
TotalPrice.text = Session["Cart"].ToString

我用于vb.net,所以如果我的C#语法关闭,我道歉