网上购物系统(Task007)——FormView显示商品详细信息

来源:岁月联盟 编辑:exp 时间:2012-05-12

一、在数据库表Item中添加字段Descn、SupplyTime、SupplyDate、SupplyArea。因为除Descn,其它几个字段内容都差不多,所以,设置一下默认值,以后添加,更新数据库内容时,会更方便。二、在数据集Model类库中添加类ItemDetails.cs。

[csharp]  using System; 
 
namespace WestGarden.Model 

    public class ItemDetails 
    { 
        private int itemid; 
        private string categoryid; 
        private string name; 
        private decimal price; 
        private string image; 
        private string categoryname; 
 
        private string descn; 
        private string supplytime; 
        private string supplydate; 
        private string supplyarea; 
 
        public ItemDetails() { } 
 
        public ItemDetails(int itemid, string categoryid, string name, decimal price, string image, string categoryname, 
            string descn, string supplytime,string supplydate,string supplyarea) 
        { 
            this.itemid = itemid; 
            this.categoryid = categoryid; 
            this.name = name; 
            this.price = price; 
            this.image = image; 
            this.categoryname = categoryname; 
 
            this.descn = descn; 
            this.supplytime = supplytime; 
            this.supplydate = supplydate; 
            this.supplyarea = supplyarea; 
        } 
 
        public int ItemId 
        { 
            get { return itemid; } 
            set { itemid = value; } 
        } 
        public string CategoryId 
        { 
            get { return categoryid; } 
            set { categoryid = value; } 
        } 
        public string Name 
        { 
            get { return name; } 
            set { name = value; } 
        } 
        public decimal Price 
        { 
            get { return price; } 
            set { price = value; } 
        } 
        public string Image 
        { 
            get { return image; } 
            set { image = value; } 
        } 
        public string CategoryName 
        { 
            get { return categoryname; } 
            set { categoryname = value; } 
        } 
 
        public string Descn 
        { 
            get { return descn; } 
            set { descn = value; } 
        } 
        public string SupplyTime 
        { 
            get { return supplytime; } 
            set { supplytime = value; } 
        } 
        public string SupplyDate 
        { 
            get { return supplydate; } 
            set { supplydate = value; } 
        } 
        public string SupplyArea 
        { 
            get { return supplyarea; } 
            set { supplyarea = value; } 
        } 
    } 

using System;

namespace WestGarden.Model
{
    public class ItemDetails
    {
        private int itemid;
        private string categoryid;
        private string name;
        private decimal price;
        private string image;
        private string categoryname;

        private string descn;
        private string supplytime;
        private string supplydate;
        private string supplyarea;

        public ItemDetails() { }

        public ItemDetails(int itemid, string categoryid, string name, decimal price, string image, string categoryname,
            string descn, string supplytime,string supplydate,string supplyarea)
        {
            this.itemid = itemid;
            this.categoryid = categoryid;
            this.name = name;
            this.price = price;
            this.image = image;
            this.categoryname = categoryname;

            this.descn = descn;
            this.supplytime = supplytime;
            this.supplydate = supplydate;
            this.supplyarea = supplyarea;
        }

        public int ItemId
        {
            get { return itemid; }
            set { itemid = value; }
        }
        public string CategoryId
        {
            get { return categoryid; }
            set { categoryid = value; }
        }
        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        public decimal Price
        {
            get { return price; }
            set { price = value; }
        }
        public string Image
        {
            get { return image; }
            set { image = value; }
        }
        public string CategoryName
        {
            get { return categoryname; }
            set { categoryname = value; }
        }

        public string Descn
        {
            get { return descn; }
            set { descn = value; }
        }
        public string SupplyTime
        {
            get { return supplytime; }
            set { supplytime = value; }
        }
        public string SupplyDate
        {
            get { return supplydate; }
            set { supplydate = value; }
        }
        public string SupplyArea
        {
            get { return supplyarea; }
            set { supplyarea = value; }
        }
    }
}
 

 

三、在数据访问层DAL的Item.cs类中添加函数GetItemDetailsByItemId()

[csharp] public IList<ItemDetails> GetItemDetailsByItemId(int ItemId) 

 
    IList<ItemDetails> itemDetailsByItemId = new List<ItemDetails>(); 
 
    SqlParameter parm = new SqlParameter(PARM_ITEM_ID, SqlDbType.Int); 
    parm.Value = ItemId; 
 
    using (SqlDataReader rdr = SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, SQL_SELECT_ITEMDETAILS_BY_CATEGORY, parm)) 

            while (rdr.Read()) 
            { 
                ItemDetails item = new ItemDetails(rdr.GetInt32(0), rdr.GetString(1), rdr.GetString(2), rdr.GetDecimal(3), rdr.GetString(4), rdr.GetString(5),rdr.GetString(6),rdr.GetString(7),rdr.GetString(8),rdr.GetString(9)); 
                itemDetailsByItemId.Add(item); 
        } 
    } 
    return itemDetailsByItemId; 

public IList<ItemDetails> GetItemDetailsByItemId(int ItemId)
{

    IList<ItemDetails> itemDetailsByItemId = new List<ItemDetails>();

    SqlParameter parm = new SqlParameter(PARM_ITEM_ID, SqlDbType.Int);
    parm.Value = ItemId;

    using (SqlDataReader rdr = SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, SQL_SELECT_ITEMDETAILS_BY_CATEGORY, parm))
{
      while (rdr.Read())
      {
          ItemDetails item = new ItemDetails(rdr.GetInt32(0), rdr.GetString(1), rdr.GetString(2), rdr.GetDecimal(3), rdr.GetString(4), rdr.GetString(5),rdr.GetString(6),rdr.GetString(7),rdr.GetString(8),rdr.GetString(9));
             itemDetailsByItemId.Add(item);
        }
    }
    return itemDetailsByItemId;
}
 

 

四、Controls中添加用户控件ItemDetailsControl.ascx

1、前台代码:

[html] <%@ Control Language="C#" AutoEventWireup="true" CodeFile="ItemDetailsControl.ascx.cs" Inherits="WestGarden.Web.ItemDetailsControl" %> 
<%@ Register TagPrefix="WestGardenControl" Namespace="WestGarden.Web" %> 
<div> 
<asp:FormView ID="fvwItemDetails" runat="server"> 
    <ItemTemplate> 
        <table cellpadding="0" cellspacing="0"> 
            <tr> 
                <td height="18" width="18"> 
                </td> 
                <td height="18" width="96"> 
                </td> 
                <td height="18" width="18"> 
                </td> 
                <td height="18" width="96"> 
                </td> 
                <td height="18" width="180"> 
                </td> 
            </tr> 
            <tr> 
                <td width="18"> 
                </td> 
                <td rowspan="7" width="96"> 
                    <asp:Image Width="144" Height="144" ID="imgItem" ImageUrl='<%# Eval("Image") %>' 
                        AlternateText='<%# Eval("Name") %>' runat="server" /></td> 
                <td width="18"> 
                </td> 
                <td width="96"> 
                    商品类别:</td> 
                <td width="180"> 
                    <%# Eval("CategoryName") %> 
                </td> 
            </tr> 
            <tr> 
                <td width="18"> 
                </td> 
                <td width="18"> 
                </td> 
                <td width="96"> 
                    商品名称:</td> 
                <td width="180"> 
                    <%# Eval("Name") %> 
                </td> 
            </tr> 
            <tr> 
                <td width="18"> 
                </td> 
                <td width="18"> 
                </td> 
                <td width="96"> 
                    商品价格:</td> 
                <td width="180"> 
                    <%# Eval("Price") %> 
                </td> 
            </tr> 
            <tr> 
                <td width="18"> 
                </td> 
                <td width="18"> 
                </td> 
                <td width="96"> 
                    商品描述:</td> 
                <td width="180"> 
                    <%# Eval("Descn") %> 
                </td> 
            </tr> 
            <tr> 
                <td width="18"> 
                </td> 
                <td width="18"> 
                </td> 
                <td width="96"> 
                    供应时间:</td> 
                <td width="180"> 
                    <%# Eval("SupplyTime") %> 
                </td> 
            </tr> 
            <tr> 
                <td width="18"> 
                </td> 
                <td width="18"> 
                </td> 
                <td width="96"> 
                    供应日期:</td> 
                <td width="180"> 
                    <%# Eval("SupplyDate") %> 
                </td> 
            </tr> 
            <tr> 
                <td width="18"> 
                </td> 
                <td width="18"> 
                </td> 
                <td width="96"> 
                    供应地区:</td> 
                <td width="180"> 
                    <%# Eval("SupplyArea") %> 
                </td> 
            </tr> 
            <tr> 
                <td height="18" width="18"> 
                </td> 
                <td height="18" width="96"> 
                </td> 
                <td height="18" width="18"> 
                </td> 
                <td height="18" width="96"> 
                </td> 
                <td height="18" width="180"> 
                </td> 
            </tr> 
        </table> 
    </ItemTemplate> 
</asp:FormView> 
</div> 
<div><a href='Items.aspx?categoryId=<%=Request.QueryString["categoryId"] %>'>< 返回</a></div> 
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="ItemDetailsControl.ascx.cs" Inherits="WestGarden.Web.ItemDetailsControl" %>
<%@ Register TagPrefix="WestGardenControl" Namespace="WestGarden.Web" %>
<div>
<asp:FormView ID="fvwItemDetails" runat="server">
    <ItemTemplate>
        <table cellpadding="0" cellspacing="0">
            <tr>
                <td height="18" width="18">
                </td>
                <td height="18" width="96">
                </td>
                <td height="18" width="18">
                </td>
                <td height="18" width="96">
                </td>
                <td height="18" width="180">
                </td>
            </tr>
            <tr>
                <td width="18">
                </td>
                <td rowspan="7" width="96">
                    <asp:Image Width="144" Height="144" ID="imgItem" ImageUrl='<%# Eval("Image") %>'
                        AlternateText='<%# Eval("Name") %>' runat="server" /></td>
                <td width="18">
                </td>
                <td width="96">
                    商品类别:</td>
                <td width="180">
                    <%# Eval("CategoryName") %>
                </td>
            </tr>
            <tr>
                <td width="18">
                </td>
                <td width="18">
                </td>
                <td width="96">
                    商品名称:</td>
                <td width="180">
                    <%# Eval("Name") %>
                </td>
            </tr>
            <tr>
                <td width="18">
                </td>
                <td width="18">
                </td>
                <td width="96">
                    商品价格:</td>
                <td width="180">
                    <%# Eval("Price") %>
                </td>
            </tr>
            <tr>
                <td width="18">
                </td>
                <td width="18">
                </td>
                <td width="96">
                    商品描述:</td>
                <td width="180">
                    <%# Eval("Descn") %>
                </td>
            </tr>
            <tr>
                <td width="18">
                </td>
                <td width="18">
                </td>
                <td width="96">
                    供应时间:</td>
                <td width="180">
                    <%# Eval("SupplyTime") %>
                </td>
            </tr>
            <tr>
                <td width="18">
                </td>
                <td width="18">
                </td>
                <td width="96">
                    供应日期:</td>
                <td width="180">
                    <%# Eval("SupplyDate") %>
                </td>
            </tr>
            <tr>
                <td width="18">
                </td>
                <td width="18">
                </td>
                <td width="96">
                    供应地区:</td>
                <td width="180">
                    <%# Eval("SupplyArea") %>
                </td>
            </tr>
            <tr>
                <td height="18" width="18">
                </td>
                <td height="18" width="96">
                </td>
                <td height="18" width="18">
                </td>
                <td height="18" width="96">
                </td>
                <td height="18" width="180">
                </td>
            </tr>
        </table>
    </ItemTemplate>
</asp:FormView>
</div>
<div><a href='Items.aspx?categoryId=<%=Request.QueryString["categoryId"] %>'>< 返回</a></div>
 

 

2、后台代码

[csharp] using System; 
using System.Web; 
using System.Web.UI.WebControls; 
using WestGarden.DAL; 
 
namespace WestGarden.Web 

    public partial class ItemDetailsControl : System.Web.UI.UserControl 
    { 
        protected void Page_Load(object sender, EventArgs e) 
        { 
            int itemKey = int.Parse(Request.QueryString["itemId"]); 
 
            Item item = new Item(); 
            fvwItemDetails.DataSource = item.GetItemDetailsByItemId(itemKey); 
            fvwItemDetails.DataBind(); 
        } 
    } 

using System;
using System.Web;
using System.Web.UI.WebControls;
using WestGarden.DAL;

namespace WestGarden.Web
{
    public partial class ItemDetailsControl : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            int itemKey = int.Parse(Request.QueryString["itemId"]);

            Item item = new Item();
            fvwItemDetails.DataSource = item.GetItemDetailsByItemId(itemKey);
            fvwItemDetails.DataBind();
        }
    }
}
 

 

五、Web中添加窗体ItemDetails.aspx并选择母版页MasterPage.master,拖入用户控件ItemDetailsControl.ascx,并在Page_Load()函数中添加代码:

[csharp] protected void Page_Load(object sender, EventArgs e) 

    Page.Title = WebUtility.GetCategoryName(Request.QueryString["categoryId"]); 

 


作者 yousuos