欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-03-30 21:56:57
一、在表item中添加字段descn、supplytime、supplydate、supplyarea。因为除descn,其它几个字段内容都差不多,所以,设置一下默认值,以后添加,...

一、在表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" %> 
<p> 
<: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> 
</p> 
<p><a href='items.aspx?categoryid=<%=request.querystring["categoryid"] %>'>< 返回</a></p> 
<%@ control language="c#" autoeventwireup="true" codefile="itemdetailscontrol.ascx.cs" inherits="westgarden.web.itemdetailscontrol" %>
<%@ register tagprefix="westgardencontrol" namespace="westgarden.web" %>
<p>
<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>
</p>
<p><a href='items.aspx?categoryid=<%=request.querystring["categoryid"] %>'>< 返回</a></p>
 

 

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