1、加入站点计数:
在globalv.asax.cs中加入
protected void application_start(object sender, eventargs e)
{
application[“globalcounter”]=0;
}
protected void application_beginrequest(object sender, eventargs e)
{
application.lock();
application[“globalcounter”]=(int)application[“globalcounter”]+1;
application.unlock();
}
在需要显示的页面加入如下语句:
//访问量统计计数器
labelvisitcount.text=application[“globalcounter”].tostring();
-----------------------------------------------
2、一个session使用实例:(导入using system.web.sessionstate;和using system.web.security;命名空间并使用forms验证的例子)
在web.config中加入
<authentication mode=”forms”>
<forms loginurl=”login.aspx” name=”.loginauthen” timeout=”60″ protection=”all”/>
</authentication>
<sessionstate
mode=”stateserver”
stateconnectionstring=”tcpip=127.0.0.1:42424″
sqlconnectionstring=”data source=127.0.0.1;trusted_connection=yes”
cookieless=”false”
timeout=”30″
/>
启动asp.net state service服务,并视情况决定该服务的登陆帐户与是否与服务器桌面交互(运行权限问题)
在globalv.asax.cs中加入
protected void session_start(object sender, eventargs e)
{
//配置登录session:
// session[“loginvalidate”]=false;
session.add(“loginnamevalidate”,null);
}
//session的应用程序级清除
protected void application_end(object sender, eventargs e)
{
try
{
if ((bool)session[“loginnamevalidate”]!=false)
{
session[“loginnamevalidate”]=false;
session.remove(“loginnamevalidate”);
}
}
catch
{
}
finally
{
session.removeall();
}
}
//session的页面级正常退出:
private void buttonquit_click(object sender, system.eventargs e)
{
//release the resources when quit.
try
{
formsauthentication.signout();
response.redirect(“cancelall.aspx”);
session[“loginnamevalidate”]=false;
session.remove(“loginnamevalidate”);
}
catch
{
}
finally
{
session.removeall();
}
}
补充://页面级session的非正常退出如下,除此外还可以视情况将错误记录到系统应用程序错误日志中
private void onunload(system.eventargs e)
{
{
//release the resources when quit.
try
{
formsauthentication.signout();
response.redirect(“cancelall.aspx”);
session[“loginnamevalidate”]=false;
session.remove(“loginnamevalidate”);
}
catch
{
}
finally
{
//视情况看是否要退出程序,据此决定是否添加session.removeall();
}
}
}
-----------------------------------------------
3、一个错误记录到系统应用程序错误日志中实例:
在globalv.asax.cs中加入
protected void application_error(object sender, eventargs e)
{
//错误后记录到系统日志中;注意导入system.diagnostics命名空间;注意在
//[hkey_local_machine\system\currentcontrolset\services\eventlog\application建个该程序错误日志分支。
try
{
string errormessage=”我的系统有错误发生,详细错误是”+server.getlasterror();
server.clearerror();
string logname=”myapplicationlog”;
string sourcename=”我的错误日志”;
if(!(eventlog.sourceexists(sourcename)))
{
eventlog.createeventsource(sourcename,logname);
}
//insert into eventlog:
eventlog mylog=new eventlog();
mylog.source=sourcename;
mylog.writeentry(errormessage,eventlogentrytype.error);
}
catch
{
}
finally
{
//视情转到错误页面response.redirect(“error.aspx”);
}
}
记录错误日志如果不改注册表将会出现asp.net帐户访问注册表的权限问题,可以手工改注册表或做成.reg文件预先导入。
-----------------------------------------------
4、datagrid中根据某个条件更改该行颜色的解决方案
//如要处理全球化日期格式需要导入system.globalization;
//定义个datatable,此处名称mytable可变,用于以后绑定到datagrid。
//以后需要使用时正常填充数据集然后如下操作导入数据集的表到mytable,进行更改(更改颜色、条件、字段等)后绑定到目的datagrid
protected system.data.datatable mytable;
try
{
//先填充数据集再操作其中数据表再绑定到datagrid中,下列操作与sql分离便于复用
oledbdataadapter1.fill(dataset1);
datacolumn mycolumn;
datarow mydatarow;
mytable=new datatable(“mytable”);
mycolumn = new datacolumn();
mycolumn.datatype = system.type.gettype(“system.string”);
mycolumn.columnname = “id”;
mytable.columns.add(mycolumn);
mycolumn = new datacolumn();
mycolumn.datatype = system.type.gettype(“system.string”);
mycolumn.columnname = “title”;
mytable.columns.add(mycolumn);
mycolumn = new datacolumn();
mycolumn.datatype = system.type.gettype(“system.string”);
mycolumn.columnname = “sdate”;
mytable.columns.add(mycolumn);
mycolumn = new datacolumn();
mycolumn.datatype = system.type.gettype(“system.string”);
mycolumn.columnname = “fdate1”;
mytable.columns.add(mycolumn);
mycolumn = new datacolumn();
mycolumn.datatype = system.type.gettype(“system.string”);
mycolumn.columnname = “cbunit”;
mytable.columns.add(mycolumn);
mycolumn = new datacolumn();
mycolumn.datatype = system.type.gettype(“system.string”);
mycolumn.columnname = “result”;
mytable.columns.add(mycolumn);
for (int i=0;i< dataset1.tables[0].rows.count;i++)
{
mydatarow=mytable.newrow();
string stringfdate1=dataset1.tables[0].rows[i][“fdate1”].tostring().trim();
cultureinfo cultureinfofdate1=new cultureinfo(“zh-cn”);
datetime datetimefdate1=datetime.parse(stringfdate1,cultureinfofdate1);
timespan ts=datetime.now-datetimefdate1;
mydatarow[“id”] =datasetdaoqibanjian1.tables[0].rows[i][“id”].tostring();
mydatarow[“title”] =datasetdaoqibanjian1.tables[0].rows[i][“title”].tostring();
//此处示例为检查fdate1字段离当前日期差1、2、3周的相关行分别显示不同的颜色
if(ts.days<=7)
{
mydatarow[“sdate”] = “<font color=green>”+datetime.parse(dataset1.tables[0].rows[i][“sdate”].tostring()).tolongdatestring()+”</font>”;
mydatarow[“fdate1”] = “<font color=green>”+datetime.parse(dataset1.tables[0].rows[i][“fdate1″].tostring()).tolongdatestring()+”</font>”;
mydatarow[“cbunit”] = “<font color=green>”+dataset1.tables[0].rows[i][“cbunit”].tostring()+”</font>”;
mydatarow[“result”] = “<font color=green>”+dataset1.tables[0].rows[i][“result”].tostring()+”</font>”;
}
else
{
if(ts.days>7 && ts.days<=14)
{
mydatarow[“sdate”] = “<font color=darkorange>”+datetime.parse(dataset1.tables[0].rows[i][“sdate”].tostring()).tolongdatestring()+”</font>”;
mydatarow[“fdate1”] = “<font color=darkorange>”+datetime.parse(dataset1.tables[0].rows[i][“fdate1″].tostring()).tolongdatestring()+”</font>”;
mydatarow[“cbunit”] = “<font color=darkorange>”+dataset1.tables[0].rows[i][“cbunit”].tostring()+”</font>”;
mydatarow[“result”] = “<font color=darkorange>”+dataset1.tables[0].rows[i][“result”].tostring()+”</font>”;
}
else
{
if(ts.days>14)
{
mydatarow[“sdate”] = “<font color=red>”+datetime.parse(dataset1.tables[0].rows[i][“sdate”].tostring()).tolongdatestring()+”</font>”;
mydatarow[“fdate1”] = “<font color=red>”+datetime.parse(dataset1.tables[0].rows[i][“fdate1″].tostring()).tolongdatestring()+”</font>”;
mydatarow[“cbunit”] = “<font color=red>”+dataset1.tables[0].rows[i][“cbunit”].tostring()+”</font>”;
mydatarow[“result”] = “<font color=red>”+dataset1.tables[0].rows[i][“result”].tostring()+”</font>”;
}
else
{
}
}
}
mytable.rows.add(mydatarow);
}
//把mytable绑定到datagrid1表
datagrid1.datasource=mytable;
datagrid1.databind();
}
catch
{
}
finally
{
}