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

本文涉及的产品
日志服务 SLS,月写入数据量 50GB 1个月
简介:

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

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

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

InBlock.gif
InBlock.gif 
public AppException(string message,Exception innerException)
ExpandedSubBlockStart.gif 
{
InBlock.gif  LogEvent(message);
InBlock.gif  
if (innerException != null)
ExpandedSubBlockStart.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.gif 
{
ExpandedSubBlockStart.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.gif  
{
ExpandedSubBlockStart.gif   
/// <summary>
InBlock.gif   
/// 将错误信息记录到Win2000/NT事件日志中
InBlock.gif   
/// <param name="message">需要记录的文本信息</param>
ExpandedSubBlockEnd.gif   
/// </summary>

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

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

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

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

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

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

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

InBlock.gif
ExpandedSubBlockStart.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.gif   
{
InBlock.gif    StringBuilder strBuilder 
= new StringBuilder();
InBlock.gif    
if (catchInfo != String.Empty)
ExpandedSubBlockStart.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.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.gif   
{
InBlock.gif    
try
ExpandedSubBlockStart.gif    

InBlock.gif     EventLogEntryType LogEntryType;
InBlock.gif     
switch (level)
ExpandedSubBlockStart.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.gif   
catch {} //忽略任何异常
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.gif
{
InBlock.gif 
if (e.Item.ItemType!=ListItemType.Header)
ExpandedSubBlockStart.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.gif
{
InBlock.gif 
//改变列的选定,实现全选或全不选。
InBlock.gif
 CheckBox chkExport ;
InBlock.gif 
if( CheckAll.Checked)
ExpandedSubBlockStart.gif 
{
InBlock.gif  
foreach(DataGridItem oDataGridItem in MyDataGrid.Items)
ExpandedSubBlockStart.gif  
{
InBlock.gif   chkExport 
= (CheckBox)oDataGridItem.FindControl("chkExport");
InBlock.gif   chkExport.Checked 
= true;
ExpandedSubBlockEnd.gif  }

ExpandedSubBlockEnd.gif }

InBlock.gif 
else
ExpandedSubBlockStart.gif 
{
InBlock.gif  
foreach(DataGridItem oDataGridItem in MyDataGrid.Items)
ExpandedSubBlockStart.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;



本文转自高海东博客园博客,原文链接:http://www.cnblogs.com/ghd258/archive/2005/11/02/267501.html,如需转载请自行联系原作者
相关实践学习
日志服务之使用Nginx模式采集日志
本文介绍如何通过日志服务控制台创建Nginx模式的Logtail配置快速采集Nginx日志并进行多维度分析。
相关文章
|
1月前
|
SQL 开发框架 .NET
分享130个ASP整站程序源码,总有一款适合您
分享130个ASP整站程序源码,总有一款适合您
85 1
|
1月前
|
SQL 开发框架 .NET
分享134个ASP整站程序源码,总有一款适合您
分享134个ASP整站程序源码,总有一款适合您
29 1
|
1月前
|
开发框架 .NET 网络安全
分享128个ASP整站程序源码,总有一款适合您
分享128个ASP整站程序源码,总有一款适合您
32 0
|
1月前
|
XML 开发框架 .NET
LabVIEW中加载.NET 2.0,3.0和3.5程序集
LabVIEW中加载.NET 2.0,3.0和3.5程序集
34 4
|
26天前
|
XML 开发框架 .NET
【.NET Core】常见C#代码约定
【.NET Core】常见C#代码约定
19 5
|
3天前
|
开发框架 JavaScript 前端开发
详细解读ASP常用三十三种代码
详细解读ASP常用三十三种代码
|
3天前
|
开发框架 JavaScript 前端开发
详细解读ASP常用三十三种代码
详细解读ASP常用三十三种代码
|
3天前
|
存储 安全 C#
技术心得记录:强命名的延迟与关联在.net程序集保护中的作用及其逆向方法
技术心得记录:强命名的延迟与关联在.net程序集保护中的作用及其逆向方法
|
3天前
|
开发框架 前端开发 JavaScript
程序与技术分享:ASP.NET发展史(【译】)
程序与技术分享:ASP.NET发展史(【译】)
10 0
|
3天前
|
网络协议
技术好文:Smark.Net实现简单聊天程序
技术好文:Smark.Net实现简单聊天程序