.net2.0学习笔记 未整理

来源:互联网 发布:2010excel软件下载 编辑:程序博客网 时间:2024/05/17 08:31

Beginning ASP.NET 2.0

E-Commerce in C# 2005

2005入门 电子商务

第二章

现在比较流行的3层结构是表示层、业务层、数据层。

正确的逻辑才有正确的结构。

我们的例子是气球商店。也是使用3层结构。

我们的层次结构和使用到的技术。

表现层

使用asp.net Form

Asp.net 用户控件

业务层

C#

数据层

Sqlserver

Exercise:增加web项目

File New Web Site.

 


创建了一个默认的页面 default.asp

F5可以进行调试。

 

Exercise:建立第一个页面

增加一个MasterPage项目。中文叫 母板页。

在页面中使用formtable建立页面框架。

<%@ Master Language="C#" AutoEventWireup="true"

CodeFile="BalloonShop.master.cs" Inherits="BalloonShop" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"

"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

<title>BalloonShop</title>

</head>

<body>

<form id="Form1" runat="server">

<table cellspacing="0" cellpadding="0" width="770" border="0">

<tr>

<td width="220" valign="top">

List of Departments

<br />

List of Categories

<br />

</td>

<td valign="top">

Header

<asp:ContentPlaceHolder ID="contentPlaceHolder" runat="server">

</asp:ContentPlaceHolder>

</td>

</tr>

</table>

</form>

</body>

</html>

其中有服务器端控件 contentplaceholder有绿色的箭头。

删除默认页。

 

新建一个默认aspx页面,在创建的时候复选“选择母板”。

这里好像,这个页面只能对母板页中标记为contentplaceholder的部分进行操作。

How It Works: The Main Web Page

它是怎样工作的

Right now you have the skeleton of the first BalloonShop page in place. Perhaps it’s not apparent right now, butworking with Master Pages will save you a lot of headaches later on when you extend the site.

现在,你已经为第一个页面增加了骨架,现在还没有准备好,不过使用母板将减轻以后的麻烦。

 

The Master Page establishes the layout of the pages that implement it, and these pages have the freedom to updatethe contents of the ContentPlaceHolder elements.

母板能够规划页面。这些俄页面也可以自由的更新内容,通过ContentPlaceHolder元素。

In our case, the header, the list of departments, and the listof categories are standard elements that will appear in every page of the web site (although the list of categories willhave blank output in some cases and will appear only when a department is selected—you’ll see more about thisin the next chapter).

在这个例子中,其他部分都是标准元素,每一页都是一样的。

注意 母板页不仅仅只能包含ContentPlaceHolder元素。

 

创建站点图片

创建用户控件文件夹 UserControls

将写好的用户控件header.ascx插入到母板中。

 

 

sqlservice 2005 联机

使用存储过程 它是通过业务层,属于数据层的一部分。

Data Add New Stored Procedure.

完成後运行。右键存储过程。

 

 

增加网站逻辑

业务层是应用程序的大脑,因为它是负责程序业务逻辑。

对于这个业务我们需要建立3个类稳健。GenericDataAccessCatalogAccessBalloonShopConfigraion

对于sql2005 local使用(local)/SqlExpress

 

创建SqlCommand对象。使用存储过程。

// Open the connection

conn.Open();

// Create the SqlDataReader object by executing the command

SqlDataReader reader = comm.ExecuteReader();

// Create a new DataTable and populate it from the SqlDataReader

DataTable table = new DataTable();

table.Load(reader);

// Close the reader and the connection

reader.Close();

conn.Close();

 

net2.0新增了DbProviderFactory

 

 

对于发送email,当网站发生错误,我们可以使用发mail的方式发送报告。

SmtpClient and MailMessage classes

 

创建业务逻辑代码。

将数据链接代码写在config文件。

<connectionStrings>

<add name="BalloonShopConnection" connectionString="Server=

(local)/SqlExpress;

Integrated Security=True;Database=BalloonShop"

providerName="System.Data.SqlClient"/>

</connectionStrings>

 

现在 网站的类文件要放在app_data

public static DataTable ExecuteSelectCommand(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 执行後存入datatable

        DbDataReader reader = command.ExecuteReader();

        table = new DataTable();

        table.Load(reader);

        // Close the reader

        reader.Close();

        }

        catch (Exception ex)

        {

        Utilities.LogError(ex);

        throw ex;

        }

        finally

        {

        // Close the connection

        command.Connection.Close();

        }

        return table;

}

范例 通过command得到table

 

 

这个数据层由2个基础数据工具类和1个业务逻辑类组成。

 

将得到的数据显示出来。

注意 在webconfig中要加入

<identity impersonate="true" /> 在能得到认证通过

这里用来填充的是datalist

 

增加一个自定义的错误页。

先创建全局应用程序类。

void Application_Error(Object sender, EventArgs e)

{

// Log all unhandled errors

Utilities.LogError(Server.GetLastError());

}

config.xml

<customErrors mode="RemoteOnly" defaultRedirect="Oooops.aspx" />

 
原创粉丝点击