整理了一些常用的.NET代碼,貼出來供大家參考,并希望我們一起搜集更多的内容。
一、為GridView添加索引列 < asp:TemplateField HeaderText ="ID" >
< ItemTemplate >
<% # Container.DataItemIndex + 1 %>
</ ItemTemplate >
</ asp:TemplateField >
二、批量删除列
模闆列代碼: < asp:TemplateField HeaderText ="選擇" >
< ItemTemplate >
< asp:CheckBox ID ="chkSelect" runat ="server" />
</ ItemTemplate >
</ asp:TemplateField > 全選: for ( int i = 0 ; i < gv.Rows.Count; i ++ )
{
CheckBox chkSelect = (CheckBox)gv.Rows[i].FindControl( " chkSelect " );
chkSelect.Checked = true ;
} 反選: for ( int i = 0 ; i < gv.Rows.Count; i ++ )
{
CheckBox chkSelect = (CheckBox)gv.Rows[i].FindControl( " chkSelect " );
chkSelect.Checked = ! chkSelect.Checked;
} 取消選擇: for ( int i = 0 ; i < gv.Rows.Count; i ++ )
{
CheckBox chkSelect = (CheckBox)gv.Rows[i].FindControl( " chkSelect " );
chkSelect.Checked = false ;
} 删除: for ( int i = 0 ; i < gv.Rows.Count; i ++ )
{
CheckBox chkSelect = (CheckBox)gv.Rows[i].FindControl( " chkSelect " );
if (chkSelect.Checked)
{
// 删除記錄
}
}
三、删除表格標明記錄
int id = ( int )MyDataGrid.DataKeys[e.Item.ItemIndex]; // 在調用前必須指定DataKeyNames屬性為要擷取
的主鍵。
string deleteCmd = " DELETE from Employee where emp_id = " + intEmpID.ToString();
四、為按鈕添加确認對話框
button.Attributes.Add( " onclick " , " return confirm(’确認?’) " );
五、點選表格行連結另一頁
private void grdCustomer_ItemDataBound( object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
// 點選表格打開
if (e.Item.ItemType == ListItemType.Item e.Item.ItemType == ListItemType.AlternatingItem)
e.Item.Attributes.Add( " onclick " , " window.open(’Default.aspx?id= " + e.Item.Cells[ 0 ].Text + "
’); " );
}
六、超連接配接列傳遞參數
< asp:HyperLinkColumn Target ="_blank" headertext ="ID号" DataTextField ="id" NavigateUrl ="aaa.aspx?id='<%# DataBinder.Eval(Container.DataItem, " 資料字段1")% > ' & name=' <% # DataBinder.Eval(Container.DataItem, " 資料字段2 " ) %> ' />
七、綁定日期格式
e.items.cell[ " time " ].text = DateTime.Parse(e.items.cell[ " time " ].text.ToString( " yyyy-MM-dd " ))
八、統一捕獲異常 Global.asax中:
protected void Application_Error(Object sender, EventArgs e) {
if (Server.GetLastError() is HttpUnhandledException)
Server.Transfer( " MyErrorPage.aspx " );
// 編寫添加到日志的代碼Log.add();
}
九、自定義異常處理 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
}