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

网上购物系统(Task004)——数据访问层DAL

程序员文章站 2022-04-19 23:23:37
一、添加类库dal,在dal中添加类category.cs,并在类库中添加函数getcategories(),代码如下:   using system; using...

一、添加类库dal,在dal中添加类category.cs,并在类库中添加函数getcategories(),代码如下:

 

using system;
using system.data;
using system.collections.generic;
using system.text;

using system.data.sqlclient;
using westgarden.model;
using westgarden.dbutility;

namespace westgarden.dal
{
    public class category
    {
        // static constants
        private const string sql_select_categories = "select categoryid, name, descn from category";
       
        /// <summary>
        /// method to get all categories
        /// </summary>      
        public ilist<categoryinfo> getcategories()
        {

            ilist<categoryinfo> categories = new list<categoryinfo>();

            //execute a query to read the categories
            using (sqldatareader rdr = sqlhelper.executereader(sqlhelper.connectionstringlocaltransaction, commandtype.text, sql_select_categories, null))
            {
                while (rdr.read())
                {
                    categoryinfo cat = new categoryinfo(rdr.getstring(0), rdr.getstring(1), rdr.getstring(2));
                    categories.add(cat);
                }
            }
            return categories;
        }


    }
}


函数getcategories()要在sqlhelper中读取连接字符串,因此,需要在sqlhelper.cs中添加代码:

public static readonly string connectionstringlocaltransaction = configurationmanager.connectionstrings["netshopconnstring"].connectionstring;

注意添加引用system.configuration。

二、使用用户控件

在web中新建文件夹controls并在其中添加用户控件navigationcontrol.ascx,窗体页和代码页代码分别如下:

[html] <%@ control language="c#" autoeventwireup="true" codefile="navigationcontrol.ascx.cs" inherits="westgarden.web.navigationcontrol" %> 
<%@ outputcache duration="100000" varybyparam="*" %> 
 
<:repeater id="repcategories" runat="server"> 
<headertemplate> 
<table cellspacing="0" border="0" style="border-collapse: collapse;"> 
</headertemplate> 
<itemtemplate> 
<tr> 
<td class="<%= controlstyle %>"><asp:hyperlink runat="server" id="lnkcategory"  navigateurl='<%# string.format("~/items.aspx?page=0&categoryid={0}", eval("categoryid")) %>' text='<%# eval("name") %>' /><asp:hiddenfield runat="server" id="hidcategoryid" value='<%# eval("categoryid") %>' /></td> 
</tr> 
</itemtemplate> 
<footertemplate> 
</table> 
</footertemplate> 
</asp:repeater> 
<%@ control language="c#" autoeventwireup="true" codefile="navigationcontrol.ascx.cs" inherits="westgarden.web.navigationcontrol" %>
<%@ outputcache duration="100000" varybyparam="*" %>

<asp:repeater id="repcategories" runat="server">
<headertemplate>
<table cellspacing="0" border="0" style="border-collapse: collapse;">
</headertemplate>
<itemtemplate>
<tr>
<td class="<%= controlstyle %>"><asp:hyperlink runat="server" id="lnkcategory"  navigateurl='<%# string.format("~/items.aspx?page=0&categoryid={0}", eval("categoryid")) %>' text='<%# eval("name") %>' /><asp:hiddenfield runat="server" id="hidcategoryid" value='<%# eval("categoryid") %>' /></td>
</tr>
</itemtemplate>
<footertemplate>
</table>
</footertemplate>
</asp:repeater>
 

 

[csharp] using system; 
using system.web.ui.webcontrols; 
using westgarden.dal; 
 
namespace westgarden.web { 
    public partial class navigationcontrol : system.web.ui.usercontrol { 
                
        private string controlstyle; 
 
        protected string controlstyle { 
            get { return controlstyle; } 
        } 
     
        protected void getcontrolstyle() { 
            if (request.servervariables["script_name"].tolower().indexof("default.aspx") > 0) 
                controlstyle = "navigationlinks"; 
            else 
                controlstyle = "mainnavigation"; 
        } 
        
 
        protected void page_load(object sender, eventargs e) { 
            getcontrolstyle(); 
            bindcategories(); 
 
            string categoryid = request.querystring["categoryid"]; 
            if (!string.isnullorempty(categoryid)) 
                selectcategory(categoryid); 
        } 
 
        private void selectcategory(string categoryid) { 
            foreach (repeateritem item in repcategories.items) { 
                hiddenfield hidcategoryid = (hiddenfield)item.findcontrol("hidcategoryid"); 
                if(hidcategoryid.value.tolower() == categoryid.tolower()) { 
                    hyperlink lnkcategory = (hyperlink)item.findcontrol("lnkcategory"); 
                    lnkcategory.forecolor = system.drawing.color.fromargb(199, 116, 3); 
                    break; 
                } 
            } 
        } 
 
        private void bindcategories() { 
            category category = new category(); 
            repcategories.datasource = category.getcategories(); 
            repcategories.databind();             
        } 
    } 

using system;
using system.web.ui.webcontrols;
using westgarden.dal;

namespace westgarden.web {
    public partial class navigationcontrol : system.web.ui.usercontrol {
              
        private string controlstyle;

        protected string controlstyle {
            get { return controlstyle; }
        }
 
        protected void getcontrolstyle() {
            if (request.servervariables["script_name"].tolower().indexof("default.aspx") > 0)
                controlstyle = "navigationlinks";
            else
                controlstyle = "mainnavigation";
        }
      

        protected void page_load(object sender, eventargs e) {
            getcontrolstyle();
            bindcategories();

            string categoryid = request.querystring["categoryid"];
            if (!string.isnullorempty(categoryid))
                selectcategory(categoryid);
        }

        private void selectcategory(string categoryid) {
            foreach (repeateritem item in repcategories.items) {
                hiddenfield hidcategoryid = (hiddenfield)item.findcontrol("hidcategoryid");
                if(hidcategoryid.value.tolower() == categoryid.tolower()) {
                    hyperlink lnkcategory = (hyperlink)item.findcontrol("lnkcategory");
                    lnkcategory.forecolor = system.drawing.color.fromargb(199, 116, 3);
                    break;
                }
            }
        }

        private void bindcategories() {
            category category = new category();
            repcategories.datasource = category.getcategories();
            repcategories.databind();           
        }
    }
}

三、为default.aspx添加主题westgarden并添加样式表stylesheet.css,在default.aspx中应用主题,删除default.aspx.cs代码页中的代码及default.aspx中reapeter控件,直接把用户控件拖入到页面中。

 


作者 yousuosi