Common codes in ASP. NET (Article 2)

Source: Internet
Author: User

11. Custom Exception Handling

// Custom exception handling class
Using system;
Using system. diagnostics;

Namespace myappexception
{
/// <Summary>
///
Applications inherited from system exception applicationexceptionProgramException Handling class.
/// Automatically record the exception content to the application logs of Windows NT/2000

/// </Summary>
Public class appexception: system. applicationexception

{
Public appexception ()
{
If
(Applicationconfiguration. eventlogenabled) logevent ("an unknown error occurs. ");
}

Public appexception (string message)
{
Logevent (Message );
}

Public appexception (string message, exception innerexception)
{

Logevent (Message );
If (innerexception! = NULL)
{

Logevent (innerexception. Message );
}
}

// Log record type
Using system;
Using system. configuration;
Using
System. diagnostics;
Using system. IO;
Using system. text;
Using
System. Threading;

Namespace myeventlog
{
/// <Summary>
/// Event logging class, supporting event logging

/// <Remarks>
/// Defines four logging methods (error, warning, info, trace)
///
</Remarks>
/// </Summary>
Public class applicationlog
{
///
<Summary>
/// Record the error information to the Win2000/NT Event Log
/// <Param
Name = "message"> text information to be recorded </param>
/// </Summary>
Public static void
Writeerror (string message)
{
Writelog (tracelevel. error, message );

}

/// <Summary>
/// Record the warning information to the Win2000/NT Event Log
/// <Param
Name = "message"> text information to be recorded </param>
/// </Summary>
Public static void
Writewarning (string message)
{
Writelog (tracelevel. Warning, message );

}

/// <Summary>
/// Record the prompt information to the Win2000/NT Event Log
/// <Param
Name = "message"> text information to be recorded </param>
/// </Summary>
Public static void
Writeinfo (string message)
{
Writelog (tracelevel. info, message );
}

/// <Summary>
/// Record tracking information to Win2000/NT Event Logs
/// <Param
Name = "message"> text information to be recorded </param>
/// </Summary>
Public static void
Writetrace (string message)
{
Writelog (tracelevel. verbose, message );

}

/// <Summary>
/// Format the text information recorded in the event log
/// <Param
Name = "ex"> the exception object to be formatted </param>
/// <Param name = "catchinfo"> exception message title string. </param>

/// <Retvalue>
/// <Para> formatted exception information string, including the exception content and trace stack. </para>
///
</Retvalue>
/// </Summary>
Public static string
Formatexception (exception ex, string catchinfo)
{
Stringbuilder
Strbuilder = new stringbuilder ();
If (catchinfo! = String. Empty)
{

Strbuilder. append (catchinfo). append ("\ r \ n ");
}

Strbuilder. append (ex. Message). append ("\ r \ n"). append (ex. stacktrace );

Return strbuilder. tostring ();
}

/// <Summary>
/// Actual event log Writing Method
/// <Param
Name = "level"> level (error, warning, info, trace) of the information to be recorded. </param>
/// <Param
Name = "messagetext"> text to be recorded. </param>
/// </Summary>
Private Static void
Writelog (tracelevel level, string messagetext)
{
Try
{

Eventlogentrytype logentrytype;
Switch (level)
{
Case
Tracelevel. Error:
Logentrytype = eventlogentrytype. error;
Break;

Case tracelevel. Warning:
Logentrytype = eventlogentrytype. Warning;

Break;
Case tracelevel. Info:
Logentrytype =
Eventlogentrytype. Information;
Break;
Case tracelevel. verbose:

Logentrytype = eventlogentrytype. successaudit;
Break;
Default:

Logentrytype = eventlogentrytype. successaudit;
Break;
}

EventLog = new EventLog ("application ",
Applicationconfiguration. eventlogmachinename,
Applicationconfiguration. eventlogsourcename );
// Write Event Logs

EventLog. writeentry (messagetext, logentrytype );

}
Catch {} // ignore any exceptions
}
} // Class applicationlog

}

12. horizontal panel scrolling and vertical auto Scaling

<Asp: Panel
Style = "overflow-X: Scroll; overflow-Y: auto;"> </ASP: Panel>

13. Press enter to convert to Tab

<Script language = "JavaScript" for = "document" event = "onkeydown">

If (event. keycode = 13 & event. srcelement. type! = 'Button '&&
Event. srcelement. type! = 'Submit '& event. srcelement. type! = 'Reset'
& Event. srcelement. type! = ''&&
Event. srcelement. type! = 'Textea ');
Event. keycode = 9;
</SCRIPT>

Onkeydown = "If (event. keycode = 13) event. keycode = 9"

14. DataGrid super join Column

Datanavigateurlfield = "field name" datanavigateurlformatstring = "http: // XX/INC/Delete. aspx? Id = {0 }"

15. The row of the DataGrid changes color with the mouse

Private void dgzf_itemdatabound (Object sender,
System. Web. UI. webcontrols. datagriditemeventargs E)
{
If
(E. Item. itemtype! = Listitemtype. header)
{
E. Item. Attributes. Add (
"Onmouseout", "This. style. backgroundcolor = \" "+ E. Item. Style [" background-color "] + "\"");

E. Item. Attributes. Add ("onmouseover", "This. style. backgroundcolor = \" "+
"# Eff3f7" + "\"");
}
}

16. template Column

<Asp: templatecolumn visible = "false" sortexpression = "Demo" headertext = "ID">

<Itemtemplate>
<Asp: Label text = '<% # databinder. eval (container. dataitem,
"ArticleID") %> 'runat = "server" width = "80%" id = "lblcolumn"/>
</Itemtemplate>

</ASP: templatecolumn>

<Asp: templatecolumn headertext = "selected">
<Headerstyle wrap = "false"
Horizontalalign = "center"> <Itemtemplate>
<Asp: checkbox
Id = "chkexport" runat = "server"/>
</Itemtemplate>
<Edititemtemplate>

<Asp: checkbox id = "chkexporton" runat = "server" enabled = "true"/>

</Edititemtemplate>
</ASP: templatecolumn>

BackgroundCode

Protected void checkall_checkedchanged (Object sender, system. eventargs E)

{
// Change the column selection to select all or all columns.
Checkbox chkexport;
If (checkall. Checked)

{
Foreach (maid in mydatagrid. Items)
{

Chkexport = (checkbox) odatagriditem. findcontrol ("chkexport ");

Chkexport. Checked = true;
}
}
Else
{

Foreach (maid in mydatagrid. Items)
{
Chkexport
= (Checkbox) odatagriditem. findcontrol ("chkexport ");
Chkexport. Checked =
False;
}
}
}

17. digit formatting

The result of <% # container. dataitem ("price") %> is 500.0000. How to format it to 500.00 ?]

<% # Container. dataitem ("price", "{0: ¥ #,## 0.00}") %>

Int I = 123456;
String S = I. tostring ("###,##. 00 ");

18. Date formatting

[ASPX page: <% # databinder. eval (container. dataitem, "company_ureg_date") %>

Shown as: 19:44:28

I only want]

<% #
Databinder. eval (container. dataitem, "company_ureg_date", "{0: yyyy-m-d}") %>

How should I change it?

[Format date]

It is usually the object (datetime) objectfromdb). tostring ("yyyy-mm-dd ");

[Date verification expression]

A. The correct input format is as follows: [], [10:29:39], []

^ (\ D {2} ([02468] [048]) | ([13579] [26]) [\-\/\ s]? (0? [13578]) | (1 [02]) [\-\/\ s]? (0? [1-9]) | ([1-2] [0-9]) | (3 [01]) | (0? [469]) | (11) [\-\/\ s]? (0? [1-9]) | ([1-2] [0-9]) | (30) | (0? 2 [\-\/\ s]? (0? [1-9]) | ([1-2] [0-9]) | (\ D {2} ([02468] [1235679]) | ([13579] [01345789]) [\-\/\ s]? (0? [13578]) | (1 [02]) [\-\/\ s]? (0? [1-9]) | ([1-2] [0-9]) | (3 [01]) | (0? [469]) | (11) [\-\/\ s]? (0? [1-9]) | ([1-2] [0-9]) | (30) | (0? 2 [\-\/\ s]? (0? [1-9]) | (1 [0-9]) | (2 [0-8]) (\ s (0? [1-9]) | (1 [0-2]) \ :( [0-5] [0-9]) (\ s) | (\ :( [0-5] [0-9]) \ s) ([am | Pm | am | PM] {2, 2 })))? $

B. The following correct input format: [0001-12-31], [9999 09 30], [2002/03/03]

^ \ D {4} [\-\/\ s]? (0 [13578]) | (1 [02]) [\-\/\ s]? ([0-2] [0-9]) | (3 [01]) | (0 [469]) | (11 )) [\-\/\ s]? ([0-2] [0-9]) | (30) | (02 [\-\/\ s]? [0-2] [0-9]) $

[Case-sensitive conversion]

Httputility. htmlencode (string );

Httputility. htmldecode (string)

19. How to set global variables

In global. asax

Application_start () event

Add application [attribute name] = xxx;

Is your global variable

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.