避免不必要的执行操作
Page_Load
和
IsPostBack
void Page_Load(Object sender, EventArgs e) {
// ...set up a connection and command here...
if (!Page.IsPostBack) {
String query = "select * from Authors where FirstName like '%JUSTIN%'";
myCommand.Fill(ds, "Authors");
myDataGrid.DataBind();
}
}
void Button_Click(Object sender, EventArgs e) {
String query = "select * from Authors where FirstName like '%BRAD%'";
myCommand.Fill(ds, "Authors");
myDataGrid.DataBind();
}
关闭不必要的 Session 状态
<%@ Page EnableSessionState="false" %>
注意使用
Server Control
不必要时可以不使用
Server Control
不必要时可以关闭
ViewState
<asp:datagrid
EnableViewState="false“ runat="server"/>
<%@ Page EnableViewState="false" %>
不要用 Exception 控制程序流程
try {
result = 100 / num;
}
catch (Exception e) {
result = 0;
}
if (num != 0)
result = 100 / num;
else
result = 0;
禁用 VB 和 JScript 动态数据类型
<%@ Page Language="VB" Strict="true" %>
使用存储过程数据访问
只读数据访问不要使用
DataSet
使用
SqlDataReader
代替
DataSet
SqlDataReader
是
read-only, forward-only
关闭
ASP.NET
的
Debug
模式
使用
ASP.NET Output Cache
缓冲数据
页面缓冲
<%@OutputCache%>
Duration
VaryByParam
片断缓冲
VaryByControl
数据缓冲
过期依赖条件
Cache.Insert("MyData", Source, new
CacheDependency(Server.MapPath("authors.xml")));
Cache.Insert("MyData", Source, null,
DateTime.Now.AddHours(1), TimeSpan.Zero);
Cache.Insert("MyData", Source, null, DateTime.MaxValue,
TimeSpan.FromMinutes(20));