【问题标题】:How to pass the some of values from the textbox grid to another page如何将文本框网格中的一些值传递到另一个页面
【发布时间】: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" />
  &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");
    }

当转到下一页时,我将如何检索在文本框中输入的总金额并显示它?

【问题讨论】:

  • 您的标记与设计不同。您是否使用文本框来获取金额?
  • 您可以使用会话和其他状态管理技术维护页面之间的值。
  • 是的,我将在 gridview 的文本框中输入金额,并将所有值存储在会话中,当我单击结帐按钮时,我将转到下一页,我想要的是值的总和在文本框中添加
  • 这与MVC无关(标签已移除)
  • 你会在谷歌上找到很多例子。请参考这个code.tutsplus.com/tutorials/…

标签: asp.net c#-4.0


【解决方案1】:

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

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

我习惯于 vb.net,所以如果我的 C# 语法关闭,我深表歉意

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-04
    相关资源
    最近更新 更多