asp.Net常用コード(2)

6061 ワード

Target=「_blank」headertext=「ID番号」DataTextField=「id」NavigateUrl=「aaa.aspx?id=」<%#DataBinder.Eval(Container.DataItem、「データフィールド1")%>’&name='<%#DataBinder.Eval(Container.DataItem、「データフィールド2")%>」/>
7.表クリックで色を変えるif (e.Item.ItemType == ListItemType.Item ||e.Item.ItemType == ListItemType.AlternatingItem)
{
 e.Item.Attributes.Add("onclick","this.style.backgroundColor=’#99cc00’;
    this.style.color=’buttontext’;this.style.cursor=’default’;");
}

DataGridに書いてある_ItemDataBound里if (e.Item.ItemType == ListItemType.Item ||e.Item.ItemType == ListItemType.AlternatingItem)
{
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.color=’’;");
}

  
8.日付フォーマットについて
日付フォーマットDataFormatString="{0:yyyy-MM-dd}"
itembound事件でe.items.cell[" "].text=DateTime.Parse(e.items.cell[" "].text.ToString("yyyy-MM-dd"))
 
 9.エラー情報を取得し、指定したページに移動
Responseは使用しないでください.Redirect、サーバを使用します.Transfer
  e.g // in global.asax
protected void Application_Error(Object sender, EventArgs e) {
if (Server.GetLastError() is HttpUnhandledException)
Server.Transfer("MyErrorPage.aspx");

// HttpUnhandledException ASP.NET okay :)
}

Redirectではpost-backが発生してエラー情報が失われるため、ページガイドはサーバ側で直接実行する必要があります.これにより、エラー処理ページでエラー情報を取得し、対応する処理を行うことができます.
  
10.クリアクッキーCookie.Expires=[DateTime];
Response.Cookies("UserName").Expires = 0

  
11.カスタム例外処理//
using System;
using System.Diagnostics;

namespace MyAppException
{
 /// <summary>
 /// ApplicationException 。
 /// Windows NT/2000
 /// </summary>
 public class AppException:System.ApplicationException
 {
  public AppException()
  {
   if (ApplicationConfiguration.EventLogEnabled)LogEvent(" 。");
  }

 public AppException(string message)
 {
  LogEvent(message);
 }

 public AppException(string message,Exception innerException)
 {
  LogEvent(message);
  if (innerException != null)
  {
   LogEvent(innerException.Message);
  }
 }

 //
 using System;
 using System.Configuration;
 using System.Diagnostics;
 using System.IO;
 using System.Text;
 using System.Threading;

 namespace MyEventLog
 {
  /// <summary>
  /// ,
  /// <remarks>
  /// 4 (error, warning, info, trace)
  /// </remarks>
  /// </summary>
  public class ApplicationLog
  {
   /// <summary>
   /// Win2000/NT
   /// <param name="message"> </param>
   /// </summary>
   public static void WriteError(String message)
   {
    WriteLog(TraceLevel.Error, message);
   }

   /// <summary>
   /// Win2000/NT
   /// <param name="message"> </param>
   /// </summary>
   public static void WriteWarning(String message)
   {
    WriteLog(TraceLevel.Warning, message);  
   }

   /// <summary>
   /// Win2000/NT
   /// <param name="message"> </param>
   /// </summary>
   public static void WriteInfo(String message)
   {
    WriteLog(TraceLevel.Info, message);
   }
   /// <summary>
   /// Win2000/NT
   /// <param name="message"> </param>
   /// </summary>
   public static void WriteTrace(String message)
   {
    WriteLog(TraceLevel.Verbose, message);
   }

   /// <summary>
   ///
   /// <param name="ex"> </param>
   /// <param name="catchInfo"> .</param>
   /// <retvalue>
   /// <para> , .</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>
   ///
   /// <param name="level"> (error,warning,info,trace).</param>
   /// <param name="messageText"> .</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 );
     //
     eventLog.WriteEntry(messageText, LogEntryType);

    }
   catch {} //
  }
 } //class ApplicationLog