单击时如何将ListView项发送到另一个页面?

时间:2018-04-20 04:57:53

标签: c# html asp.net

我正在使用列出了多个项目的ListView,每个项目都有自己的购买按钮。现在,用户可以选择他们想要的物品(汽车)上的购买按钮,并将它们带到BuyCars.aspx页面,但没有任何东西显示所选项目(汽车)。当用户点击购买某辆车到BuyCars.aspx总页面时,我无法弄清楚如何发送数据。

我为代码量道歉,但这个问题让我感到疯狂。

这是我的代码。谢谢!

Cars.aspx



<%@ Page Title="" Language="C#" MasterPageFile="~/MasterBlaster024.Master" AutoEventWireup="True" CodeBehind="Cars.aspx.cs" Inherits="CarSales_REAL.WebForm2" %>
  <asp:Content ID="Content1" ContentPlaceHolderID="Main" runat="server">
  </asp:Content>
  <asp:Content ID="Content2" ContentPlaceHolderID="div2" runat="server">
    <div class="row">
      <asp:ListView ID="ListView1" runat="server" class="custom-class" DataSourceID="SqlDataSource1" OnItemCommand="ListView1_ItemCommand" DataKeyNames="CarID" OnSelectedIndexChanged="ListView1_SelectedIndexChanged">
        <ItemTemplate>
          <div class="col-sm-4">
            <div class="custom_class">
              <a href='/Shop/Order/<%# Eval("CarID") %>'>
                            <img src='/Images/<%# Eval("ImageCar") %>'
                                alt='<%# Eval("Name") %>' /></a>
              <div class="caption">
                <h3>
                  <%# Eval("Name") %>
                </h3>
                <p><b>Price: <%# Eval("CarPrice", "{0:c}") %></b>
                  <br><br>
                  <%# Eval("Long_Description") %>
                </p>
              </div>
              <%--<div class="row">
                            <div class ="form-group">
                                <label class="col-sm-1">Quantity:</label>
                            </div>     
                                                                               
                        </div>--%>
                <div class="col-sm-8">
                  <%--<asp:RequiredFieldValidator ID="RequiredFieldValidator1" CssClass="text-danger"
                            runat="server" ControlToValidate="txtQuantity1" Display="Dynamic" 
                            ErrorMessage="Quantity is a required field." ValidationGroup='<%# ListView1.ClientID + "_" + Container.DataItemIndex %>'></asp:RequiredFieldValidator>
                    <asp:RangeValidator ID="RangeValidator1" runat="server" CssClass="text-danger" ControlToValidate="txtQuantity1" Display="Dynamic" ErrorMessage="Quantity must range from 1 to 2." MaximumValue="2" MinimumValue="1" Type="Integer"></asp:RangeValidator>--%>

                </div>
                <div class="form-group">
                  <div class="col-sm-12">
                    <div class="col-sm-12">
                      <asp:Button ID="btnBuy" runat="server" Text="Buy Car" onclick="btnAdd_Click" CssClass="btn" ValidationGroup='<%# ListView1.ClientID + "_" + Container.DataItemIndex %>' />
                      <asp:Button ID="btnCart" runat="server" Text="See Selected Car" PostBackUrl="~/BuyCars.aspx" CausesValidation="False" CssClass="btn" />


                    </div>
                  </div>
                </div>
                <div>
                  <asp:Label ID="lblMessage" runat="server" EnableViewState="false" CssClass="text-info col-sm-12"></asp:Label>
                </div>
            </div>

          </div>
        </ItemTemplate>
      </asp:ListView>
      <%--<div class="col-sm-3">
                                <asp:TextBox ID="txtQuantity1" runat="server"                
                                CssClass="form-control"></asp:TextBox>
                            </div>--%>
    </div>

    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT [CarID], [Name], [Long_Description], [CarPrice], [ImageCar] FROM [Table] ORDER BY [Name]" OnSelecting="SqlDataSource1_Selecting"></asp:SqlDataSource>
  </asp:Content>
&#13;
&#13;
&#13;

Cars.aspx.cs

&#13;
&#13;
namespace CarSales_REAL
{
    public partial class WebForm2 : System.Web.UI.Page
    {
        private Product selectedProduct;

        

        protected void Page_Load(object sender, EventArgs e)
        {
            UnobtrusiveValidationMode = UnobtrusiveValidationMode.None;
            
            if (!IsPostBack) ListView1.DataBind();
            selectedProduct = this.GetSelectedProduct();
            

        }

        private Product GetSelectedProduct()
        {
            DataView productsTable = (DataView)
                SqlDataSource1.Select(DataSourceSelectArguments.Empty);
            //productsTable.RowFilter = "CarID = '" + ListView1.SelectedValue + "'";
            DataRowView row = productsTable[1];

            Product p = new Product();
            p.CardID = row["CarID"].ToString();
            p.Name = row["Name"].ToString();
            //p.Short_Description = row["Short_Description"].ToString();
            p.Long_description = row["long_Description"].ToString();
            p.CarPrice = (decimal)row["CarPrice"];
            //p.ImageFile = row["ImageFile"].ToString();
            return p;
        }

        protected void SqlDataSource1_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
        {
            
           
            
            
        }

        

        protected void btnAdd_Click(object sender, EventArgs e)

        {
            Button btnBuy = (Button)sender;
            if (Page.IsValid)
            {
                CartItemList cart = CartItemList.GetCart();
                CartItem cartItem = cart[selectedProduct.CardID];

                if (cartItem == null)
                {
                    //cart.AddItem(selectedProduct,
                    //                Convert.ToInt32(txtQuantity1.Text));
                }
                else
                {
                    //cartItem.AddQuantity(Convert.ToInt32(txtQuantity1.Text));
                }
                Response.Redirect("BuyCars.aspx");

            }
        }

        protected void btnCart_Click(object sender, EventArgs e)
        {
            
        }

        protected void ListView1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void ListView1_ItemCommand(object sender, ListViewCommandEventArgs e)
        {

        }
    }
}
&#13;
&#13;
&#13;

BuyCars.aspx

&#13;
&#13;
<%@ Page Title="" Language="C#" MasterPageFile="~/MasterBlaster024.Master" AutoEventWireup="True" CodeBehind="BuyCars.aspx.cs" Inherits="CarSales_REAL.WebForm3" %>
<%@ MasterType VirtualPath="~/MasterBlaster024.Master" %>
<asp:Content ID="Content1" ContentPlaceHolderID="Main" runat="server">
    
</asp:Content>
    
<asp:Content ID="Content2" ContentPlaceHolderID="div2" runat="server">
    <div class="row">
        <div class="col-sm-6">
            <div class="form-group">
                <div class="col-sm-12"><asp:ListBox ID="Cart1" runat="server" CssClass="form-control"></asp:ListBox></div>
            </div>
        </div>
        <div class="form-group">
            <div class="col-sm-12"><asp:Button ID="btnRemove" runat="server" 
                Text="Remove Item" OnClick="btnRemove_Click" CssClass="btn"/></div>
            <div class="form-group">
                <div class="col-sm-12"><asp:Button ID="btnEmpty" runat="server"
                    Text="Empty Cart" OnClick="btnEmpty_Click" CssClass="btn" />
                </div>
            </div>
        </div>
        <div class="row">
        <div class="col-sm-12">
            <div class="form-group">
                <asp:Label ID="lblMessage" runat="server" EnableViewState="false"
                    CssClass="text-info col-sm-12"></asp:Label>
            </div>
            <div class="form-group">
                <div class="col-sm-12">
                    <asp:Button ID="btnContinue" runat="server"
                        PostBackUrl="~/Cars.aspx" Text="Continue Shopping" CssClass="btn" />
                    <asp:Button ID="btnCheckOut" runat="server" Text="Check Out"  
                        onclick="btnCheckOut_Click" CssClass="btn" />
                </div>
            </div>

        </div>
        </div>

    
    </div>
</asp:Content>
&#13;
&#13;
&#13;

BuyCars.aspx.cs

&#13;
&#13;
namespace CarSales_REAL
{
    
    
        public partial class WebForm3 : System.Web.UI.Page
        {
            private CartItemList cart;

            
            protected void Page_Load(object sender, EventArgs e)
            {
                

                cart = CartItemList.GetCart();
                if (!IsPostBack)
                    this.DisplayCart();
            }

            private void DisplayCart()
            {

                Cart1.Items.Clear();
                CartItem item;

                for (int i = 0; i < cart.Count; i++)
                {
                    item = cart[i];
                    Cart1.Items.Add(item.Display());
                }
            }

            protected void btnRemove_Click(object sender, EventArgs e)
            {
                if (cart.Count > 0)
                {
                    if (Cart1.SelectedIndex > -1)
                    

                    
                    {
                        cart.RemoveAt(Cart1.SelectedIndex);
                        this.DisplayCart();
                    }
                    else
                    {
                        lblMessage.Text = "Please select the item you want to remove.";
                    }
                }
            }

            protected void btnEmpty_Click(object sender, EventArgs e)
            {
                if (cart.Count > 0)
                {
                    cart.Clear();
                    Cart1.Items.Clear();
                }
            }

            protected void btnCheckOut_Click(object sender, EventArgs e)
            {
                lblMessage.Text = "Sorry, that function hasn't been implemented yet.";
            }
        }
    
}
&#13;
&#13;
&#13;

0 个答案:

没有答案