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


1. 打开新的窗口并传送参数:
None.gif // 传送参数:
None.gif response.write( " <script>window.open(’*.aspx?id= " + this .DropDownList1.SelectIndex + " &id1= " + dot.gif + " ’)</script> " )
None.gif   // 接收参数:
None.gif string a = Request.QueryString( " id " );
None.gif string b = Request.QueryString( " id1 " );
2.为按钮添加对话框
None.gif Button1.Attributes.Add( " onclick " , " return confirm(’确认?’) " );
None.gifbutton.attributes.add( " onclick " , " if(confirm(’are you suredot.gif?’)){return true;}else{return false;} " )
3.删除表格选定记录
None.gif int intEmpID = ( int )MyDataGrid.DataKeys[e.Item.ItemIndex];
None.gif string deleteCmd = " DELETE from Employee where emp_id = " + intEmpID.ToString()
4.删除表格记录警告
None.gif private void DataGrid_ItemCreated(Object sender,DataGridItemEventArgs e)
ExpandedBlockStart.gifContractedBlock.gif dot.gif {
InBlock.gif switch(e.Item.ItemType)
ExpandedSubBlockStart.gifContractedSubBlock.gif dot.gif{
InBlock.gif  case ListItemType.Item :
InBlock.gif  case ListItemType.AlternatingItem :
InBlock.gif  case ListItemType.EditItem:
InBlock.gif   TableCell myTableCell;
InBlock.gif   myTableCell = e.Item.Cells[14];
InBlock.gif   LinkButton myDeleteButton ;
InBlock.gif   myDeleteButton = (LinkButton)myTableCell.Controls[0];
InBlock.gif   myDeleteButton.Attributes.Add("onclick","return confirm(’您是否确定要删除这条信息’);");
InBlock.gif   break;
InBlock.gif  default:
InBlock.gif   break;
ExpandedSubBlockEnd.gif }

ExpandedBlockEnd.gif}

None.gif
5.点击表格行链接另一页
None.gif private void grdCustomer_ItemDataBound( object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
ExpandedBlockStart.gifContractedBlock.gif dot.gif {
InBlock.gif //点击表格打开
InBlock.gif if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
InBlock.gif  e.Item.Attributes.Add("onclick","window.open(’Default.aspx?id=" + e.Item.Cells[0].Text + "’);");
ExpandedBlockEnd.gif}

None.gif   // 双击表格连接到另一页
None.gif   // 在itemDataBind事件中
None.gif if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
ExpandedBlockStart.gifContractedBlock.gif dot.gif {
InBlock.gif string OrderItemID =e.item.cells[1].Text;
InBlock.gif dot.gif
InBlock.gif e.item.Attributes.Add("ondblclick", "location.href=’../ShippedGrid.aspx?id=" + OrderItemID + "’");
ExpandedBlockEnd.gif}

None.gif // 双击表格打开新一页
None.gif if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
ExpandedBlockStart.gifContractedBlock.gif dot.gif {
InBlock.gif string OrderItemID =e.item.cells[1].Text;
InBlock.gif dot.gif
InBlock.gif e.item.Attributes.Add("ondblclick", "open(’../ShippedGrid.aspx?id=" + OrderItemID + "’)");
ExpandedBlockEnd.gif}

None.gif  ★特别注意:【 ? id = 】 处不能为 【 ? id = 】
6.表格超连接列传递参数
None.gif <asp:HyperLinkColumn Target="_blank" headertext="ID号" DataTextField="id" NavigateUrl="aaa.aspx?id=’
None.gif <%# DataBinder.Eval(Container.DataItem, "数据字段1")%>’ & name=’<%# DataBinder.Eval(Container.DataItem, "数据字段2")%>’ />
7.表格点击改变颜色
None.gif if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
ExpandedBlockStart.gifContractedBlock.gif dot.gif {
InBlock.gif e.Item.Attributes.Add("onclick","this.style.backgroundColor=’#99cc00’;
InBlock.gif    this.style.color=’buttontext’;this.style.cursor=’default’;");
ExpandedBlockEnd.gif}

None.gif  写在DataGrid的_ItemDataBound里
None.gif if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
ExpandedBlockStart.gifContractedBlock.gif dot.gif {
InBlock.gife.Item.Attributes.Add("onmouseover","this.style.backgroundColor=’#99cc00’;
InBlock.gif   this.style.color=’buttontext’;this.style.cursor=’default’;");
InBlock.gife.Item.Attributes.Add("onmouseout","this.style.backgroundColor=’’;this.style.color=’’;");
ExpandedBlockEnd.gif}

None.gif

8.关于日期格式

None.gif 日期格式设定
None.gifDataFormatString = " {0:yyyy-MM-dd} "
None.gif   // 我觉得应该在itembound事件中
None.gif e.items.cell[ " 你的列 " ].text = DateTime.Parse(e.items.cell[ " 你的列 " ].text.ToString( " yyyy-MM-dd " ))
9.获取错误信息并到指定页面
None.gif // 不要使用Response.Redirect,而应该使用Server.Transfer
None.gif   e.g
None.gif // in global.asax
ExpandedBlockStart.gifContractedBlock.gif protected void Application_Error(Object sender, EventArgs e) dot.gif {
InBlock.gifif (Server.GetLastError() is HttpUnhandledException)
InBlock.gifServer.Transfer("MyErrorPage.aspx");
InBlock.gif
InBlock.gif//其余的非HttpUnhandledException异常交给ASP.NET自己处理就okay了 :)
ExpandedBlockEnd.gif}

None.gif   // Redirect会导致post-back的产生从而丢失了错误信息,所以页面导向应该直接在服务器端执行,这样就可以在错误处理页面得到出错信息并进行相应的处理
10.清空Cookie
None.gif Cookie.Expires = [DateTime];
None.gifResponse.Cookies( " UserName " ).Expires = 0
None.gif

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

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/54654/viewspace-425461/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/54654/viewspace-425461/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。
经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值