ASP. NET practical code (2) (1)

Source: Internet
Author: User

1. Table hyperjoin column Transfer Parameters

CODE: <asp: HyperLinkColumn Target = "_ blank" headertext = "id" DataTextField = "id" Navi
Required url = "aaa. aspx? Id = '<% # DataBinder. Eval (Container. DataItem, "Data field 1") %>' & name = '<% # Data
Binder. Eval (Container. DataItem, "data field 2") %> '/>

2. Click to change the color of the table.

CODE:if (e.Item.ItemType == ListItemType.Item ||e.Item.ItemType == ListItemType.Alte
rnatingItem){e.Item.Attributes.Add("onclick","this.style.backgroundColor=’#99cc00’;  this.style.color=’buttontext’;this.style.cursor=’default’;");}

Written in _ ItemDataBound of the DataGrid

CODE:if (e.Item.ItemType == ListItemType.Item ||e.Item.ItemType == ListItemType.Alterna
tingItem){e.Item.Attributes.Add("onmouseover","this.style.backgroundColor=’#99cc00’;this.style.color=’buttontext’;this.style.cursor=’default’;");e.Item.Attributes.Add("onmouseout","this.style.backgroundColor=’’;this.style.co
lor=’’;");}

3. About Date Format

Date Format setting

DataFormatString = "{0: yyyy-MM-dd }"

I think it should be in the itembound event

E. items. cell ["your column"]. text = DateTime. parse (e. items. cell ["your column"]. text. toString ("yyyy-MM-dd "))

4. Get error information and go to the specified page

Instead of using Response. Redirect, you should use Server. Transfer

E. g:

CODE: // in global. asaxprotected void Application_Error (Object sender, EventArgs e) {if (Server. GetLastError () is HttpUnhandledException) Server. Transfer ("MyErrorPage. aspx ");
// The remaining non-HttpUnhandledException exceptions are handled by ASP. NET itself and okay will be handled :)
}

The other non-HttpUnhandledException exceptions are handled by ASP. NET itself and okay.

Redirect will lead to post-back loss of error information, so the page orientation should be executed directly on the server side, so that the error information can be obtained on the error handling page and processed accordingly.

5. Clear cookies

CODE:Cookie.Expires=[DateTime];Response.Cookies("UserName").Expires = 0

6. Custom Exception Handling

CODE: // custom exception handling class: using System; using System. diagnostics; namespace MyAppException {/// <summary> /// application exception handling class inherited from the system exception class ApplicationException. /// 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) ;}}// logging class using System; using System. configuration; using System. diagnostics; using System. IO; using System. text; using System. threading; namespace MyEventLog {// <summary> // event log record class, event logging support /// <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> /// </summ Ary> public static void WriteInfo (String message) {WriteLog (TraceLevel. info, message );} /// <summary> /// record the tracking information to the Win2000/NT Event Log /// <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 information 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"> the level of error, warning, info, trace ). </param> /// <param name = "messageText"> the 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 eventLog = new EventLog ("Application", ApplicationConfiguration. eventLogMachineName, ApplicationConfiguration. eventLogSourceName); // write Event Log eventLog. writeEntry (messageText, LogEntryType);} catch {}// ignore any exceptions} // class ApplicationLog}

7. horizontal Panel scrolling and vertical auto Scaling

CODE:<asp:panel style="overflow-x:scroll;overflow-y:auto;"></asp:panel>


Related Article

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.