ASP.NET程序中常用代码汇总(二)

11.自定义异常处理
None.gif // 自定义异常处理类 
None.gif
using  System;
None.gif
using  System.Diagnostics;
None.gif
None.gif
namespace  MyAppException
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif 
/**//// <summary>
InBlock.gif 
/// 从系统异常类ApplicationException继承的应用程序异常处理类。
InBlock.gif 
/// 自动将异常内容记录到Windows NT/2000的应用程序日志
ExpandedSubBlockEnd.gif 
/// </summary>

InBlock.gif public class AppException:System.ApplicationException
ExpandedSubBlockStart.gifContractedSubBlock.gif 
dot.gif{
InBlock.gif  
public AppException()
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif   
if (ApplicationConfiguration.EventLogEnabled)LogEvent("出现一个未知错误。");
ExpandedSubBlockEnd.gif  }

InBlock.gif
InBlock.gif 
public AppException(string message)
ExpandedSubBlockStart.gifContractedSubBlock.gif 
dot.gif{
InBlock.gif  LogEvent(message);
ExpandedSubBlockEnd.gif }

InBlock.gif
InBlock.gif 
public AppException(string message,Exception innerException)
ExpandedSubBlockStart.gifContractedSubBlock.gif 
dot.gif{
InBlock.gif  LogEvent(message);
InBlock.gif  
if (innerException != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif   LogEvent(innerException.Message);
ExpandedSubBlockEnd.gif  }

ExpandedSubBlockEnd.gif }

InBlock.gif
InBlock.gif 
//日志记录类
InBlock.gif
 using System;
InBlock.gif 
using System.Configuration;
InBlock.gif 
using System.Diagnostics;
InBlock.gif 
using System.IO;
InBlock.gif 
using System.Text;
InBlock.gif 
using System.Threading;
InBlock.gif
InBlock.gif 
namespace MyEventLog
ExpandedSubBlockStart.gifContractedSubBlock.gif 
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// <summary>
InBlock.gif  
/// 事件日志记录类,提供事件日志记录支持 
InBlock.gif  
/// <remarks>
InBlock.gif  
/// 定义了4个日志记录方法 (error, warning, info, trace) 
InBlock.gif  
/// </remarks>
ExpandedSubBlockEnd.gif  
/// </summary>

InBlock.gif  public class ApplicationLog
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif   
/**//// <summary>
InBlock.gif   
/// 将错误信息记录到Win2000/NT事件日志中
InBlock.gif   
/// <param name="message">需要记录的文本信息</param>
ExpandedSubBlockEnd.gif   
/// </summary>

InBlock.gif   public static void WriteError(String message)
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    WriteLog(TraceLevel.Error, message);
ExpandedSubBlockEnd.gif   }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif   
/**//// <summary>
InBlock.gif   
/// 将警告信息记录到Win2000/NT事件日志中
InBlock.gif   
/// <param name="message">需要记录的文本信息</param>
ExpandedSubBlockEnd.gif   
/// </summary>

InBlock.gif   public static void WriteWarning(String message)
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    WriteLog(TraceLevel.Warning, message);  
ExpandedSubBlockEnd.gif   }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif   
/**//// <summary>
InBlock.gif   
/// 将提示信息记录到Win2000/NT事件日志中
InBlock.gif   
/// <param name="message">需要记录的文本信息</param>
ExpandedSubBlockEnd.gif   
/// </summary>

InBlock.gif   public static void WriteInfo(String message)
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    WriteLog(TraceLevel.Info, message);
ExpandedSubBlockEnd.gif   }

ExpandedSubBlockStart.gifContractedSubBlock.gif   
/**//// <summary>
InBlock.gif   
/// 将跟踪信息记录到Win2000/NT事件日志中
InBlock.gif   
/// <param name="message">需要记录的文本信息</param>
ExpandedSubBlockEnd.gif   
/// </summary>

InBlock.gif   public static void WriteTrace(String message)
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    WriteLog(TraceLevel.Verbose, message);
ExpandedSubBlockEnd.gif   }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif   
/**//// <summary>
InBlock.gif   
/// 格式化记录到事件日志的文本信息格式
InBlock.gif   
/// <param name="ex">需要格式化的异常对象</param>
InBlock.gif   
/// <param name="catchInfo">异常信息标题字符串.</param>
InBlock.gif   
/// <retvalue>
InBlock.gif   
/// <para>格式后的异常信息字符串,包括异常内容和跟踪堆栈.</para>
InBlock.gif   
/// </retvalue>
ExpandedSubBlockEnd.gif   
/// </summary>

InBlock.gif   public static String FormatException(Exception ex, String catchInfo)
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    StringBuilder strBuilder 
= new StringBuilder();
InBlock.gif    
if (catchInfo != String.Empty)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif     strBuilder.Append(catchInfo).Append(
"\r\n");
ExpandedSubBlockEnd.gif    }

InBlock.gif    strBuilder.Append(ex.Message).Append(
"\r\n").Append(ex.StackTrace);
InBlock.gif    
return strBuilder.ToString();
ExpandedSubBlockEnd.gif   }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif   
/**//// <summary>
InBlock.gif   
/// 实际事件日志写入方法
InBlock.gif   
/// <param name="level">要记录信息的级别(error,warning,info,trace).</param>
InBlock.gif   
/// <param name="messageText">要记录的文本.</param>
ExpandedSubBlockEnd.gif   
/// </summary>

InBlock.gif   private static void WriteLog(TraceLevel level, String messageText)
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    
try
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif
InBlock.gif     EventLogEntryType LogEntryType;
InBlock.gif     
switch (level)
ExpandedSubBlockStart.gifContractedSubBlock.gif     
dot.gif{
InBlock.gif      
case TraceLevel.Error:
InBlock.gif       LogEntryType 
= EventLogEntryType.Error;
InBlock.gif       
break;
InBlock.gif      
case TraceLevel.Warning:
InBlock.gif       LogEntryType 
= EventLogEntryType.Warning;
InBlock.gif       
break;
InBlock.gif      
case TraceLevel.Info:
InBlock.gif       LogEntryType 
= EventLogEntryType.Information;
InBlock.gif       
break;
InBlock.gif      
case TraceLevel.Verbose:
InBlock.gif       LogEntryType 
= EventLogEntryType.SuccessAudit;
InBlock.gif       
break;
InBlock.gif      
default:
InBlock.gif       LogEntryType 
= EventLogEntryType.SuccessAudit;
InBlock.gif       
break;
ExpandedSubBlockEnd.gif     }

InBlock.gif
InBlock.gif     EventLog eventLog 
= new EventLog("Application", ApplicationConfiguration.EventLogMachineName, ApplicationConfiguration.EventLogSourceName );
InBlock.gif     
//写入事件日志
InBlock.gif
     eventLog.WriteEntry(messageText, LogEntryType);
InBlock.gif
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockStart.gifContractedSubBlock.gif   
catch dot.gif{} //忽略任何异常
ExpandedSubBlockEnd.gif
  }
 
ExpandedSubBlockEnd.gif }
 //class ApplicationLog
ExpandedSubBlockEnd.gif
}

InBlock.gif
InBlock.gif
12.Panel 横向滚动,纵向自动扩展
None.gif <asp:panel style="overflow-x:scroll;overflow-y:auto;"></asp:panel>
13.回车转换成Tab
None.gif <script language = " javascript "   for = " document "  event = " onkeydown ">
None.gif  if (event.keyCode == 13   &&  event.srcElement.type != ’button’  &&  event.srcElement.type != ’submit’  &&      event.srcElement.type != ’reset’  &&  event.srcElement.type != ’’ &&  event.srcElement.type !=’textarea’); 
None.gif   event.keyCode = 9;
None.gif< /script>
None.gif
None.gifonkeydown = " if(event.keyCode==13) event.keyCode=9 "
None.gif
None.gif
14.DataGrid超级连接列
None.gif DataNavigateUrlField="字段名" DataNavigateUrlFormatString="http://xx/inc/delete.aspx?ID={0}"
15.DataGrid行随鼠标变色
None.gif private   void  DGzf_ItemDataBound( object  sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif 
if (e.Item.ItemType!=ListItemType.Header)
ExpandedSubBlockStart.gifContractedSubBlock.gif 
dot.gif{
InBlock.gif  e.Item.Attributes.Add( 
"onmouseout","this.style.backgroundColor=\""+e.Item.Style["BACKGROUND-COLOR"]+"\"");
InBlock.gif  e.Item.Attributes.Add( 
"onmouseover","this.style.backgroundColor=\"""#EFF3F7"+"\"");
ExpandedSubBlockEnd.gif }

ExpandedBlockEnd.gif}

None.gif
None.gif
16.模板列
None.gif <ASP:TEMPLATECOLUMN visible="False" sortexpression="demo" headertext="ID">
None.gif<ITEMTEMPLATE>
None.gif<ASP:LABEL text=’<%# DataBinder.Eval(Container.DataItem, "ArticleID")%>’ runat="server" width="80%" id="lblColumn" />
None.gif</ITEMTEMPLATE>
None.gif</ASP:TEMPLATECOLUMN>
None.gif
None.gif<ASP:TEMPLATECOLUMN headertext="选中">
None.gif<HEADERSTYLE wrap="False" horizontalalign="Center"></HEADERSTYLE>
None.gif<ITEMTEMPLATE>
None.gif<ASP:CHECKBOX id="chkExport" runat="server" />
None.gif</ITEMTEMPLATE>
None.gif<EDITITEMTEMPLATE>
None.gif<ASP:CHECKBOX id="chkExportON" runat="server" enabled="true" />
None.gif</EDITITEMTEMPLATE>
None.gif</ASP:TEMPLATECOLUMN>
None.gif
None.gif 后台代码
None.gif
None.gif
None.gif
protected   void  CheckAll_CheckedChanged( object  sender, System.EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif 
//改变列的选定,实现全选或全不选。
InBlock.gif
 CheckBox chkExport ;
InBlock.gif 
if( CheckAll.Checked)
ExpandedSubBlockStart.gifContractedSubBlock.gif 
dot.gif{
InBlock.gif  
foreach(DataGridItem oDataGridItem in MyDataGrid.Items)
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif   chkExport 
= (CheckBox)oDataGridItem.FindControl("chkExport");
InBlock.gif   chkExport.Checked 
= true;
ExpandedSubBlockEnd.gif  }

ExpandedSubBlockEnd.gif }

InBlock.gif 
else
ExpandedSubBlockStart.gifContractedSubBlock.gif 
dot.gif{
InBlock.gif  
foreach(DataGridItem oDataGridItem in MyDataGrid.Items)
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif   chkExport 
= (CheckBox)oDataGridItem.FindControl("chkExport");
InBlock.gif   chkExport.Checked 
= false;
ExpandedSubBlockEnd.gif  }

ExpandedSubBlockEnd.gif }

ExpandedBlockEnd.gif}

None.gif
17.数字格式化
None.gif 【<%#Container.DataItem("price")%>的结果是500.0000,怎样格式化为500.00?】
None.gif
None.gif
None.gif<%#Container.DataItem("price","{0:¥#,##0.00}")%>
None.gif
None.gifint i=123456;
None.gifstring s=i.ToString("###,###.00");
None.gif
18.日期格式化
【aspx页面内:<%# DataBinder.Eval(Container.DataItem,"Company_Ureg_Date")%>

  显示为: 2004-8-11 19:44:28

  我只想要:2004-8-11 】

<%# DataBinder.Eval(Container.DataItem,"Company_Ureg_Date","{0:yyyy-M-d}")%>
  应该如何改?

  【格式化日期】

  取出来,一般是object((DateTime)objectFromDB).ToString("yyyy-MM-dd");

  【日期的验证表达式】

  A.以下正确的输入格式: [2004-2-29], [2004-02-29 10:29:39 pm], [2004/12/31]

^((\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.以下正确的输入格式:[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]))$
19【大小写转换】
HttpUtility.HtmlEncode(string);
HttpUtility.HtmlDecode(string)

20.如何设定全局变量
  Global.asax中
  Application_Start()事件中
  添加Application[属性名] = xxx;


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值