2005年11月08日

发掘.net性能对于一个application至关重要,所以现在经常找一些可以提高性能的技巧,下面是有关DataBinder的一个小技巧。

对于一般的绑定,我们使用<%# DataBinder.Eval(Container.DataItem, "字段名") %>
用DataBinder.eval 绑定不必关心数据来源。不必关心数据的类型eval会把这个数据对象转换为一个字符串。在底层绑定做了很多工作,使用了反射性能。正因为使用方便了,但却影响了数据性能.


看下<%# DataBinder.Eval(Container.DataItem, "字段名")
%>。当于dataset绑定时DataItem其实是一个DataRowView(如果绑定的是一个数据读取器DataReader,它就是一个
IdataRecord。)因此直接转换成DataRowView的话,将会给性能带来很大提升 <%#
ctype(Container.DataItem,DataRowView).Row("字段名") %> 对数据的绑定建议使用<%#
ctype(Container.DataItem,DataRowView).Row("字段名") %>。数据量大的时候可提高几百倍的速度。

使用时注意两方面:
1.需在页面添加<%@ Import namespace="System.Data"%>
2.注意字段名的大小写(要特别注意)。如果和查询的不一致,在某些情况下会导致比<%# DataBinder.Eval(Container.DataItem, "字段名") %>还要慢。

如果想进一步提高速度,可采用<%# ctype(Container.DataItem,DataRowView).Row(0) %>的方法,不过其可读性不高。

Asp.net的身份验证有有三种,分别是"Windows | Forms | Passport",其中又以Forms验证用的最多,也最灵活。
Forms
验证方式对基于用户的验证授权提供了很好的支持,可以通过一个登录页面验证用户的身份,将此用户的身份发回到客户端的Cookie,之后此用户再访问这个
web应用就会连同这个身份Cookie一起发送到服务端。服务端上的授权设置就可以根据不同目录对不同用户的访问授权进行控制了。


题来了,在实际是用中我们往往需要的是基于角色,或者说基于用户组的验证和授权。对一个网站来说,一般的验证授权的模式应该是这样的:根据实际需求把用户
分成不同的身份,就是角色,或者说是用户组,验证过程不但要验证这个用户本身的身份,还要验证它是属于哪个角色的。而访问授权是根据角色来设置的,某些角
色可以访问哪些资源,不可以访问哪些资源等等。要是基于用户来授权访问将会是个很不实际的做法,用户有很多,还可能随时的增减,不可能在配置文件中随时的
为不断增加的新用户去增加访问授权的。

下面大概的看一下Forms的过程。

Forms身份验证基本原理:

一 身份验证

要采用Forms身份验证,先要在应用程序根目录中的Web.config中做相应的设置:

  <authentication mode="Forms">
   <forms name=".ASPXAUTH" loginUrl="/Default.aspx" protection="All" timeout="30" path="/" />
  </authentication>

其中<authentication mode= "forms"> 表示本应用程序采用Forms验证方式。
1.
<forms>标签中的name表示指定要用于身份验证的 HTTP Cookie。默认情况下,name 的值是
.ASPXAUTH。采用此种方式验证用户后,以此用户的信息建立一个FormsAuthenticationTicket类型的身份验证票,再加密序列
化为一个字符串,最后将这个字符串写到客户端的name指定名字的Cookie中.一旦这个Cookie写到客户端后,此用户再次访问这个web应用时会
将连同Cookie一起发送到服务端,服务端将会知道此用户是已经验证过的.

再看一下身份验证票都包含哪些信息呢,我们看一下FormsAuthenticationTicket类:
CookiePath: 返回发出 Cookie 的路径。注意,窗体的路径设置为 /。由于窗体区分大小写,这是为了防止站点中的 URL 的大小写不一致而采取的一种保护措施。这在刷新 Cookie 时使用
Expiration: 获取 Cookie 过期的日期/时间。
IsPersistent: 如果已发出持久的 Cookie,则返回 true。否则,身份验证 Cookie 将限制在浏览器生命周期范围内。
IssueDate: 获取最初发出 Cookie 的日期/时间。
Name: 获取与身份验证 Cookie 关联的用户名。
UserData :获取存储在 Cookie 中的应用程序定义字符串。
Version: 返回字节版本号供将来使用。


2.
<forms>标签中的loginUrl指定如果没有找到任何有效的身份验证 Cookie,为登录将请求重定向到的 URL。默认值为
default.aspx。loginUrl指定的页面就是用来验证用户身份的,一般此页面提供用户输入用户名和密码,用户提交后由程序来根据自己的需要
来验证用户的合法性(大多情况是将用户输入信息同数据库中的用户表进行比较),如果验证用户有效,则生成同此用户对应的身份验证票,写到客户端的
Cookie,最后将浏览器重定向到用户初试请求的页面.一般是用FormsAuthentication.RedirectFromLoginPage
方法来完成生成身份验证票,写回客户端,浏览器重定向等一系列的动作.

public static void RedirectFromLoginPage( string userName, bool createPersistentCookie, string strCookiePath );

其中:
userName: 就是此用户的标示,用来标志此用户的唯一标示,不一定要映射到用户账户名称.
createPersistentCookie: 标示是否发出持久的 Cookie。

不是持久Cookie,Cookie的有效期Expiration属性有当前时间加上web.config中timeout的时间,每次请求页面时,在验
证身份过程中,会判断是否过了有效期的一半,要是的话更新一次cookie的有效期;若是持久cookie,Expiration属性无意义,这时身份验
证票的有效期有cookie的Expires决定,RedirectFromLoginPage方法给Expires属性设定的是50年有效期。
strCookiePath: 标示将生成的Cookie的写到客户端的路径,身份验证票中保存这个路径是在刷新身份验证票Cookie时使用(这也是生成Cookie的Path),若没有strCookiePath 参数,则使用web.config中 path属性的设置。

这里可以看到,此方法参数只有三个,而身份验证票的属性有七个,不足的四个参数是这么来的:
IssueDate: Cookie发出时间由当前时间得出,
Expiration:过期时间由当前时间和下面要说的<forms>标签中timeout参数算出。此参数对非持久性cookie有意义。
UserData: 这个属性可以用应用程序写入一些用户定义的数据,此方法没有用到这个属性,只是简单的将此属性置为空字符串,请注意此属性,在后面我们将要使用到这个属性。
Version: 版本号由系统自动提供.

RedirectFromLoginPage
方法生成生成身份验证票后,会调用FormsAuthentication.Encrypt
方法,将身份验证票加密为字符串,这个字符串将会是以.ASPXAUTH为名字的一个Cookie的值。这个Cookie的其它属性的生成:
Domain,Path属性为确省值,Expires视createPersistentCookie参数而定,若是持久cookie,Expires设
为50年以后过期;若是非持久cookie,Expires属性不设置。
生成身份验证Cookie后,将此Cookie加入到Response.Cookies中,等待发送到客户端。
最后RedirectFromLoginPage方法调用FormsAuthentication.GetRedirectUrl 方法获取到用户原先请求的页面,重定向到这个页面。

3. <forms>标签中的timeout和path,是提供了身份验证票写入到Cookie过期时间和默认路径。

以上就是基于Forms身份验证的过程,它完成了对用户身份的确认。下面介绍基于Forms身份验证的访问授权。

二 访问授权


证了身份,是要使用这个身份,根据不同的身份我们可以进行不同的操作,处理,最常见的就是对不同的身份进行不同的授权,Forms验证就提供这样的功能。
Forms授权是基于目录的,可以针对某个目录来设置访问权限,比如,这些用户可以访问这个目录,那些用户不能访问这个目录。
同样,授权设置是在你要控制的那个目录下的web.config文件中来设置:
<authorization>
    <allow users="comma-separated list of users"
        roles="comma-separated list of roles"
        verbs="comma-separated list of verbs" />
     <deny users="comma-separated list of users"
        roles="comma-separated list of roles"
        verbs="comma-separated list of verbs" />
</authorization>

<allow>标签表示允许访问,其中的属性
1. users:一个逗号分隔的用户名列表,这些用户名已被授予对资源的访问权限。问号 (?) 允许匿名用户;星号 (*) 允许所有用户。
2. roles:一个逗号分隔的角色列表,这些角色已被授予对资源的访问权限。
3. verbs:一个逗号分隔的 HTTP 传输方法列表,这些 HTTP 传输方法已被授予对资源的访问权限。注册到 ASP.NET 的谓词为 GET、HEAD、POST 和 DEBUG。

<deny>标签表示不允许访问。其中的属性同上面的。


运行时,授权模块迭代通过 <allow> 和 <deny>
标记,直到它找到适合特定用户的第一个访问规则。然后,它根据找到的第一项访问规则是 <allow> 还是 <deny>
规则来允许或拒绝对 URL 资源的访问。Machine.config 文件中的默认身份验证规则是 <allow
users="*"/>,因此除非另行配置,否则在默认情况下会允许访问。

那么这些user 和roles又是如何得到的呢?下面看一下授权的详细过程:

1.
一旦一个用户访问这个网站,就行登录确认了身份,身份验证票的cookie也写到了客户端。之后,这个用户再次申请这个web的页面,身份验证票的
cookie就会发送到服务端。在服务端,asp.net为每一个http请求都分配一个HttpApplication对象来处理这个请求,在
HttpApplication.AuthenticateRequest事件后,安全模块已建立用户标识,就是此用户的身份在web端已经建立起来,这
个身份完全是由客户端发送回来的身份验证票的cookie建立的。
2. 用户身份在HttpContext.User
属性中,在页面中可以通过Page.Context
来获取同这个页面相关的HttpContext对象。对于Forms验证,HttpContext.User属性是一个GenericPrincipal
类型的对象,GenericPrincipal只有一个公开的属性Identity,有个私有的m_role属性,是string[]类型,存放此用户是
属于哪些role的数组,还有一个公开的方法IsInRole(string role),来判断此用户是否属于某个角色。
由于身份验证票的cookie中根本没有提供role这个属性,就是说Forms身份验证票没有提供此用户的role信息,所以,对于Forms验证,在服务端得到的GenericPrincipal 用户对象的m_role属性永远是空的。
3.
GenericPrincipal. Identity
属性是一个FormsIdentity类型的对象,这个对象有个Name属性,就是此用户的标示,访问授权就是将此属性做为user来进行授权验证的。
FormsIdentity还有一个属性,就是Ticket属性,此属性是身份验证票FormsAuthenticationTicket类型,就是之前
服务器写到客户端的身份验证票。
服务器在获取到身份验证票FormsAuthenticationTicket对象后,查看这个身份验证票是不是
非持久的身份验证,是的话要根据web.config中timeout属性设置的有效期来更新这个身份验证票的cookie(为避免危及性能,在经过了超
过一半的指定时间后更新该 Cookie。这可能导致精确性上的损失。持久性 Cookie 不超时。)
4.
在HttpApplication.ResolveRequestCache事件之前,asp.net开始取得用户请求的页面,建立
HttpHandler控制点。这就意味着,在HttpApplication.ResolveRequestCache事件要对用户访问权限就行验证,
看此用户或角色是否有权限访问这个页面,之后在这个请求的生命周期内再改变此用户的身份或角色就没有意义了。


上是Forms验证的全过程,可以看出,这个Forms验证是基于用户的,没有为角色的验证提供直接支持。身份验证票
FormsAuthenticationTicket
中的Name属性是用户标示,其实还有一个属性UserData,这个属性可以由应用程序来写入自定义的一些数据,我们可以利用这个字段来存放role的
信息,从而达到基于角色验证的目的。

Forms身份验证基于角色的授权

一 身份验证

在web.config的<authentication>的设置还是一样:

<authentication mode="forms">
    <forms name=".ASPXAUTH " loginUrl="/login.aspx" timeout="30" path= "/">
    </forms>
</authentication>

/login.aspx
验证用户合法性页面中,在验证了用户的合法性后,还要有个取得此用户属于哪些role的过程,这个看各个应用的本身如何设计的了,一般是在数据库中会有个
use_role表,可以从数据库中获得此用户属于哪些role,在此不深究如何去获取用户对应的role,最后肯定能够获得的此用户对应的所有的
role用逗号分割的一个字符串。
在上面的非基于角色的方法中,我们用了
FormsAuthentication.RedirectFromLoginPage
方法来完成生成身份验证票,写回客户端,浏览器重定向等一系列的动作。这个方法会用一些确省的设置来完成一系列的动作,在基于角色的验证中我们不能用这一
个方法来实现,要分步的做,以便将一些定制的设置加进来:

1. 首先要根据用户标示,和用户属于的角色的字符串来创建身份验证票
public FormsAuthenticationTicket(
int version, //设为1
string name, //用户标示
DateTime issueDate, //Cookie 的发出时间, 设置为 DateTime.Now
DateTime expiration, //过期时间
bool isPersistent, //是否持久性(根据需要设置,若是设置为持久性,在发出
cookie时,cookie的Expires设置一定要设置)
string userData, //这里用上面准备好的用逗号分割的role字符串
string cookiePath
// 设为"/",这要同发出cookie的路径一致,因为刷新cookie
要用这个路径
);

FormsAuthenticationTicket
Ticket = new FormsAuthenticationTicket (1,"kent",DateTime.Now,
DateTime.Now.AddMinutes(30), false,UserRoles,"/") ;

2. 生成身份验证票的Cookie
2.1 将身份验证票加密序列化成一个字符串
string HashTicket = FormsAuthentication.Encrypt (Ticket) ;
2.2 生成cookie
HttpCookie UserCookie = new HttpCookie(FormsAuthentication.FormsCookieName, HashTicket) ;
FormsAuthentication.FormsCookieName 是用来获取web.config中设置的身份验证cookie的名字,缺省为" .ASPXAUTH".
若身份验证票中的isPersistent属性设置为持久类,则这个cookie的Expires属性一定要设置,这样这个cookie才会被做为持久cookie保存到客户端的cookie文件中.
3. 将身份验证票Cookie输出到客户端
通过Response.Cookies.Add(UserCookie) 将身份验证票Cookie附加到输出的cookie集合中,发送到客户端.
4. 重定向到用户申请的初试页面.

验证部分代码(这部分代码是在login.aspx页面上点击了登录按钮事件处理代码):

private void Buttonlogin_Click(object sender, System.EventArgs e)
{
     string user = TextBoxUser.Text; //读取用户名
     string password = TextBoxPassword.Text; //读取密码
     if(Confirm(user,password) == true) //confirm方法用来验证用户合法性的
    {
         string userRoles = UserToRole(user); //调用UserToRole方法来获取role字符串
        
FormsAuthenticationTicket Ticket = new FormsAuthenticationTicket
(1,user,DateTime.Now,          DateTime.Now.AddMinutes(30),
false,userRoles,"/") ; //建立身份验证票对象
         string HashTicket = FormsAuthentication.Encrypt (Ticket) ; //加密序列化验证票为字符串
         HttpCookie UserCookie = new HttpCookie(FormsAuthentication.FormsCookieName, HashTicket) ;
//生成Cookie
          Context.Response.Cookies.Add (UserCookie) ; //输出Cookie
         Context.Response.Redirect (Context.Request["ReturnUrl"]) ; // 重定向到用户申请的初始页面
     }
    else
    {
        // 用户身份未被确认时的代码
    }
}
//此方法用来验证用户合法性的
private bool Confirm(string user,string password)
{
    //相应的代码
}
//此方法用来获得的用户对应的所有的role用逗号分割的一个字符串
private string UserToRole(string user)
{
    //相应的代码
}

二 基于角色访问授权

这里我们要做的是,将客户端保存的身份验证票中UserData中保存的表示角色的信息恢复到在服务端表示用户身份的GenericPrincipal对象中(记住,原来的验证过程中, GenericPrincipal对象只包含了用户信息,没有包含role信息)
一个Http请求的过程中,HttpApplication.AuthenticateRequest事件表示安全模块已建立用户标识,就是此用户的身份在web端已经建立起来, 在这个事件之后我们就可以获取用户身份信息了.
在HttpApplication.ResolveRequestCache
事件之前,asp.net开始取得用户请求的页面,建立HttpHandler控制点,这时就已经要验证用户的权限了,所以恢复用户角色的工作只能在
HttpApplication.AuthenticateRequest事件和
HttpApplication.ResolveRequestCache事件之间的过程中做.
我们选择Application_AuthorizeRequest事件中做这个工作,可以在global.asax文件中处理HttpApplication的所有的事件,代码如下:

protected void Application_AuthorizeRequest(object sender, System.EventArgs e)
{
    HttpApplication App = (HttpApplication) sender;
     HttpContext Ctx = App.Context ; //获取本次Http请求相关的HttpContext对象
    if (Ctx.Request.IsAuthenticated == true) //验证过的用户才进行role的处理
    {
        FormsIdentity Id = (FormsIdentity)Ctx.User.Identity ;
        FormsAuthenticationTicket Ticket = Id.Ticket ; //取得身份验证票
        string[] Roles = Ticket.UserData.Split (‘,’) ; //将身份验证票中的role数据转成字符串数组
        Ctx.User = new GenericPrincipal (Id, Roles) ; //将原有的Identity加上角色信息新建一个GenericPrincipal表示当前用户,这样当前用户就拥有了role信息
    }
}

1. 打开新的窗口并传送参数:

  传送参数:

response.write("<script>window.open('*.aspx?id="+this.DropDownList1.SelectIndex+"&id1="+...+"')</script>")
  接收参数:

string a = Request.QueryString("id");
string b = Request.QueryString("id1");

  2.为按钮添加对话框

Button1.Attributes.Add("onclick","return confirm('确认?')");
button.attributes.add("onclick","if(confirm('are you sure...?')){return true;}else{return false;}")

  3.删除表格选定记录

int intEmpID = (int)MyDataGrid.DataKeys[e.Item.ItemIndex];
string deleteCmd = "DELETE from Employee where emp_id = " + intEmpID.ToString()

  4.删除表格记录警告

private void DataGrid_ItemCreated(Object sender,DataGridItemEventArgs e)
{
 switch(e.Item.ItemType)
 {
  case ListItemType.Item :
  case ListItemType.AlternatingItem :
  case ListItemType.EditItem:
   TableCell myTableCell;
   myTableCell = e.Item.Cells[14];
   LinkButton myDeleteButton ;
   myDeleteButton = (LinkButton)myTableCell.Controls[0];
   myDeleteButton.Attributes.Add("onclick","return confirm('您是否确定要删除这条信息');");
   break;
  default:
   break;
 }

}

  5.点击表格行链接另一页

private void grdCustomer_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
 //点击表格打开
 if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
  e.Item.Attributes.Add("onclick","window.open('Default.aspx?id=" + e.Item.Cells[0].Text + "');");
}

  双击表格连接到另一页

  在itemDataBind事件中

if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
 string OrderItemID =e.item.cells[1].Text;
 ...
 e.item.Attributes.Add("ondblclick", "location.href='../ShippedGrid.aspx?id=" + OrderItemID + "'");
}

  双击表格打开新一页

if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
 string OrderItemID =e.item.cells[1].Text;
 ...
 e.item.Attributes.Add("ondblclick", "open('../ShippedGrid.aspx?id=" + OrderItemID + "')");
}
6.表格超连接列传递参数

<asp:HyperLinkColumn Target="_blank" headertext="ID号" DataTextField="id" NavigateUrl="aaa.aspx?id='
 <%# DataBinder.Eval(Container.DataItem, "数据字段1")%>' & name='<%# DataBinder.Eval(Container.DataItem, "数据字段2")%>' />

  7.表格点击改变颜色

if (e.Item.ItemType == ListItemType.Item ||e.Item.ItemType == ListItemType.AlternatingItem)
{
 e.Item.Attributes.Add("onclick","this.style.backgroundColor='#99cc00';
    this.style.color='buttontext';this.style.cursor='default';");
}

  写在DataGrid的_ItemDataBound里

if (e.Item.ItemType == ListItemType.Item ||e.Item.ItemType == ListItemType.AlternatingItem)
{
e.Item.Attributes.Add("onmouseover","this.style.backgroundColor='#99cc00';
   this.style.color='buttontext';this.style.cursor='default';");
e.Item.Attributes.Add("onmouseout","this.style.backgroundColor='';this.style.color='';");
}


  8.关于日期格式

  日期格式设定

DataFormatString="{0:yyyy-MM-dd}"
  我觉得应该在itembound事件中

e.items.cell["你的列"].text=DateTime.Parse(e.items.cell["你的列"].text.ToString("yyyy-MM-dd"))
  9.获取错误信息并到指定页面

  不要使用Response.Redirect,而应该使用Server.Transfer

  e.g

// in global.asax
protected void Application_Error(Object sender, EventArgs e) {
if (Server.GetLastError() is HttpUnhandledException)
Server.Transfer("MyErrorPage.aspx");

//其余的非HttpUnhandledException异常交给ASP.NET自己处理就okay了 :)
}

  Redirect会导致post-back的产生从而丢失了错误信息,所以页面导向应该直接在服务器端执行,这样就可以在错误处理页面得到出错信息并进行相应的处理

  10.清空Cookie

Cookie.Expires=[DateTime];
Response.Cookies("UserName").Expires = 0

  11.自定义异常处理

//自定义异常处理类
using System;
using System.Diagnostics;

namespace MyAppException
{
 /// <summary>
 /// 从系统异常类ApplicationException继承的应用程序异常处理类。
 /// 自动将异常内容记录到Windows NT/2000的应用程序日志
 /// </summary>
 public class AppException:System.ApplicationException
 {
  public AppException()
  {
   if (ApplicationConfiguration.EventLogEnabled)LogEvent("出现一个未知错误。");
  }

 public AppException(string message)
 {
  LogEvent(message);
 }

 public AppException(string message,Exception innerException)
 {
  LogEvent(message);
  if (innerException != null)
  {
   LogEvent(innerException.Message);
  }
 }

 //日志记录类
 using System;
 using System.Configuration;
 using System.Diagnostics;
 using System.IO;
 using System.Text;
 using System.Threading;

 namespace MyEventLog
 {
  /// <summary>
  /// 事件日志记录类,提供事件日志记录支持
  /// <remarks>
  /// 定义了4个日志记录方法 (error, warning, info, trace)
  /// </remarks>
  /// </summary>
  public class ApplicationLog
  {
   /// <summary>
   /// 将错误信息记录到Win2000/NT事件日志中
   /// <param name="message">需要记录的文本信息</param>
   /// </summary>
   public static void WriteError(String message)
   {
    WriteLog(TraceLevel.Error, message);
   }

   /// <summary>
   /// 将警告信息记录到Win2000/NT事件日志中
   /// <param name="message">需要记录的文本信息</param>
   /// </summary>
   public static void WriteWarning(String message)
   {
    WriteLog(TraceLevel.Warning, message);  
   }

   /// <summary>
   /// 将提示信息记录到Win2000/NT事件日志中
   /// <param name="message">需要记录的文本信息</param>
   /// </summary>
   public static void WriteInfo(String message)
   {
    WriteLog(TraceLevel.Info, message);
   }
   /// <summary>
   /// 将跟踪信息记录到Win2000/NT事件日志中
   /// <param name="message">需要记录的文本信息</param>
   /// </summary>
   public static void WriteTrace(String message)
   {
    WriteLog(TraceLevel.Verbose, message);
   }

   /// <summary>
   /// 格式化记录到事件日志的文本信息格式
   /// <param name="ex">需要格式化的异常对象</param>
   /// <param name="catchInfo">异常信息标题字符串.</param>
   /// <retvalue>
   /// <para>格式后的异常信息字符串,包括异常内容和跟踪堆栈.</para>
   /// </retvalue>
   /// </summary>
   public static String FormatException(Exception ex, String catchInfo)
   {
    StringBuilder strBuilder = new StringBuilder();
    if (catchInfo != String.Empty)
    {
     strBuilder.Append(catchInfo).Append("\r\n");
    }
    strBuilder.Append(ex.Message).Append("\r\n").Append(ex.StackTrace);
    return strBuilder.ToString();
   }

   /// <summary>
   /// 实际事件日志写入方法
   /// <param name="level">要记录信息的级别(error,warning,info,trace).</param>
   /// <param name="messageText">要记录的文本.</param>
   /// </summary>
   private static void WriteLog(TraceLevel level, String messageText)
   {
    try
    {
     EventLogEntryType LogEntryType;
     switch (level)
     {
      case TraceLevel.Error:
       LogEntryType = EventLogEntryType.Error;
       break;
      case TraceLevel.Warning:
       LogEntryType = EventLogEntryType.Warning;
       break;
      case TraceLevel.Info:
       LogEntryType = EventLogEntryType.Information;
       break;
      case TraceLevel.Verbose:
       LogEntryType = EventLogEntryType.SuccessAudit;
       break;
      default:
       LogEntryType = EventLogEntryType.SuccessAudit;
       break;
     }

 
    EventLog eventLog = new EventLog("Application",
ApplicationConfiguration.EventLogMachineName,
ApplicationConfiguration.EventLogSourceName );
     //写入事件日志
     eventLog.WriteEntry(messageText, LogEntryType);

    }
   catch {} //忽略任何异常
  }
 } //class ApplicationLog
}

12.Panel 横向滚动,纵向自动扩展

<asp:panel style="overflow-x:scroll;overflow-y:auto;"></asp:panel>
  13.回车转换成Tab

<script language="javascript" for="document" event="onkeydown">
 if(event.keyCode==13
&& event.srcElement.type!='button' &&
event.srcElement.type!='submit' &&
    event.srcElement.type!='reset' &&
event.srcElement.type!=''&& event.srcElement.type!='textarea');

   event.keyCode=9;
</script>

onkeydown="if(event.keyCode==13) event.keyCode=9"

  
14.DataGrid超级连接列

DataNavigateUrlField="字段名" DataNavigateUrlFormatString="http://xx/inc/delete.aspx?ID={0}"
  
15.DataGrid行随鼠标变色

private void DGzf_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
 if (e.Item.ItemType!=ListItemType.Header)
 {
  e.Item.Attributes.Add( "onmouseout","this.style.backgroundColor=\""+e.Item.Style["BACKGROUND-COLOR"]+"\"");
  e.Item.Attributes.Add( "onmouseover","this.style.backgroundColor=\""+ "#EFF3F7"+"\"");
 }
}

  
16.模板列

<ASP:TEMPLATECOLUMN visible="False" sortexpression="demo" headertext="ID">
<ITEMTEMPLATE>
<ASP:LABEL
text='<%# DataBinder.Eval(Container.DataItem, "ArticleID")%>'
runat="server" width="80%" id="lblColumn" />
</ITEMTEMPLATE>
</ASP:TEMPLATECOLUMN>

<ASP:TEMPLATECOLUMN headertext="选中">
<HEADERSTYLE wrap="False" horizontalalign="Center"></HEADERSTYLE>
<ITEMTEMPLATE>
<ASP:CHECKBOX id="chkExport" runat="server" />
</ITEMTEMPLATE>
<EDITITEMTEMPLATE>
<ASP:CHECKBOX id="chkExportON" runat="server" enabled="true" />
</EDITITEMTEMPLATE>
</ASP:TEMPLATECOLUMN>

  后台代码

protected void CheckAll_CheckedChanged(object sender, System.EventArgs e)
{
 //改变列的选定,实现全选或全不选。
 CheckBox chkExport ;
 if( CheckAll.Checked)
 {
  foreach(DataGridItem oDataGridItem in MyDataGrid.Items)
  {
   chkExport = (CheckBox)oDataGridItem.FindControl("chkExport");
   chkExport.Checked = true;
  }
 }
 else
 {
  foreach(DataGridItem oDataGridItem in MyDataGrid.Items)
  {
   chkExport = (CheckBox)oDataGridItem.FindControl("chkExport");
   chkExport.Checked = false;
  }
 }
}

  
17.数字格式化

  【<%#Container.DataItem("price")%>的结果是500.0000,怎样格式化为500.00?】

<%#Container.DataItem("price","{0:¥#,##0.00}")%>

int i=123456;
string s=i.ToString("###,###.00");

18.日期格式化

  【aspx页面内:<%# DataBinder.Eval(Container.DataItem,"Company_Ureg_Date")%>

  显示为: 2004-8-11 19:44:28

  我只想要:2004-8-11 】

<%# DataBinder.Eval(Container.DataItem,"Company_Ureg_Date","{0:yyyy-M-d}")%>
  应该如何改?

  【格式化日期】

  取出来,一般是object((DateTime)objectFromDB).ToString("yyyy-MM-dd");

  【日期的验证表达式】

  A.以下正确的输入格式: [2004-2-29], [2004-02-29 10:29:39 pm], [2004/12/31]

^((\d{2}(([02468][048])|([13579][26]))[\-\/\s]?((((0?[13578])|(1[02]))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\-\/\s]?((0?[1-9])|([1-2][0-9])))))|(\d{2}(([02468][1235679])|([13579][01345789]))[\-\/\s]?((((0?[13578])|(1[02]))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\-\/\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))(\s(((0?[1-9])|(1[0-2]))\:([0-5][0-9])((\s)|(\:([0-5][0-9])\s))([AM|PM|am|pm]{2,2})))?$
  B.以下正确的输入格式:[0001-12-31], [9999 09 30], [2002/03/03]

^\d{4}[\-\/\s]?((((0[13578])|(1[02]))[\-\/\s]?(([0-2][0-9])|(3[01])))|(((0[469])|(11))[\-\/\s]?(([0-2][0-9])|(30)))|(02[\-\/\s]?[0-2][0-9]))$

  【大小写转换】

HttpUtility.HtmlEncode(string);
HttpUtility.HtmlDecode(string)

  
19.如何设定全局变量

  Global.asax中

  Application_Start()事件中

  添加Application[属性名] = xxx;

  就是你的全局变量

  20.怎样作到HyperLinkColumn生成的连接后,点击连接,打开新窗口?

  HyperLinkColumn有个属性Target,将器值设置成"_blank"即可.(Target="_blank")

  【ASPNETMENU】点击菜单项弹出新窗口

  在你的menuData.xml文件的菜单项中加入URLTarget="_blank",如:

<?xml version="1.0" encoding="GB2312"?>
<MenuData ImagesBaseURL="images/">
<MenuGroup>
<MenuItem Label="内参信息" URL="Infomation.aspx" >
<MenuGroup ID="BBC">
<MenuItem Label="公告信息" URL="Infomation.aspx" URLTarget="_blank" LeftIcon="file.gif"/>
<MenuItem Label="编制信息简报" URL="NewInfo.aspx" LeftIcon="file.gif" />
......

  最好将你的aspnetmenu升级到1.2版

  
21.读取DataGrid控件TextBox值

foreach(DataGrid dgi in yourDataGrid.Items)
{
 TextBox tb = (TextBox)dgi.FindControl("yourTextBoxId");
 tb.Text....
}

  23.在DataGrid中有3个模板列包含Textbox分别为 DG_ShuLiang (数量) DG_DanJian(单价) DG_JinE(金额)分别在5.6.7列,要求在录入数量及单价的时候自动算出金额即:数量*单价=金额还要求录入时限制为 数值型.我如何用客户端脚本实现这个功能?

  〖思归〗

<asp:TemplateColumn HeaderText="数量">
<ItemTemplate>
<asp:TextBox id="ShuLiang" runat='server' Text='<%# DataBinder.Eval(Container.DataItem,"DG_ShuLiang")%>'
onkeyup="javascript:DoCal()"
/>

<asp:RegularExpressionValidator
id="revS" runat="server" ControlToValidate="ShuLiang"
ErrorMessage="must be integer" ValidationExpression="^\d+$" />
</ItemTemplate>
</asp:TemplateColumn>

<asp:TemplateColumn HeaderText="单价">
<ItemTemplate>
<asp:TextBox id="DanJian" runat='server' Text='<%# DataBinder.Eval(Container.DataItem,"DG_DanJian")%>'
onkeyup="javascript:DoCal()"
/>

<asp:RegularExpressionValidator
id="revS2" runat="server" ControlToValidate="DanJian"
ErrorMessage="must be numeric" ValidationExpression="^\d+(\.\d*)?$"
/>

</ItemTemplate>
</asp:TemplateColumn>

<asp:TemplateColumn HeaderText="金额">
<ItemTemplate>
<asp:TextBox id="JinE" runat='server' Text='<%# DataBinder.Eval(Container.DataItem,"DG_JinE")%>' />
</ItemTemplate>
</asp:TemplateColumn><script language="javascript">
function DoCal()
{
 var e = event.srcElement;
 var row = e.parentNode.parentNode;
 var txts = row.all.tags("INPUT");
 if (!txts.length || txts.length < 3)
  return;

 var q = txts[txts.length-3].value;
 var p = txts[txts.length-2].value;

 if (isNaN(q) || isNaN(p))
  return;

 q = parseInt(q);
 p = parseFloat(p);

 txts[txts.length-1].value = (q * p).toFixed(2);
}
</script>

4.datagrid选定比较底下的行时,为什么总是刷新一下,然后就滚动到了最上面,刚才选定的行因屏幕的关系就看不到了。

page_load
page.smartNavigation=true

  
25.在Datagrid中修改数据,当点击编辑键时,数据出现在文本框中,怎么控制文本框的大小 ?

private void DataGrid1_ItemDataBound(obj sender,DataGridItemEventArgs e)
{
 for(int i=0;i<e.Item.Cells.Count-1;i++)
  if(e.Item.ItemType==ListItemType.EditType)
  {
   e.Item.Cells[i].Attributes.Add("Width", "80px")
  }
}

  
26.对话框

private static string ScriptBegin = "<script language=\"JavaScript\">";
private static string ScriptEnd = "</script>";

public static void ConfirmMessageBox(string PageTarget,string Content)
{
 string ConfirmContent="var retValue=window.confirm('"+Content+"');"+"if(retValue){window.location='"+PageTarget+"';}";

 ConfirmContent=ScriptBegin + ConfirmContent + ScriptEnd;

 Page ParameterPage = (Page)System.Web.HttpContext.Current.Handler;
 ParameterPage.RegisterStartupScript("confirm",ConfirmContent);
 //Response.Write(strScript);
}

  27. 将时间格式化:string aa=DateTime.Now.ToString("yyyy年MM月dd日");

  1.1 取当前年月日时分秒

currentTime=System.DateTime.Now;
  1.2 取当前年

int 年= DateTime.Now.Year;
  1.3 取当前月

int 月= DateTime.Now.Month;
  1.4 取当前日

int 日= DateTime.Now.Day;
  1.5 取当前时

int 时= DateTime.Now.Hour;
  1.6 取当前分

int 分= DateTime.Now.Minute;
  1.7 取当前秒

int 秒= DateTime.Now.Second;
  1.8 取当前毫秒

int 毫秒= DateTime.Now.Millisecond;
  
28.自定义分页代码:

  先定义变量 :

public static int pageCount; //总页面数
public static int curPageIndex=1; //当前页面

  下一页:

if(DataGrid1.CurrentPageIndex < (DataGrid1.PageCount - 1))
{
 DataGrid1.CurrentPageIndex += 1;
 curPageIndex+=1;
}

bind(); // DataGrid1数据绑定函数

  上一页:

if(DataGrid1.CurrentPageIndex >0)
{
 DataGrid1.CurrentPageIndex += 1;
 curPageIndex-=1;
}

bind(); // DataGrid1数据绑定函数

  直接页面跳转:

int a=int.Parse(JumpPage.Value.Trim());//JumpPage.Value.Trim()为跳转值

if(a<DataGrid1.PageCount)
{
 this.DataGrid1.CurrentPageIndex=a;
}

bind();
29.DataGrid使用:

  添加删除确认:

private void DataGrid1_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
 foreach(DataGridItem di in this.DataGrid1.Items)
 {
  if(di.ItemType==ListItemType.Item||di.ItemType==ListItemType.AlternatingItem)
  {
   ((LinkButton)di.Cells[8].Controls[0]).Attributes.Add("onclick","return confirm('确认删除此项吗?');");
  }
 }
}

  样式交替:

ListItemType itemType = e.Item.ItemType;

if (itemType == ListItemType.Item )
{
 e.Item.Attributes["onmouseout"] = "javascript:this.style.backgroundColor='#FFFFFF';";
 e.Item.Attributes["onmouseover"] = "javascript:this.style.backgroundColor='#d9ece1';cursor='hand';" ;
}
else if( itemType == ListItemType.AlternatingItem)
{
 e.Item.Attributes["onmouseout"] = "javascript:this.style.backgroundColor='#a0d7c4';";
 e.Item.Attributes["onmouseover"] = "javascript:this.style.backgroundColor='#d9ece1';cursor='hand';" ;
}

  添加一个编号列:

DataTable dt= c.ExecuteRtnTableForAccess(sqltxt); //执行sql返回的DataTable
DataColumn dc=dt.Columns.Add("number",System.Type.GetType("System.String"));

for(int i=0;i<dt.Rows.Count;i++)
{
 dt.Rows[i]["number"]=(i+1).ToString();
}

DataGrid1.DataSource=dt;
DataGrid1.DataBind();

  DataGrid1中添加一个CheckBox,页面中添加一个全选框

private void CheckBox2_CheckedChanged(object sender, System.EventArgs e)
{
 foreach(DataGridItem thisitem in DataGrid1.Items)
 {
  ((CheckBox)thisitem.Cells[0].Controls[1]).Checked=CheckBox2.Checked;
 }
}

  将当前页面中DataGrid1显示的数据全部删除

foreach(DataGridItem thisitem in DataGrid1.Items)
{
 if(((CheckBox)thisitem.Cells[0].Controls[1]).Checked)
 {
  string strloginid= DataGrid1.DataKeys[thisitem.ItemIndex].ToString();
  Del (strloginid); //删除函数
 }
}

  30.当文件在不同目录下,需要获取数据库连接字符串(如果连接字符串放在Web.config,然后在Global.asax中初始化)

  在Application_Start中添加以下代码:

Application["ConnStr"]=this.Context.Request.PhysicalApplicationPath+ConfigurationSettings.
   AppSettings["ConnStr"].ToString();

  31. 变量.ToString()

  字符型转换 转为字符串

12345.ToString("n"); //生成 12,345.00
12345.ToString("C"); //生成 ¥12,345.00
12345.ToString("e"); //生成 1.234500e+004
12345.ToString("f4"); //生成 12345.0000
12345.ToString("x"); //生成 3039 (16进制)
12345.ToString("p"); //生成 1,234,500.00%

  32、变量.Substring(参数1,参数2);

  截取字串的一部分,参数1为左起始位数,参数2为截取几位。 如:string s1 = str.Substring(0,2);

  
33.在自己的网站上登陆其他网站:(如果你的页面是通过嵌套方式的话,因为一个页面只能有一个FORM,这时可以导向另外一个页面再提交登陆信息)

<SCRIPT language="javascript">
<!--
 function gook(pws)
 {
  frm.submit();
 }
//-->

</SCRIPT> <body leftMargin="0" topMargin="0" onload="javascript:gook()" marginwidth="0" marginheight="0">
<form name="frm" action=" http://220.194.55.68:6080/login.php?retid=7259 " method="post">
<tr>
<td>
<input id="f_user" type="hidden" size="1" name="f_user" runat="server">
<input id="f_domain" type="hidden" size="1" name="f_domain" runat="server">
<input class="box" id="f_pass" type="hidden" size="1" name="pwshow" runat="server">

<INPUT id="lng" type="hidden" maxLength="20" size="1" value="5" name="lng">
<INPUT id="tem" type="hidden" size="1" value="2" name="tem">

</td>

</tr>

</form>

  文本框的名称必须是你要登陆的网页上的名称,如果源码不行可以用vsniffer 看看。

  下面是获取用户输入的登陆信息的代码:

string name;
name=Request.QueryString["EmailName"];

try
{
 int a=name.IndexOf("@",0,name.Length);
 f_user.Value=name.Substring(0,a);
 f_domain.Value=name.Substring(a+1,name.Length-(a+1));
 f_pass.Value=Request.QueryString["Psw"];
}

catch
{
 Script.Alert("错误的邮箱!");
 Server.Transfer("index.aspx");
}


任何编程模型都有常见的性能缺陷,ASP.NET 也不例外。本节描述一些可避免在代码中出现性能瓶颈的方法。

  1. 在未使用时禁用会话状态

    并非所有的应用程序或页都要求基于每个用户的会话状态。如果不需要,可将其完全禁用。这可以通过以下页级别指令轻松实现:

    <%@ Page EnableSessionState="false" %>

    注意:如果页需要访问会话变量但不创建或修改它们,请将指令值设置为 ReadOnly。还可为 XML Web 服务方法禁用会话状态。请参阅 XML Web 服务一节中的使用对象和内部对象

  2. 慎重选择会话状态提供程序
    ASP.NET 为存储应用程序的会话数据提供了三种不同的方法:进程内会话状态、作为 Windows 服务的进程外会话状态和 SQL
    数据库中的进程外会话状态。每种方法都有自己的优点,但进程内会话状态是迄今为止速度最快的解决方案。如果仅在会话状态中存储少量易失数据,则应使用进程
    内提供程序。进程外解决方案主要用于 Web 花园和 Web 农场方案,或用于当服务器/进程重新启动时不能丢失数据的情况。

  3. 避免与服务器间的过多往返行程:Web 窗体页框架是 ASP.NET 的最佳功能之一,因为它可以显著减少为完成某项任务所需编写的代码量。使用服务器控件和回发事件处理模型的页元素编程访问无疑是最省时的功能。但是,对这些功能的使用存在着适当和不适当的方法,了解何时使用它们是适当的很重要。

    应用程序通常仅在检索数据或存储数据时才需要往返于服务器。多数数据操作可在往返行程间在客户端进行。例如在用户提交数据前,通常可以在客户端验证窗体项。通常,如果不需要将信息中继回服务器,则不应往返于服务器。

    如果编写自己的服务器控件,请考虑让它们为上级(支持 ECMAScript)浏览器呈现客户端代码。通过采用“智能”控件,可显著减少对 Web 服务器的不必要点击次数。

  4. 使用 Page.IsPostback 避免往返行程上的额外工作:如果处理服务器控件回发,通常需要在第一次请求页时执行代码,该代码不同于激发事件时用于往返行程的代码。如果检查 Page.IsPostBack 属性,则代码可按条件执行,具体取决于是否有对页的初始请求或对服务器控件事件的响应。这样做似乎很明显,但实际上可以忽略此项检查而不更改页的行为。例如:
    <script language="VB" runat="server">

    Public ds As DataSet
    ...

    Sub Page_Load(sender As Object, e As EventArgs)
    ' ...set up a connection and command here...
    If Not (Page.IsPostBack)
    Dim query As String = "select * from Authors where FirstName like '%JUSTIN%'"
    myCommand.Fill(ds, "Authors")
    myDataGrid.DataBind()
    End If
    End Sub

    Sub Button_Click(sender As Object, e As EventArgs)
    Dim query As String = "select * from Authors where FirstName like '%BRAD%'"
    myCommand.Fill(ds, "Authors")
    myDataGrid.DataBind()
    End Sub

    </script>

    <form runat="server">
    <asp:datagrid datasource='<%# ds.Tables["Authors"].DefaultView %>' runat="server"/><br>
    <asp:button onclick="Button_Click" runat="server"/>
    </form>
    VB

    Page_Load 事件对所有请求都执行,因此检查了 Page.IsPostBack,以便在处理 Button_Click 事件回发时不执行第一个查询。请注意,即使没有此检查,页的行为也不会改变,因为第一个查询中的绑定会被事件处理程序中的 DataBind 调用推翻。记住,在编写页时会很容易忽略这个简单的性能改进。

  5. 谨慎适当地使用服务器控件:尽管服务器控件使用起来非常容易,但它并不总是最佳选择。许多情况下,简单的呈现或数据绑定替换可以完成同样的事情。例如:
    <script language="VB" runat="server">

    Public imagePath As String
    Sub Page_Load(sender As Object, e As EventArgs)
    '...retrieve data for imagePath here...
    DataBind()
    End Sub

    </script>

    <%--the span and img server controls are unecessary...--%>
    The path to the image is: <span innerhtml='<%# imagePath %>' runat="server"/><br>
    <img src='<%# imagePath %>' runat="server"/>

    <br><br>

    <%-- use databinding to substitute literals instead...--%>
    The path to the image is: <%# imagePath %><br>
    <img src='<%# imagePath %>' />

    <br><br>

    <%-- or a simple rendering expression...--%>
    The path to the image is: <%= imagePath %><br>
    <img src='<%= imagePath %>' />
    VB

    在此示例中,不需要服务器控件将值代入发送回客户端的结果 HTML。在许多其他情况下此方法同样适用,甚至在服务器控件模板中。但是,如果要以编程方式操作控件属性、从中处理事件或利用其状态保存,则服务器控件更合适。应检查服务器控件的使用,并查找可优化的代码。

  6. 避免过多的服务器控件视图状态:自动状态管理是一种功能,它使服务器控件能够在往返行程中重新填充它们的值,而不要求编写任何代码。但是,此功能并不能任意使用,因为控件状态是在隐藏的窗体字段中传入和传出服务器的。应当明白 ViewState 何时有帮助,何时没有。例如,如果在每个往返行程中将控件绑定到数据(如第四条提示中的数据网格示例所示),则不要求控件维护它的视图状态,因为无论如何都将擦除任何重新填充的数据。

    默认情况下,为所有的服务器控件启用 ViewState。若要禁用它,请将控件的 EnableViewState 属性设置为 false,如下例所示:

    <asp:datagrid EnableViewState="false" datasource="..." runat="server"/>

    还可在页级别关闭 ViewState。这在根本不从页回发时非常有用,如下例所示:

    <%@ Page EnableViewState="false" %>

    注意,User Control 指令也支持此属性。若要分析页上的服务器控件使用的视图状态量,请启用跟踪并查看“控件层次结构”表中的“视图状态”列。有关跟踪功能及如何启用它的更多信息,请参阅应用程序级别的跟踪记录功能。

  7. 对字符串连接使用 Response.Write:在页面或用户控件中对字符串连接使用 HttpResponse.Write 方法。该方法提供非常有效的缓冲和连接服务。但是,如果您打算执行大量的连接,则使用下列示例中的方法(即多次调用 Response.Write)来连接字符串比仅调用一次 Response.Write 方法要快。
    Response.Write("a")
    Response.Write(myString)
    Response.Write("b")
    Response.Write(myObj.ToString())
    Response.Write("c")
    Response.Write(myString2)
    Response.Write("d")
    VB

  8. 不要依赖代码中的异常:异常非常浪费资源,应在代码中尽量避免。绝不要将异常作为控制常规程序流的方法。如果可
    以在代码中检测到会导致异常的条件,就应该那样做,而不要等到捕捉异常后再处理该条件。常见的方案包括:检查空值,分配给将分析为数字值的字符串,或在应
    用数学运算前检查特定值。例如:

    ' Consider changing this:

    Try
    result = 100 / num

    Catch (e As Exception)
    result = 0
    End Try

    // To this:

    If Not (num = 0)
    result = 100 / num
    Else
    result = 0
    End If
    VB

  9. 在 Visual Basic 或 JScript 代码中使用早期绑定:Visual
    Basic、VBScript 和 JScript
    的优点之一是它们无类型的特性。只需使用它们即可创建变量,并不需要显式的类型声明。从一种类型分配到另一种类型时,同样会自动执行转换。这既是优点也是
    缺点,因为就性能而言,晚期绑定虽很方便但很昂贵。

    Visual Basic 语言现在通过使用特殊的 Option Strict 编译器指令来支持类型安全编程。为了向后兼容,默认情况下 ASP.NET 不启用 Option Strict。但为了获得最佳性能,应在页上使用 Strict 属性或使用 Control 指令,为页启用 Option Strict

    <%@ Page Language="VB" Strict="true" %>

    <%

    Dim B
    Dim C As String

    ' This causes a compiler error:
    A = "Hello"

    ' This causes a compiler error:
    B = "World"

    ' This does not:
    C = "!!!!!!"

    ' But this does:
    C = 0

    %>

    JScript 也支持无类型编程,但它不提供强制早期绑定的编译器指令。满足以下条件的变量是后期绑定的:

    • 被显式声明为对象。
    • 是无类型声明的类的字段。
    • 是无显式类型声明的专用函数/方法成员,并且无法从其使用推断出类型。

    最后一个特点非常复杂。如果 JScript 编译器能够根据使用变量的方式推断出其类型就会进行优化。在下面的示例中,变量 A 为早期绑定,而变量 B 为后期绑定:

    var A;
    var B;

    A = "Hello";
    B = "World";
    B = 0;

    为获得最佳性能,请将 JScript 变量声明为具有类型。例如,“var A : String”。

  10. 将大量调用的 COM 组件移植为托管代码:.NET 框架一种非常容易的方法与传统的 COM
    组件相互操作。其优点是可以在保留现有代码的同时利用新的平台。但是,在某些情况下,保留旧组件的性能成本超出了将组件迁移到托管代码的费用。每种情况都
    非常特别,而决定所需更改的内容的最佳方法是测量站点的性能。但是,通常 COM
    交互性的性能影响与函数调用的次数或从非托管代码封送到托管代码的数据量成比例。由于各层间的通讯数,需要同大量调用交互的组件称为“chatty”。应
    考虑将这种组件移植为完全托管的代码,以从 .NET 平台提供的性能收益中获益。或者可以考虑重新设计组件,以请求更少的调用或一次封送更多数据。

  11. 将 SQL 存储过程用于数据访问:在 .NET 框架提供的所有数据访问方法中,基于 SQL
    的数据访问是生成性能最好的可缩放 Web 应用程序的最佳选择。使用托管 SQL
    提供程序时,可通过使用编译的存储过程而不是特殊查询,获得额外的性能提高。有关使用 SQL 存储过程的示例,请参考本教程的服务器端的数据访问一节。

  12. 使用 SqlDataReader 获得快进只读数据游标SqlDataReader 对象对从 SQL 数据库中检索的数据提供前进只读游标。如果 SqlDataReader 适合于您的情况,则它是一个比 DataSet 更好的选择。因为 SqlDataReader 支持 IEnumerable 接口,甚至还可以绑定服务器控件。有关使用 SqlDataReader 的示例,请参阅本教程的服务器端数据访问一节。

  13. 尽可能缓存数据和输出:ASP.NET
    编程模型提供了一个简单的机制,在不需要为每个请求动态计算页输出或数据时缓存它们。在设计页时可以考虑用缓存来优化应用程序中那些预期有最大通信量的地
    方。适当地使用缓存可增强站点的性能,有时甚至可以增大一个数量级或更多,这是 .NET
    框架的任何其他功能无法企及的。有关如何使用缓存的更多信息,请参阅本教程的缓存服务一节。

  14. 为多处理器计算机启用 Web 花园:ASP.NET 进程模型帮助在多处理器计算机上启用可缩放性,将工作分发给多个进程(每个 CPU 一个),并且每个进程都将处理器关系设置为其 CPU。该技术称为 Web 园艺,它可以显著提高某些应用程序的性能。若要了解如何启用 Web 园艺,请参考使用进程模型一节。

  15. 不要忘记禁用调试模式:ASP.NET 配置中的 <compilation> 节控制应用程序是否在调试模式中编译。调试模式严重降低性能。在部署生产应用程序或测量性能之前,始终记住禁用调试模式。有关调试模式的更多信息,请参考题为 SDK 调试器的章节。