0

this is my 2nd post in this forum. I am having a difficulty to find a way that when i click the add button it will link to the car page with the same item info from the part page. please check the code below: This is the layout of the item page:

<%@ Page Title="Parts" Language="C#" MasterPageFile="~/Site1.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Carshop.Pages.Parts.Default"%>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server"></asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
Parts
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
    <Columns>
        <asp:BoundField HeaderText="PartID" DataField="PartID"></asp:BoundField>
        <asp:BoundField HeaderText="ItemName" DataField="ItemName"></asp:BoundField >
        <asp:BoundField HeaderText="ItemDesciption" DataField="ItemDesciption"></asp:BoundField >
        <asp:BoundField HeaderText="Company" DataField="Company"></asp:BoundField >
        <asp:BoundField HeaderText="Price" DataField="Price" DataFormatString="{0:c}" ></asp:BoundField >
        <asp:ImageField HeaderText="Images" DataImageUrlField="Images"></asp:ImageField>     
        <asp:BoundField HeaderText="Active" DataField="Active"></asp:BoundField >
        <asp:TemplateField>
            <ItemTemplate>
                <asp:Button ID="Button1"  runat="server" Text="Add" PostBackUrl="~/Pages/Cart/Default.aspx"></asp:Button>                   
            </ItemTemplate>
        </asp:TemplateField> 
    </Columns>
</asp:GridView>

-------------------------------------------------------------

this is the code behide of the page:

  namespace Carshop.Pages.Parts
{
    public partial class Default : System.Web.UI.Page
    {
    protected void Page_Load(object sender, EventArgs e)
    {
        using (DBEntities entities = new DBEntities())
        {
            GridView1.DataSource = entities.tblParts.ToList();
            GridView1.DataBind();
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
      // what method should I code here?
    }


}
}

--------------------------------------------------------------

shopping cart codes:

namespace Carshop.Entities
{
//Sealed to stop unwelcome dependencies
public sealed class Shoppingcart
   {
    //singleton is a adesgin pattern
    //Only one object can be created
    private static Shoppingcart instance;

    //Private Data Members
    private int PartID { get; set; }
    private string ItemName { get; set; }
    private string ItemDesciptiopn { get; set; }

    private Shoppingcart() { }

    public static Shoppingcart SetShoppingcart (string _ItemName, string _ItemDesciption)
    {
        if (instance == null)
        {
            instance = new Shoppingcart();
            instance.ItemName = _ItemName;
            instance.ItemDesciptiopn = _ItemDesciption;
        }

        return instance;
    }
}

}

-----------------------------------------------

cart page:

<%@ Page Title="" Language="C#" MasterPageFile="~/Site1.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Carshop.Pages.Cart.Default" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server"></asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
Cart
<asp:GridView ID="GridView1" runat="server"></asp:GridView>
</asp:Content>
4

1 回答 1

0

如果我理解了您的问题,那么当用户单击位于网格视图中的按钮时,您可能希望提供从一页到另一页的值,这可能会对您有所帮助。

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
    <Columns>
        <asp:BoundField HeaderText="PartID" DataField="PartID"></asp:BoundField>
        <asp:BoundField HeaderText="ItemName" DataField="ItemName"></asp:BoundField >
        <asp:BoundField HeaderText="ItemDesciption" DataField="ItemDesciption"></asp:BoundField >
        <asp:BoundField HeaderText="Company" DataField="Company"></asp:BoundField >
        <asp:BoundField HeaderText="Price" DataField="Price" DataFormatString="{0:c}" ></asp:BoundField >
        <asp:ImageField HeaderText="Images" DataImageUrlField="Images"></asp:ImageField>     
        <asp:BoundField HeaderText="Active" DataField="Active"></asp:BoundField >
        <asp:TemplateField>
            <ItemTemplate>
                <asp:Button ID="Button1"  
               runat="server" Text="Add" 
               PostBackUrl='<%# "~/Pages/Cart/Default.aspx?PartID="+Eval("TempID") %>'/>
            </ItemTemplate>
        </asp:TemplateField> 
    </Columns>
</asp:GridView>

只需将其绑定PartIDPostBackUrl然后您就可以获得PartID已在您的Pages/Cart/Default.aspx页面上单击的内容,而不仅仅是从数据库中选择值,PartID您就可以开始了。

于 2012-10-29T05:42:38.003 回答