1. Add site count:
Add to 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 ();
}
Add the following statement on the page to be displayed:
// Traffic statistics counter
Labelvisitcount. Text = application ["globalcounter"]. tostring ();
-----------------------------------------------
2. A session instance:
(Import using system. Web. sessionstate; and using system. Web. Security; namespace and use forms for verification)
Add in 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"
/>
Start the ASP. NET State service, and determine whether the login account of the Service interacts with the server desktop as required (the operation permission is incorrect)
Add to globalv. asax. CS
Protected void session_start (Object sender, eventargs E)
{
// Configure the logon session:
// Session ["loginvalidate"] = false;
Session. Add ("loginnamevalidate", null );
}
// Application-level clearing of sessions
Protected void application_end (Object sender, eventargs E)
{
Try
{
If (bool) session ["loginnamevalidate"]! = False)
{
Session ["loginnamevalidate"] = false;
Session. Remove ("loginnamevalidate ");
}
}
Catch
{
}
Finally
{
Session. removeall ();
}
}
// The session Exits normally at the page level:
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 ();
}
}
Supplement: // The abnormal exit of page-level sessions is as follows. Besides, errors can be recorded in system application error logs as needed.
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
{
// Depending on the situation, determine whether to exit the program and add session. removeall ();
}
}
}
-----------------------------------------------
3. An error is recorded in the system application error log instance:
Add to globalv. asax. CS
Protected void application_error (Object sender, eventargs E)
{
// Record the error to the system log. Import the system. Diagnostics namespace.
// [HKEY_LOCAL_MACHINE \ SYSTEM \ CurrentControlSet \ Services \ Eventlog \ Application creates an error log branch for this program.
Try
{
String errormessage = "My system encountered an error. The detailed error is" + server. getlasterror ();
Server. clearerror ();
String LOGNAME = "myapplicationlog ";
String sourcename = "My error logs ";
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
{
// Go to the error page as expected response. Redirect ("error. aspx ");
}
}
If you do not change the registry, the ASP. NET account may have permission to access the registry. You can manually change the registry or import the. reg file in advance.
-----------------------------------------------
4. Solution for changing the color of the row in the DataGrid based on a certain condition
// To process the global date format, import system. Globalization;
// Define a able. The name mytable can be changed and used for future binding to the DataGrid.
// Fill the dataset normally when you need to use it in the future, and then import the table of the dataset to mytable in the following operations, make changes (change the color, condition, field, and so on), and bind it to the target DataGrid.
Protected system. Data. datatable mytable;
Try
{
// First fill the dataset and then operate the data table and then bind it to the DataGrid. The following operations are separated from SQL to facilitate reuse.
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 ();
// In this example, check that the fdate1 field is 1, 2, and 3 weeks different from the current date.
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 );
}
// Bind mytable to the datagrid1 table
Datagrid1.datasource = mytable;
Datagrid1.databind ();
}
Catch
{
}
Finally
{
}