(2)
由于某些页面不需要会话状态,则可以将会话状态禁用
:
<%@ Page EnableSessionState="false" %>
如果页面需要访问会话变量但不允许修改它们,可以设置页面会话状态为只读
: <%@ Page EnableViewState="false" %>
(3)Page.IsPostBack
表示是否是从客户端返回的
.
初次运行时,不是从客户端返回,它的值为
false,
当触发页面上的事件或刷新页面时,
Page.IsPostBack
由于是回发的
,
值变为
true;
一般在
: Page_Load
方法中用
:
private void Page_Load(Object sender,EventArgs e)
{
if(!Page.IsPostBack)
{
....; //
初始化页面的代码。这些代码第一次页面初始化时执行,当第二次回发时,
//
不会再执行。提高效率。
}
}
往往很多时候不得不用
IsPostBack,
因为有些控件初始化后,要保持它的状态
.
例如
: DropDownList,
如果每次都初始化,则用户无论选择其选项,都会被初始化为默认值
.
(4)
避免使用服务器控件
1.
一般的静态显示信息,尽量不要用服务端控件显示
.
因为服务端控件需要回发服务端执行
,
会降低程序执行效率
,
一般用
<DIV>
显示即可
.
如果用了服务端控件,将
runat="server"
去掉
,
也会提高效率
.
2.
禁用服务端控件的状态视图,有些控件不需要维护其状态,可以设置其属性
EnableViewState=false;
3.
在
Web.Config
文件中配置
:
(5)
避免使用
DataGrid,
功能强大的同时,增加了性能上的开销。一般用其它控件
: DataList
或
Repeater
控件能实现的,尽量不用
DataGrid.
(6)
字符串操作
1.
避免装箱操作
.
装箱操作运行效率比较低
3.
尽量少用
try语句,
此语句执行效率比较低
(
7
)
ADO.Net
使用方面优化
1.
数据库连接打开和关闭。
在需要连接时打开,当访问完数据库要立刻关闭连接
.
看两个代码段
:
I.
DataSet ds =
new DataSet();
SqlConnection MyConnection =
new SqlConnection("server=localhost; uid=sa; pwd=; database=NorthWind");
SqlCommand myCommand =
new SqlCommand(strSql,MyConnection);
SqlDataAdapter myAdapter=new SqlDataAdapter(queryStr,connectionStr);
MyConnection.Open(); //
打开连接
for(int i=0;i<1000;i++) //for
循环模拟取得数据前的商业逻辑操作
{
Thread.Sleep(1000);
}
myAdapter.Fill(ds);
for(int i=0;i<1000;i++) //for
循环模拟取得数据后的商业逻辑操作
{
Thread.Sleep(1000);
}
MyConnection.Close(); //
关闭连接
II.
DataSet ds =
new DataSet();
SqlConnection MyConnection =
new SqlConnection("server=localhost; uid=sa; pwd=; database=NorthWind");
SqlCommand myCommand =
new SqlCommand(strSql,MyConnection);
SqlDataAdapter myAdapter=new SqlDataAdapter(queryStr,connectionStr);
for(int i=0;i<1000;i++) //for
循环模拟取得数据前的商业逻辑操作
{
Thread.Sleep(1000);
}
MyConnection.Open(); //
打开连接
myAdapter.Fill(ds);
MyConnection.Close(); //
关闭连接
for(int i=0;i<1000;i++) for
循环模拟取得数据后的商业逻辑操作
{
Thread.Sleep(1000);
}
显示
II
代码比
I
代码好的多,
I
中早早占着连接不放,如果用户很多的话,容易出现连接池满情况。严重时出现死机现象。
2.
数据库查询
注意:并不一定存储过程一定比语句效率要高,如果商业逻辑很复杂的话,有时候用语句比存储过程效率要高
.
(8)
缓存优化(页面缓存和
API
缓存)
1.
使用页面缓存和片段缓存
<%@ OutputCache Duration="5" VaryByParam="None"%>
<%@ OutputCache Duration=60 VaryByParam=”TextBox1,TextBox2” %>
说明
: Duration
是设置
Cache
的过期时间
;
VarByParam
是设置是否根据参数而变化
,None
时所有参数使用同一
Cache,
设置
TextBox1
时则根据
TextBox1
的不同值分别缓存
;
当有多个参数时则要组合缓存
;
2.API
缓存。用于在应用程序中使用
I.
一个
Cache
使用的例子
:
见附件
II.
使用时注意
Page.Cache
和
HttpContext.Current.Cache
区别
:
它们指的同一个对象,在
Page
里,用
Page.Cache
,如果在
global.asax
或自己的类里用
:HttpContext.Current.Cache
在有些事件中,由于其没有
HttpContext
,就用
HttpRuntime.Cache.
附件:
/// <head>
/// <function>
///
存储类
(
存储
UserInfo
信息
)
/// </function>
/// <description>
///
用
Cache
存储用户信息
///
在指定间隔
(TimeOut)
内取,则可以从
Cache
中取,
///
如果超出存储时间
,
则从数据库取用户信息数据
///
作為所有用户信息的存儲類
.
/// </description>
/// <author>
/// <name>ChengKing</name>
/// </author>
/// </head>
using System;
using System.Web;
using System.Web.Caching;
namespace Common
{
/// <summary>
///
存储类
(
存储
UserInfo
信息
)
/// </summary>
public class Storage
{
public Storage()
{
//
// TODO:
在此处添加构造函数逻辑
//
}
#region
方法
//
实现
“
一键一值
”
存储方法
,
最普通的存储方法
//
(
“
一键一值
”
指一个
Identify
存储一个值
,
下面还有一个
“
一键多值
”
方法,因为有时候需要一个键存储多个变量对象值)
public static bool InsertIdentify(string strIdentify,object Info)
{
if(strIdentify != null && strIdentify.Length != 0 && userInfo != null)
{
//
建立回调委托的一个实例
CacheItemRemovedCallback callBack =new CacheItemRemovedCallback(onRemove);
//
以
Identify
为标志,将
userInfo
存入
Cache
HttpContext.Current.Cache.Insert(strIdentify,userInfo,null,
System.DateTime.Now.AddSeconds(300),
System.Web.Caching.Cache.NoSlidingExpiration,
System.Web.Caching.CacheItemPriority.Default,
callBack);
return true;
}
else
{
return false;
}
}
//
判断存储的
"
一键一值
"
值是否还存在(有没有过期失效或从来都未存储过)
public static bool ExistIdentify(string strIdentify)
{
if(HttpContext.Current.Cache[strIdentify] != null)
{
return true;
}
else
{
return false;
}
}
//
插入
"
一键多值
"
方法
//***
其中
StorageInfType
是一个
Enum,
里面存有三种类型
: UserInf SysInf PageInf
//
这个枚举如下
:
/*
public enum StorageInfType
{
/// <summary>
用户信息
</summary>
UserInf = 0,
/// <summary>
页面信息
</summary>
PageInf = 1,
/// <summary>
系统信息
</summary>
SysInf = 2
}
//
此枚举是自己定义的
.
可根据需要定义不同的枚举
//
加个枚举目的是实现
“
一键多值
”
存储方法,事实上
Cache
中是存放了多个变量的,只不过被这个类封装了,
//
程序员感到就好像是
“
一键一值
”.
这样做目的是可以简化开发操作
,
否则程序员要存储几个变量就得定义几个
Identify.
public static bool InsertCommonInf(string strIdentify,StorageInfType enumInfType,object objValue)
{
if(strIdentify != null && strIdentify != "" && strIdentify.Length != 0 && objValue != null)
{
//RemoveCommonInf(strIdentify,enumInfType);
//
建立回调委托的一个实例
CacheItemRemovedCallback callBack =new CacheItemRemovedCallback(onRemove);
if(enumInfType == StorageInfType.UserInf)
{
//
以用户
UserID+
信息标志
(StorageInfType
枚举
)
,将
userInfo
存入
Cache
HttpContext.Current.Cache.Insert(strIdentify+StorageInfType.UserInf.ToString(),objValue,null,
System.DateTime.Now.AddSeconds(18000), //
单位秒
System.Web.Caching.Cache.NoSlidingExpiration,
System.Web.Caching.CacheItemPriority.Default,
callBack);
}
if(enumInfType == StorageInfType.PageInf)
{
//
以用户
UserID+
信息标志
(StorageInfType
枚举
)
,将
PageInfo
存入
Cache
HttpContext.Current.Cache.Insert(strIdentify+StorageInfType.PageInf.ToString(),objValue,null,
System.DateTime.Now.AddSeconds(18000),
System.Web.Caching.Cache.NoSlidingExpiration,
System.Web.Caching.CacheItemPriority.Default,
callBack);
}
if(enumInfType == StorageInfType.SysInf)
{
//
以用户
UserID+
信息标志
(StorageInfType
枚举
)
,将
SysInfo
存入
Cache
HttpContext.Current.Cache.Insert(strIdentify+StorageInfType.SysInf.ToString(),objValue,null,
System.DateTime.Now.AddSeconds(18000),
System.Web.Caching.Cache.NoSlidingExpiration,
System.Web.Caching.CacheItemPriority.Default,
callBack);
}
return true;
}
return false;
}
//
读取
“
一键多值
”Identify
的值
public static bool ReadIdentify(string strIdentify,out UserInfo userInfo)
{
//
取出值
if((UserInfo)HttpContext.Current.Cache[strIdentify] != null)
{
userInfo = (UserInfo)HttpContext.Current.Cache[strIdentify];
if(userInfo == null)
{
return false;
}
return true;
}
else
{
userInfo = null;
return false;
}
}
//
手动移除
“
一键一值
”
对应的值
public static bool RemoveIdentify(string strIdentify)
{
//
取出值
if((UserInfo)HttpContext.Current.Cache[strIdentify] != null)
{
HttpContext.Current.Cache.Remove(strIdentify);
}
return true;
}
//
此方法在值失效之前调用,可以用于在失效之前更新数据库,或从数据库重新获取数据
private static void onRemove(string strIdentify, object userInfo,CacheItemRemovedReason reason)
{
}
#endregion
}
}