单击“添加”按钮时,gridview将链接到不同的页面

时间:2012-10-29 05:29:17

标签: asp.net

这是我在这个论坛上的第二篇文章。 我很难找到一种方法,当我点击添加按钮时,它将链接到具有来自零件页面的相同项目信息的汽车页面。请检查以下代码: 这是项目页面的布局:

<%@ 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>

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

这是该页面的代码:

  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?
    }


}
}

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

购物车代码:

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;
    }
}

}

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

购物车页面:

<%@ 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>

1 个答案:

答案 0 :(得分: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>

只需将PartID绑定到PostBackUrl,然后您就可以获取PartID页面上已点击的Pages/Cart/Default.aspx,而不仅仅是从数据库中选择值PartID你很高兴。