初学C#+ASP.NET+Oracle时积累的备忘点滴之二[1]

[入库:2006年2月23日] [更新:2007年3月24日]

本文简介:

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中加入
  prote

本文关键:初学C#+ASP.NET+Oracle时积累的备忘点滴之二
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top