飛艷小屋

          程序--人生--哲學___________________歡迎艷兒的加入

          BlogJava 首頁 新隨筆 聯系 聚合 管理
            52 Posts :: 175 Stories :: 107 Comments :: 0 Trackbacks
           

          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
             {
             }
          1、DateTime 數字型
          System.DateTime currentTime=new System.DateTime();
          1.1
          取當前年月日時分秒
          currentTime=System.DateTime.Now;
          1.2
          取當前年
          int
          =currentTime.Year;
          1.3
          取當前月
          int
          =currentTime.Month;
          1.4
          取當前日
          int
          =currentTime.Day;
          1.5
          取當前時
          int
          =currentTime.Hour;
          1.6
          取當前分
          int
          =currentTime.Minute;
          1.7
          取當前秒
          int
          =currentTime.Second;
          1.8
          取當前毫秒
          int
          毫秒=currentTime.Millisecond;
          (變量可用中文)

          1.9
          取中文日期顯示——年月日時分
          string strY=currentTime.ToString("f"); //
          不顯示秒

          1.10
          取中文日期顯示_年月
          string strYM=currentTime.ToString("y");

          1.11
          取中文日期顯示_月日
          string strMD=currentTime.ToString("m");

          1.12
          取中文年月日
          string strYMD=currentTime.ToString("D");

          1.13
          取當前時分,格式為:1424
          string strT=currentTime.ToString("t");

          1.14
          取當前時間,格式為:2003-09-23T14:46:48
          string strT=currentTime.ToString("s");

          1.15
          取當前時間,格式為:2003-09-23 14:48:30Z
          string strT=currentTime.ToString("u");

          1.16
          取當前時間,格式為:2003-09-23 14:48
          string strT=currentTime.ToString("g");

          1.17
          取當前時間,格式為:Tue, 23 Sep 2003 14:52:40 GMT
          string strT=currentTime.ToString("r");

          1.18
          獲得當前時間 n 天后的日期時間
          DateTime newDay = DateTime.Now.AddDays(100);

          2
          Int32.Parse(變量) Int32.Parse("常量")
          字符型轉換 轉為32位數字型

          3
          變量.ToString()
          字符型轉換 轉為字符串
          12345.ToString("n"); //
          生成 12,345.00
          12345.ToString("C"); //
          生成 12,345.00
          12345.ToString("e"); //
          生成 1.234500e+004
          12345.ToString("f4"); //
          生成 12345.0000
          12345.ToString("x"); //
          生成 3039 (16進制)
          12345.ToString("p"); //
          生成 1,234,500.00%


          4
          、變量.Length 數字型
          取字串長度:
          如: string str="中國";
          int Len = str.Length ; //Len
          是自定義變量, str是求測的字串的變量名

          5
          System.Text.Encoding.Default.GetBytes(變量)
          字碼轉換 轉為比特碼
          如:byte[] bytStr = System.Text.Encoding.Default.GetBytes(str);
          然后可得到比特長度:
          len = bytStr.Length;

          6
          System.Text.StringBuilder("")
          字符串相加,(+號是不是也一樣?)
          如:System.Text.StringBuilder sb = new System.Text.StringBuilder("");
          sb.Append("
          中華");
          sb.Append("
          人民");
          sb.Append("
          共和國");

          7
          、變量.Substring(參數1,參數2);
          截取字串的一部分,參數1為左起始位數,參數2為截取幾位。
          如:string s1 = str.Substring(0,2);

          8
          String user_IP=Request.ServerVariables["REMOTE_ADDR"].ToString();
          遠程用戶IP地址

          9
          、穿過代理服務器取遠程用戶真實IP地址:
          if(Request.ServerVariables["HTTP_VIA"]!=null){
          string user_IP=Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();
          }else{
          string user_IP=Request.ServerVariables["REMOTE_ADDR"].ToString();
          }

          10
          Session["變量"];
          存取Session值;
          如,賦值: Session["username"]="小布什";

          取值: Object objName=Session["username"];
          String strName=objName.ToString();
          清空: Session.RemoveAll();

          11
          String str=Request.QueryString["變量"];
          用超鏈接傳送變量。
          如在任一頁中建超鏈接:<a href=Edit.aspx?fbid=23>點擊</a>
          Edit.aspx頁中取值:String str=Request.QueryString["fdid"];

          12
          DOC對象.CreateElement("新建節點名");
          創建XML文檔新節點

          13
          、父節點.AppendChild(子節點)
          將新建的子節點加到XML文檔父節點下

          14
          父節點.RemoveChild(節點);
          刪除節點

          15
          Response
          Response.Write("字串")
          Response.Write(
          變量)
          向頁面輸出。

          Response.Redirect("URL
          地址");
          跳轉到URL指定的頁面

          16
          char.IsWhiteSpce(字串變量,位數)——邏輯型
          查指定位置是否空字符;
          如:
          string str="
          中國 人民";
          Response.Write(char.IsWhiteSpace(str,2)); //
          結果為:True, 第一個字符是0位,2是第三個字符。

          17
          char.IsPunctuation('字符') --邏輯型
          查字符是否是標點符號
          如:Response.Write(char.IsPunctuation('A')); //返回:False

          18
          (int)'字符'
          把字符轉為數字,查代碼點,注意是單引號。
          如:
          Response.Write((int)'
          '); //結果為中字的代碼:20013

          19
          (char)代碼
          把數字轉為字符,查代碼代表的字符。
          如:
          Response.Write((char)22269); //
          返回字。

          20
          Trim()
          清除字串前后空格

          21
          、字串變量.Replace("子字串","替換為")
          字串替換
          如:
          string str="
          中國";
          str=str.Replace("
          ",""); //將國字換為央字
          Response.Write(str); //
          輸出結果為中央

          再如:(這個非常實用)

          string str="
          這是<script>腳本";
          str=str.Replace("<","<font><</font>"); //
          將左尖括號替換為<font> < </font> (或換為<,但估計經XML存諸后,再提出仍會還原)
          Response.Write(str); //
          顯示為:這是<script>腳本

          如果不替換,<script>將不顯示,如果是一段腳本,將運行;而替換后,腳本將不運行。
          這段代碼的價值在于:你可以讓一個文本中的所有HTML標簽失效,全部顯示出來,保護你的具有交互性的站點。
          具體實現:將你的表單提交按鈕腳本加上下面代碼:
          string strSubmit=label1.Text; //label1
          是你讓用戶提交數據的控件ID
          strSubmit=strSubmit.Replace("<","<font><</font>");
          然后保存或輸出strSubmit
          用此方法還可以簡單實現UBB代碼。

          22
          Math.Max(i,j)
          ij中的最大值
          int x=Math.Max(5,10); // x將取值 10

          23
          、字串對比一般都用: if(str1==str2){ } , 但還有別的方法:

          (1)
          string str1; str2
          //
          語法: str1.EndsWith(str2); __檢測字串str1是否以字串str2結尾,返回布爾值.:
          if(str1.EndsWith(str2)){ Response.Write("
          字串str1是以"+str2+"結束的"); }

          (2)

          //
          語法:str1.Equals(str2); __檢測字串str1是否與字串str2相等,返回布爾值,用法同上.

          (3)

          //
          語法 Equals(str1,str2); __檢測字串str1是否與字串str2相等,返回布爾值,用法同上.

          24
          IndexOf() LastIndexOf()
          查找字串中指定字符或字串首次(最后一次)出現的位置,返回索引值,如:
          str1.IndexOf("
          ") //查找str1中的索引值(位置)
          str1.IndexOf("
          字串")//查找字串的第一個字符在str1中的索引值(位置)
          str1.IndexOf("
          字串",3,2)//str14個字符起,查找2個字符,查找字串的第一個字符在str1中的索引值(位置)

          25
          Insert()
          在字串中指定索引位插入指定字符。如:
          str1.Insert(1,"
          ");str1的第二個字符處插入,如果str1="中國",插入后為中字國

          26
          PadLeft()PadRight()
          在字串左(或右)加空格或指定char字符,使字串達到指定長度,如:
          <%
          string str1="
          中國人";
          str1=str1.PadLeft(10,'1'); //
          無第二參數為加空格
          Response.Write(str1); //
          結果為“1111111中國人 字串長為10
          %>

          27
          Remove()
          從指定位置開始刪除指定數的字符
          <%
          string str1="
          我是薩達姆的崇拜者之一";
          Response.Write(str1.Remove(5,4)); //
          結果為我是薩達姆之一
          %>
          28.split
           Dim s As String = "asdf,asdf,asdf,asdf,asdf,asdf,asdf,"
                  s.Split(",")

          posted on 2005-12-08 14:18 天外飛仙 閱讀(637) 評論(0)  編輯  收藏 所屬分類: .net
          主站蜘蛛池模板: 澳门| 灵山县| 正定县| 浮山县| 钟祥市| 丰顺县| 米脂县| 钟山县| 星座| 洪湖市| 昌吉市| 互助| 岳西县| 老河口市| 洛川县| 柳河县| 沁阳市| 治县。| 崇左市| 宣汉县| 那坡县| 台北县| 苍梧县| 桂林市| 蕉岭县| 公安县| 文成县| 新巴尔虎右旗| 长武县| 罗定市| 湖南省| 偃师市| 乌拉特中旗| 舒城县| 布尔津县| 德保县| 绥芬河市| 琼中| 集贤县| 镇远县| 富川|