初学C#+ASP.NET+Oracle时积累的备忘点滴之二
初学C#+ASP.NET+Oracle时积累的备忘点滴之二 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
{
}
TD { FONT-SIZE: 12px }.commentTextBox { FONT-SIZE: 12px }.userData { BEHAVIOR: url(#default#userdata) }