Online Shopping System (Task004)-data access layer DAL

Source: Internet
Author: User
Tags servervariables

1. Add the class library DAL, add the class Category. cs to the DAL, and add the function GetCategories () to the class library. The Code is as follows:

 

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


}
}


The GetCategories () function reads the connection string in SQLHelper. Therefore, you need to add the code in SQLHelper. cs:

Public static readonly string ConnectionStringLocalTransaction = ConfigurationManager. ConnectionStrings ["NetShopConnString"]. ConnectionString;

Note: Add reference System. Configuration.

2. Use User Controls

Create a folder Controls on the Web and add the user control NavigationControl. ascx to it. The code for the form page and the code page are as follows:

[Html] <% @ 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>
<% @ 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 ();
}
}
}

The three parameters are westult.aspx.pdf theme westgardenand the sample table stylesheet.css. Apply the theme in Default. aspx, delete the code in the Default. aspx. cs code page and the Reapeter control in Default. aspx, and drag the user control into the page directly.

 


Author: yousuosi

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.