asp.net三层架构详解

一、数据库

/*==============================================================*/

/* DBMS name:      Microsoft SQL Server 2000                    */

/*==============================================================*/

 

 

if exists (select 1

            from sysobjects

           where id = object_id('newsContent')

            and   type = 'U')

   drop table newsContent

go

 

 

/*==============================================================*/

/* Table: newsContent                                            */

/*==============================================================*/

create table newsContent (

   ID           int              identity(1,1)   primary key,

   Title          nvarchar(50)     not null,

   Content       ntext            not null,

   AddDate      datetime         not null,

  CategoryID    int              not null

)

go

 

 

二、项目文件架构

实现步骤为:4-3-6-5-2-1

 

ID

 

 

项目

 

 

描述

 

 

用途

 

 

项目引用关系

 

 

实例所需文件

 

 

相关方法

 

 

1

 

 

Web

 

 

表现层

 

 

Web页和控件

 

 

引用BLL

 

 

WebUI.aspx

WebUI.aspx.cs

 

 

 

GetContent()

 

 

2

 

 

BLL

 

 

业务逻辑层

 

 

业务逻辑组件

 

 

引用 IDAL,Model,使用DALFactory创建实例

 

 

Content.cs

 

 

ContentInfo GetContentInfo(int id)

 

 

3

 

 

IDAL

 

 

数据访问层接口定义

 

 

每个DAL实现都要实现的一组接口

 

 

引用 Model

 

 

IContent.cs

 

 

ContentInfo GetContentInfo(int id)

 

 

4

 

 

Model

 

 

业务实体

 

 

传递各种数据的容器

 

 

无引用

 

 

ContentInfo.cs

 

 

 

 

 

5

 

 

DALFactory

 

 

数据层的抽象工厂

 

 

创建反射,用来确定加载哪一个数据库访问程序集的类

 

 

引用IDAL,通过读取web.config里设置的程序集,加载类的实例,返回给BLL使用。

 

 

Content.cs

 

 

IDAL.Icontent create()

 

 

6

 

 

SQLServerDAL

 

 

SQLServer数据访问层

 

 

Microsoft SQL Server特定的Pet Shop DAL实现,使用了IDAL接口

 

 

引用 Model和IDAL,被DALFactory加载的程序集,实现接口里的方法。

 

 

SqlHelper.cs

 

 

 

Content.cs

 

 

SqlDataReader ExecuteReader()

PrepareCommand()

ContentInfo GetContentInfo(int id)

 

 

OracleDAL

 

 

Oracle数据访问层

 

 

7

 

 

DBUtility

 

 

数据库访问组件基础类

 

 

GetSqlServerConnectionString得到数据库连接字符串,也可省去该项目,在SQLServerDAL.SqlHelper中用static readonly string SqlConnectionString代替。

 

 

无引用

 

 

 

 

 

 

 

 

实现步骤过程

1、创建Model,实现业务实体。

2、创建IDAL,实现接口。

3、创建SQLServerDAL,实现接口里的方法。

4、增加web.config里的配置信息,为SQLServerDAL的程序集。

5、创建DALFactory,返回程序集的指定类的实例。

6、创建BLL,调用DALFactory,得到程序集指定类的实例,完成数据操作方法。

7、创建WEB,调用BLL里的数据操作方法。

注意:

1、web.config里的程序集名称必须与SQLServerDAL里的输出程序集名称一致。

2、DALFactory里只需要一个DataAccess类,可以完成创建所有的程序集实例。

3、项目创建后,注意修改各项目的默认命名空间和程序集名称。

4、注意修改解决方案里的项目依赖。

5、注意在解决方案里增加各项目引用。

 

三、各层间的访问过程

1、传入值,将值进行类型转换(为整型)。

2、创建BLL层的content.cs对象c,通过对象c访问BLL层的方法GetContentInfo(ID)调用BLL层。

3、BLL层方法GetContentInfo(ID)中取得数据访问层SQLServerDAL的实例,实例化IDAL层的接口对象dal,这个对象是由工厂层DALFactory创建的,然后返回IDAL层传入值所查找的内容的方法dal.GetContentInfo(id)。

4、数据工厂通过web.config配置文件中给定的webdal字串访问SQLServerDAL层,返回一个完整的调用SQLServerDAL层的路径给 BLL层。

5、到此要调用SQLServerDAL层,SQLServerDAL层完成赋值Model层的对象值为空,给定一个参数,调用SQLServerDAL层的SqlHelper的ExecuteReader方法,读出每个字段的数据赋值给以定义为空的Model层的对象。

6、SqlHelper执行sql命令,返回一个指定连接的数据库记录集,在这里需要引用参数类型,提供为打开连接命令执行做好准备PrepareCommand。

7、返回Model层把查询得到的一行记录值赋值给SQLServerDAL层的引入的Model层的对象ci,然后把这个对象返回给BLL。

8、回到Web层的BLL层的方法调用,把得到的对象值赋值给Lable标签,在前台显示给界面

 

四、项目中的文件清单

 

1、DBUtility项目

(1)connectionInfo.cs

[c-sharp]  view plain copy print ?
  1. using System;  
  2.   
  3. using System.Configuration;  
  4.   
  5.    
  6.   
  7. namespace Utility  
  8.   
  9. {  
  10.   
  11.        /// <summary>  
  12.   
  13.        /// ConnectionInfo 的摘要说明。  
  14.   
  15.        /// </summary>  
  16.   
  17.        public class ConnectionInfo  
  18.   
  19.        {  
  20.   
  21.               public static string GetSqlServerConnectionString()  
  22.   
  23.               {  
  24.   
  25.                      return ConfigurationSettings.AppSettings["SQLConnString"];  
  26.   
  27.               }  
  28.   
  29.        }  
  30.   
  31. }  

2、SQLServerDAL项目

(1)SqlHelper.cs抽象类

[c-sharp]  view plain copy print ?
  1. using System;  
  2.   
  3. using System.Data;  
  4.   
  5. using System.Data.SqlClient;  
  6.   
  7. using DBUtility;  
  8.   
  9.    
  10.   
  11. namespace SQLServerDAL  
  12.   
  13. {  
  14.   
  15.        /// <summary>  
  16.   
  17.        /// SqlHelper 的摘要说明。  
  18.   
  19.        /// </summary>  
  20.   
  21.        public abstract class SqlHelper  
  22.   
  23.        {  
  24.   
  25.               public static readonly string CONN_STR = ConnectionInfo.GetSqlServerConnectionString();  
  26.   
  27.    
  28.   
  29.               /// <summary>  
  30.   
  31.               /// 用提供的函数,执行SQL命令,返回一个从指定连接的数据库记录集  
  32.   
  33.               /// </summary>  
  34.   
  35.               /// <remarks>  
  36.   
  37.               /// 例如:  
  38.   
  39.               /// SqlDataReader r = ExecuteReader(connString, CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24));  
  40.   
  41.               /// </remarks>  
  42.   
  43.               /// <param name="connectionString">SqlConnection有效的SQL连接字符串</param>  
  44.   
  45.               /// <param name="commandType">CommandType:CommandType.Text、CommandType.StoredProcedure</param>  
  46.   
  47.               /// <param name="commandText">SQL语句或存储过程</param>  
  48.   
  49.               /// <param name="commandParameters">SqlParameter[]参数数组</param>  
  50.   
  51.               /// <returns>SqlDataReader:执行结果的记录集</returns>  
  52.   
  53.               public static SqlDataReader ExecuteReader(string connString, CommandType cmdType, string cmdText, params SqlParameter[] cmdParms)  
  54.   
  55.               {  
  56.   
  57.                      SqlCommand cmd = new SqlCommand();  
  58.   
  59.                      SqlConnection conn = new SqlConnection(connString);  
  60.   
  61.    
  62.   
  63.                      // 我们在这里用 try/catch 是因为如果这个方法抛出异常,我们目的是关闭数据库连接,再抛出异常,  
  64.   
  65.                      // 因为这时不会有DataReader存在,此后commandBehaviour.CloseConnection将不会工作。  
  66.   
  67.                      try  
  68.   
  69.                      {  
  70.   
  71.                             PrepareCommand(cmd, conn, null, cmdType, cmdText, cmdParms);  
  72.   
  73.                             SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);  
  74.   
  75.                             cmd.Parameters.Clear();  
  76.   
  77.                             return rdr;  
  78.   
  79.                      }  
  80.   
  81.                      catch  
  82.   
  83.                      {  
  84.   
  85.                             conn.Close();  
  86.   
  87.                             throw;  
  88.   
  89.                      }  
  90.   
  91.               }  
  92.   
  93.    
  94.   
  95.    
  96.   
  97.               /// <summary>  
  98.   
  99.               /// 为执行命令做好准备:打开数据库连接,命令语句,设置命令类型(SQL语句或存储过程),函数语取。  
  100.   
  101.               /// </summary>  
  102.   
  103.               /// <param name="cmd">SqlCommand 组件</param>  
  104.   
  105.               /// <param name="conn">SqlConnection 组件</param>  
  106.   
  107.               /// <param name="trans">SqlTransaction 组件,可以为null</param>  
  108.   
  109.               /// <param name="cmdType">语句类型:CommandType.Text、CommandType.StoredProcedure</param>  
  110.   
  111.               /// <param name="cmdText">SQL语句,可以为存储过程</param>  
  112.   
  113.               /// <param name="cmdParms">SQL参数数组</param>  
  114.   
  115.               private static void PrepareCommand(SqlCommand cmd, SqlConnection conn, SqlTransaction trans, CommandType cmdType, string cmdText, SqlParameter[] cmdParms)  
  116.   
  117.               {  
  118.   
  119.    
  120.   
  121.                      if (conn.State != ConnectionState.Open)  
  122.   
  123.                             conn.Open();  
  124.   
  125.    
  126.   
  127.                      cmd.Connection = conn;  
  128.   
  129.                      cmd.CommandText = cmdText;  
  130.   
  131.    
  132.   
  133.                      if (trans != null)  
  134.   
  135.                             cmd.Transaction = trans;  
  136.   
  137.    
  138.   
  139.                      cmd.CommandType = cmdType;  
  140.   
  141.    
  142.   
  143.                      if (cmdParms != null)  
  144.   
  145.                      {  
  146.   
  147.                             foreach (SqlParameter parm in cmdParms)  
  148.   
  149.                                    cmd.Parameters.Add(parm);  
  150.   
  151.                      }  
  152.   
  153.               }  
  154.   
  155.        }  
  156.   
  157. }  

(2)Content.cs类

[c-sharp]  view plain copy print ?
  1. using System;  
  2.   
  3. using System.Data;  
  4.   
  5. using System.Data.SqlClient;  
  6.   
  7. using Model;  
  8.   
  9. using IDAL;  
  10.   
  11.    
  12.   
  13. namespace SQLServerDAL  
  14.   
  15. {  
  16.   
  17.        /// <summary>  
  18.   
  19.        /// Content 的摘要说明。  
  20.   
  21.        /// </summary>  
  22.   
  23.        public class Content:IContent   
  24.   
  25.        {  
  26.   
  27.    
  28.   
  29.               private const string PARM_ID = "@ID";  
  30.   
  31.               private const string SQL_SELECT_CONTENT = "Select ID, Title, Content, AddDate, CategoryID From newsContent Where ID = @ID";  
  32.   
  33.    
  34.   
  35.    
  36.   
  37.               public ContentInfo GetContentInfo(int id)  
  38.   
  39.               {  
  40.   
  41.                      //创意文章内容类  
  42.   
  43.                      ContentInfo ci = null;  
  44.   
  45.    
  46.   
  47.                      //创建一个参数  
  48.   
  49.                      SqlParameter parm = new SqlParameter(PARM_ID, SqlDbType.BigInt, 8);  
  50.   
  51.                      //赋上ID值  
  52.   
  53.                      parm.Value = id;  
  54.   
  55.    
  56.   
  57.                      using(SqlDataReader sdr = SqlHelper.ExecuteReader(SqlHelper.CONN_STR, CommandType.Text, SQL_SELECT_CONTENT, parm))  
  58.   
  59.                      {  
  60.   
  61.                             if(sdr.Read())  
  62.   
  63.                             {   
  64.   
  65.                                    ci = new ContentInfo(sdr.GetInt32(0),sdr.GetString(1), sdr.GetString(2),  
  66.   
  67.                                           sdr.GetDateTime(3), sdr.GetInt32(4), sdr.GetInt32(5), sdr.GetString(6));  
  68.   
  69.                             }  
  70.   
  71.                      }  
  72.   
  73.                      return ci;  
  74.   
  75.               }  
  76.   
  77.        }  
  78.   
  79. }  

 

3、Model项目

(1)contentInfo.cs

[c-sharp]  view plain copy print ?
  1. using System;  
  2.   
  3.    
  4.   
  5. namespace Model  
  6.   
  7. {  
  8.   
  9.        /// <summary>  
  10.   
  11.        /// Class1 的摘要说明。  
  12.   
  13.        /// </summary>  
  14.   
  15.        public class ContentInfo  
  16.   
  17.        {  
  18.   
  19.               private int _ID;  
  20.   
  21.               private string _Content;  
  22.   
  23.               private string _Title;  
  24.   
  25.               private string _From;  
  26.   
  27.               private DateTime _AddDate;  
  28.   
  29.               private int _clsID;  
  30.   
  31.               private int _tmpID;  
  32.   
  33.    
  34.   
  35.               /// <summary>  
  36.   
  37.               /// 文章内容构造函数  
  38.   
  39.               /// </summary>  
  40.   
  41.               /// <param name="id">文章流水号ID</param>  
  42.   
  43.               /// <param name="content">文章内容</param>  
  44.   
  45.               /// <param name="title">文章标题</param>  
  46.   
  47.               /// <param name="from">文章来源</param>  
  48.   
  49.               /// <param name="clsid">文章的分类属性ID</param>  
  50.   
  51.               /// <param name="tmpid">文章的模板属性ID</param>  
  52.   
  53.               public ContentInfo(int id,string title,string content,string from,DateTime addDate,int clsid,int tmpid )  
  54.   
  55.               {  
  56.   
  57.                      this._ID = id;  
  58.   
  59.                      this._Content = content;  
  60.   
  61.                      this._Title = title;  
  62.   
  63.                      this._From = from;  
  64.   
  65.                      this._AddDate = addDate;  
  66.   
  67.                      this._clsID = clsid;  
  68.   
  69.                      this._tmpID = tmpid;  
  70.   
  71.               }  
  72.   
  73.    
  74.   
  75.    
  76.   
  77.               //属性  
  78.   
  79.               public int ID  
  80.   
  81.               {  
  82.   
  83.                      get   { return _ID; }  
  84.   
  85.               }  
  86.   
  87.               public string Content  
  88.   
  89.               {  
  90.   
  91.                      get   { return _Content; }  
  92.   
  93.               }  
  94.   
  95.               public string Title  
  96.   
  97.               {  
  98.   
  99.                      get   { return _Title; }  
  100.   
  101.               }  
  102.   
  103.               public string From  
  104.   
  105.               {  
  106.   
  107.                      get   { return _From; }  
  108.   
  109.               }  
  110.   
  111.               public DateTime AddDate  
  112.   
  113.               {  
  114.   
  115.                      get   { return _AddDate; }  
  116.   
  117.               }  
  118.   
  119.               public int ClsID  
  120.   
  121.               {  
  122.   
  123.                      get   { return _clsID; }  
  124.   
  125.               }  
  126.   
  127.               public int TmpID  
  128.   
  129.               {  
  130.   
  131.                      get   { return _tmpID; }  
  132.   
  133.               }  
  134.   
  135.    
  136.   
  137.    
  138.   
  139.    
  140.   
  141.        }  
  142.   
  143. }  

4、IDAL项目

(1)Icontent.cs

[c-sharp]  view plain copy print ?
  1. using System;  
  2.   
  3. using Model;  
  4.   
  5.    
  6.   
  7. namespace IDAL  
  8.   
  9. {  
  10.   
  11.        /// <summary>  
  12.   
  13.        /// 文章内容操作接口  
  14.   
  15.        /// </summary>  
  16.   
  17.        public interface IContent  
  18.   
  19.        {  
  20.   
  21.               /// <summary>  
  22.   
  23.               /// 取得文章的内容。  
  24.   
  25.               /// </summary>  
  26.   
  27.               /// <param name="id">文章的ID</param>  
  28.   
  29.               /// <returns></returns>  
  30.   
  31.               ContentInfo GetContentInfo(int id);  
  32.   
  33.        }  
  34.   
  35. }  

 

5、DALFactory项目

(1)Content.cs

[c-sharp]  view plain copy print ?
  1. using System;  
  2.   
  3. using System.Reflection;  
  4.   
  5. using System.Configuration;  
  6.   
  7. using IDAL;  
  8.   
  9.    
  10.   
  11. namespace DALFactory  
  12.   
  13. {  
  14.   
  15.        /// <summary>  
  16.   
  17.        /// 工产模式实现文章接口。  
  18.   
  19.        /// </summary>  
  20.   
  21.        public class Content  
  22.   
  23.        {  
  24.   
  25.               public static IDAL.IContent Create()  
  26.   
  27.               {  
  28.   
  29.                      // 这里可以查看 DAL 接口类。  
  30.   
  31.                      string path = System.Configuration.ConfigurationSettings.AppSettings["WebDAL"].ToString();  
  32.   
  33.                      string className = path+".Content";  
  34.   
  35.                       
  36.   
  37.                      // 用配置文件指定的类组合  
  38.   
  39.                      return (IDAL.IContent)Assembly.Load(path).CreateInstance(className);  
  40.   
  41.               }  
  42.   
  43.        }  
  44.   
  45. }  

 

6、BLL项目

(1)Content.cs

 

[c-sharp]  view plain copy print ?
  1. using System;  
  2.   
  3.    
  4.   
  5. using Model;  
  6.   
  7. using IDAL;  
  8.   
  9.    
  10.   
  11. namespace BLL  
  12.   
  13. {  
  14.   
  15.        /// <summary>  
  16.   
  17.        /// Content 的摘要说明。  
  18.   
  19.        /// </summary>  
  20.   
  21.        public class Content  
  22.   
  23.        {  
  24.   
  25.    
  26.   
  27.               public ContentInfo GetContentInfo(int id)  
  28.   
  29.               {  
  30.   
  31.    
  32.   
  33.                      // 取得从数据访问层取得一个文章内容实例  
  34.   
  35.                      IContent dal = DALFactory.Content.Create();  
  36.   
  37.    
  38.   
  39.                      // 用DAL查找文章内容  
  40.   
  41.                      return dal.GetContentInfo(id);  
  42.   
  43.               }  
  44.   
  45.        }  
  46.   
  47. }  

7、Web项目

1、Web.config:

[c-sharp]  view plain copy print ?
  1. <appSettings>  
  2.   
  3.       <add key="SQLConnString" value="Data Source=localhost;Persist Security info=True;Initial Catalog=newsDB;User ID=sa;Password= " />  
  4.   
  5.    <add key="WebDAL" value="SQLServerDAL" />     
  6.   
  7. </appSettings>  

 

2、WebUI.aspx

 

[c-sharp]  view plain copy print ?
  1. <%@ Page language="c#" Codebehind="WebUI.aspx.cs" AutoEventWireup="false" Inherits="Web.WebUI" %>  
  2.   
  3. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >  
  4.   
  5. <HTML>  
  6.   
  7.        <HEAD>  
  8.   
  9.               <title>WebUI</title>  
  10.   
  11.               <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">  
  12.   
  13.               <meta name="CODE_LANGUAGE" Content="C#">  
  14.   
  15.               <meta name="vs_defaultClientScript" content="JavaScript">  
  16.   
  17.               <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">  
  18.   
  19.        </HEAD>  
  20.   
  21.        <body MS_POSITIONING="GridLayout">  
  22.   
  23.               <form id="Form1" method="post" runat="server">  
  24.   
  25.                      <FONT">宋体"></FONT>  
  26.   
  27.                      <table width="600" border="1">  
  28.   
  29.                             <tr>  
  30.   
  31.                                    <td style="WIDTH: 173px"> </td>  
  32.   
  33.                                    <td>   
  34.   
  35.                                           <asp:Label id="lblTitle" runat="server"></asp:Label></td>  
  36.   
  37.                             </tr>  
  38.   
  39.                             <tr>  
  40.   
  41.                                    <td style="WIDTH: 173px; HEIGHT: 22px"> </td>  
  42.   
  43.                                    <td style="HEIGHT: 22px">   
  44.   
  45.                                           <asp:Label id="lblDataTime" runat="server"></asp:Label></td>  
  46.   
  47.                             </tr>  
  48.   
  49.                             <tr>  
  50.   
  51.                                    <td style="WIDTH: 173px"> </td>  
  52.   
  53.                                    <td>   
  54.   
  55.                                           <asp:Label id="lblContent" runat="server"></asp:Label></td>  
  56.   
  57.                             </tr>  
  58.   
  59.                             <tr>  
  60.   
  61.                                    <td style="WIDTH: 173px"> </td>  
  62.   
  63.                                    <td> </td>  
  64.   
  65.                             </tr>  
  66.   
  67.                             <tr>  
  68.   
  69.                                    <td style="WIDTH: 173px; HEIGHT: 23px"> </td>  
  70.   
  71.                                    <td style="HEIGHT: 23px"> </td>  
  72.   
  73.                             </tr>  
  74.   
  75.                             <tr>  
  76.   
  77.                                    <td style="WIDTH: 173px"> </td>  
  78.   
  79.                                    <td> </td>  
  80.   
  81.                             </tr>  
  82.   
  83.                             <tr>  
  84.   
  85.                                    <td style="WIDTH: 173px"> </td>  
  86.   
  87.                                    <td> </td>  
  88.   
  89.                             </tr>  
  90.   
  91.                             <tr>  
  92.   
  93.                                    <td style="WIDTH: 173px"> </td>  
  94.   
  95.                                    <td> </td>  
  96.   
  97.                             </tr>  
  98.   
  99.                             <tr>  
  100.   
  101.                                    <td style="WIDTH: 173px"> </td>  
  102.   
  103.                                    <td>   
  104.   
  105.                                           <asp:Label id="lblMsg" runat="server">Label</asp:Label></td>  
  106.   
  107.                             </tr>  
  108.   
  109.                      </table>  
  110.   
  111.               </form>  
  112.   
  113.        </body>  
  114.   
  115. </HTML>  

 

3、WebUI.aspx.cs后台调用显示:

[c-sharp]  view plain copy print ?
  1. using System;  
  2.   
  3. using System.Collections;  
  4.   
  5. using System.ComponentModel;  
  6.   
  7. using System.Data;  
  8.   
  9. using System.Drawing;  
  10.   
  11. using System.Web;  
  12.   
  13. using System.Web.SessionState;  
  14.   
  15. using System.Web.UI;  
  16.   
  17. using System.Web.UI.WebControls;  
  18.   
  19. using System.Web.UI.HtmlControls;  
  20.   
  21.    
  22.   
  23. using BLL;  
  24.   
  25. using Model;  
  26.   
  27.    
  28.   
  29. namespace myWeb  
  30.   
  31. {  
  32.   
  33.        /// <summary>  
  34.   
  35.        /// WebForm1 的摘要说明。  
  36.   
  37.        /// </summary>  
  38.   
  39.        public class WebUI : System.Web.UI.Page  
  40.   
  41.        {  
  42.   
  43.               protected System.Web.UI.WebControls.Label lblTitle;  
  44.   
  45.               protected System.Web.UI.WebControls.Label lblDataTime;  
  46.   
  47.               protected System.Web.UI.WebControls.Label lblContent;  
  48.   
  49.               protected System.Web.UI.WebControls.Label lblMsg;  
  50.   
  51.    
  52.   
  53.               private ContentInfo ci ;  
  54.   
  55.    
  56.   
  57.    
  58.   
  59.               private void Page_Load(object sender, System.EventArgs e)  
  60.   
  61.               {  
  62.   
  63.                      if(!Page.IsPostBack)  
  64.   
  65.                      {  
  66.   
  67.                             GetContent("1");  
  68.   
  69.                      }  
  70.   
  71.               }  
  72.   
  73.    
  74.   
  75.               private void GetContent(string id)  
  76.   
  77.               {  
  78.   
  79.                      int ID = WebComponents.CleanString.GetInt(id);  
  80.   
  81.                
  82.   
  83.                      Content c = new Content();  
  84.   
  85.                      ci = c.GetContentInfo(ID);  
  86.   
  87.                      if(ci!=null)  
  88.   
  89.                      {  
  90.   
  91.                             this.lblTitle.Text = ci.Title;  
  92.   
  93.                             this.lblDataTime.Text = ci.AddDate.ToString("yyyy-MM-dd");  
  94.   
  95.                             this.lblContent.Text = ci.Content;  
  96.   
  97.                      }  
  98.   
  99.                      else  
  100.   
  101.                      {  
  102.   
  103.                             this.lblMsg.Text = "没有找到这篇文章";  
  104.   
  105.                      }  
  106.   
  107.               }  
  108.  
  109.   
  110.  
  111.               #region Web 窗体设计器生成的代码  
  112.   
  113.               override protected void OnInit(EventArgs e)  
  114.   
  115.               {  
  116.   
  117.                      //  
  118.   
  119.                      // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。  
  120.   
  121.                      //  
  122.   
  123.                      InitializeComponent();  
  124.   
  125.                      base.OnInit(e);  
  126.   
  127.               }  
  128.   
  129.                
  130.   
  131.               /// <summary>  
  132.   
  133.               /// 设计器支持所需的方法 - 不要使用代码编辑器修改  
  134.   
  135.               /// 此方法的内容。  
  136.   
  137.               /// </summary>  
  138.   
  139.               private void InitializeComponent()  
  140.   
  141.               {     
  142.   
  143.                      this.Load += new System.EventHandler(this.Page_Load);  
  144.   
  145.    
  146.   
  147.               }  
  148.  
  149.               #endregion  
  150.   
  151.        }  
  152.   
  153. }  

4、WebComponents项目

(1)CleanString.cs

[c-sharp]  view plain copy print ?
  1. using System;  
  2.   
  3. using System.Text;  
  4.   
  5.    
  6.   
  7. namespace myWeb.WebComponents  
  8.   
  9. {  
  10.   
  11.        /// <summary>  
  12.   
  13.        /// CleanString 的摘要说明。  
  14.   
  15.        /// </summary>  
  16.   
  17.        public class CleanString  
  18.   
  19.        {  
  20.   
  21.    
  22.   
  23.               public static int GetInt(string inputString)  
  24.   
  25.               {  
  26.   
  27.                      try  
  28.   
  29.                      {  
  30.   
  31.                             return Convert.ToInt32(inputString);  
  32.   
  33.                      }  
  34.   
  35.                      catch  
  36.   
  37.                      {  
  38.   
  39.                             return 0;  
  40.   
  41.                      }  
  42.   
  43.    
  44.   
  45.               }  
  46.   
  47.    
  48.   
  49.    
  50.   
  51.               public static string InputText(string inputString, int maxLength)  
  52.   
  53.               {  
  54.   
  55.                      StringBuilder retVal = new StringBuilder();  
  56.   
  57.    
  58.   
  59.                      // check incoming parameters for null or blank string  
  60.   
  61.                      if ((inputString != null) && (inputString != String.Empty))  
  62.   
  63.                      {  
  64.   
  65.                             inputString = inputString.Trim();  
  66.   
  67.    
  68.   
  69.                             //chop the string incase the client-side max length  
  70.   
  71.                             //fields are bypassed to prevent buffer over-runs  
  72.   
  73.                             if (inputString.Length > maxLength)  
  74.   
  75.                                    inputString = inputString.Substring(0, maxLength);  
  76.   
  77.    
  78.   
  79.                             //convert some harmful symbols incase the regular  
  80.   
  81.                             //expression validators are changed  
  82.   
  83.                             for (int i = 0; i < inputString.Length; i++)  
  84.   
  85.                             {  
  86.   
  87.                                    switch (inputString[i])  
  88.   
  89.                                    {  
  90.   
  91.                                           case '"':  
  92.   
  93.                                                  retVal.Append(""");  
  94.   
  95.                                                  break;  
  96.   
  97.                                           case '<':  
  98.   
  99.                                                  retVal.Append("<");  
  100.   
  101.                                                  break;  
  102.   
  103.                                           case '>':  
  104.   
  105.                                                  retVal.Append(">");  
  106.   
  107.                                                  break;  
  108.   
  109.                                           default:  
  110.   
  111.                                                  retVal.Append(inputString[i]);  
  112.   
  113.                                                  break;  
  114.   
  115.                                    }  
  116.   
  117.                             }  
  118.   
  119.    
  120.   
  121.                             // Replace single quotes with white space  
  122.   
  123.                             retVal.Replace("'"" ");  
  124.   
  125.                      }  
  126.   
  127.    
  128.   
  129.                      return retVal.ToString();  
  130.   
  131.                       
  132.   
  133.               }  
  134.   
  135.                  
  136.   
  137.        }  
  138.   
  139. }  


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值