读书:《Asp.net2.0电子商务开发实战》(二)
写一点在初学者实践的时候需要注意的东西。
创建商品目录:
创建存储过程:GetDepartments
为网站添加业务。
BalloonShopConfiguration.cs
这个类里面主要是一些配置信息。
GenericDataAccess.cs
通用的一些方法
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.Common;
/// <summary>
///GenericDataAccess 的摘要说明
/// </summary>
public static class GenericDataAccess
{
static GenericDataAccess()
{
//
//TODO: 在此处添加构造函数逻辑
//
}
//executes a command and returns the results as a dataTable object
public static DataTable ExecuterSelectCommand(DbCommand command)
{
//The DataTable to be returned
DataTable table;
//Execute the command making sure the connection gets closed in the end
try
{
//Open the data connection
command.Connection.Open();
//Execute the command and save the results in a DataTable
DbDataReader reader = command.ExecuteReader();
table = new DataTable();
table.Load(reader);
reader.Close();
}
catch (Exception ex)
{
Utilities.LogError(ex);
throw ex;
}
finally
{
command.Connection.Close();
}
return table;
}
//creates and prepares a new DbCommand object on a new connection
public static DbCommand CreateCommand()
{
//obtain the database provider name
string dataProviderName = BalloonShopConfiguration.DbProviderName;
//obtain the database connection string
string connectionString = BalloonShopConfiguration.DbconnectionString;
//Create a new data provider factory
DbProviderFactory factory = DbProviderFactories.GetFactory(dataProviderName);
//obtain a database specific connection object
DbConnection conn = factory.CreateConnection();
//Set the Connection string
conn.ConnectionString = connectionString;
//create a database specific command object
DbCommand comm = conn.CreateCommand();
comm.CommandType = CommandType.StoredProcedure;
return comm;
}
}
CatalogAccess.cs
好我们看表示层,
新建一个用户控件,里面放上Datalist
编辑Html代码:
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="DepartmentList.ascx.cs" Inherits="UserControls_DepartmentList" %>
<asp:DataList ID="list" runat="server" CssClass="DepartmentListContent"
Width="200px">
<HeaderTemplate>
Choose a Department
</HeaderTemplate>
<HeaderStyle CssClass="DepartmentListHead" />
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server"
NavigateUrl='<%# "../Catalog.aspx?DepartmentID="+ Eval("DepartmentID") %>'
Text='<%#Eval("Name") %>'
ToolTip='<%#Eval("Description") %>'
CssClass='<%# Eval("DepartmentID").ToString()==Request.QueryString["DepartmentID"]?"DepartmentSelected":"DepartmentUnselecteded" %>'
></asp:HyperLink>
</ItemTemplate>
</asp:DataList>
后台代码
public partial class UserControls_DepartmentList : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
list.DataSource = CatalogAccess.GetDepartments();
list.DataBind();
}
}
好,我们走一遍:程序运行,首先调用将Datalist绑定数据源,
首先是到这个方法list.DataSource = CatalogAccess.GetDepartments();需要一个数据源
转跳到public static DataTable GetDepartments(),这个方法返回一个Datatable,再转跳到GenericDataAccess.CreateCommand()来生成一个Comm。有了comm,给出存储过程的名称,comm执行存储过程就OK了,返回一个Datatable,最后绑定Datalist。
流程很清晰!
不过这个<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server"
NavigateUrl='<%# "../Catalog.aspx?DepartmentID="+ Eval("DepartmentID") %>'
Text='<%#Eval("Name") %>'
ToolTip='<%#Eval("Description") %>'
CssClass='<%# Eval("DepartmentID").ToString()==Request.QueryString["DepartmentID"]?"DepartmentSelected":"DepartmentUnselecteded" %>'
></asp:HyperLink>
</ItemTemplate>
要注意看下,超链接NavigateUrl='<%# "../Catalog.aspx?DepartmentID="+ Eval("DepartmentID") %>' Text='<%#Eval("Name") %>'
返回Text目的就达到了,为什么要把ID放在URL里呢?在,Cssclass里面就知道为什么要把ID也返回并在页面见传递了。至于Eval是怎么在后面工作的,暂时就不用管了!
清楚的知道流程就可以了!
后面还有错误页面的设计,我还不会Email的设置,我就不说了!