.NET惯用效能和代码(一)

来源:互联网 发布:du指令文件夹linux命令 编辑:程序博客网 时间:2024/05/17 20:32

1. 打开新的窗口并传递参数:

  传接参数:

  response.write("")

  接收参数:

  string a = Request.QueryString("id");

  string b = Request.QueryString("id一");

  二.为旋钮增添对话框

  Button一.Attributes.Add("onclick","return confirm('确认?')");

  button.attributes.add("onclick","if(confirm('are you sure...?')){return true;}else{return false;}")

  三.剔除报表选出记要

  int intEmpID = (int)MyDataGrid.DataKeys[e.Item.ItemIndex];

  string deleteCmd = "DELETE from Employee where emp_id = " + intEmpID.ToString()

  四.剔除报表记要警告

  private void DataGrid_ItemCreated(Object sender,DataGridItemEventArgs e)

  {

  switch(e.Item.ItemType)

  {

  case ListItemType.Item :

  case ListItemType.AlternatingItem :

  case ListItemType.EditItem:

  TableCell myTableCell;

  myTableCell = e.Item.Cells[14];

  LinkButton myDeleteButton ;

  myDeleteButton = (LinkButton)myTableCell.Controls[零];

  myDeleteButton.Attributes.Add("onclick","return confirm('您是不是确定要剔除这条信息');");

  break;

  default:

  break;

  }

  }

  五.点击报表行链接另一页

  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[零].Text + "');");

  }

  双击报表联接到另一页

  在itemDataBind事件中

  if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)

  {

  string OrderItemID =e.item.cells[一].Text;

  ...

  e.item.Attributes.Add("ondblclick", "location.href='../ShippedGrid.aspx?id=" + OrderItemID + "'");

  }

  双击报表打开新一页

  if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)

  {

  string OrderItemID =e.item.cells[一].Text;

  ...

  e.item.Attributes.Add("ondblclick", "open('../ShippedGrid.aspx?id=" + OrderItemID + "')");

  }

本文来源:我的异常网
六.报表超联接本纪递参数

  ' & name='' />

  七.报表点击改变色彩

  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='';");

  }

  八.至于日期格式

  日期格式设定

  DataFormatString="{零:yyyy-MM-dd}"

  我觉着应该在itembound事件中

  e.items.cell["你的列"].text=DateTime.Parse(e.items.cell["你的列"].text.ToString("yyyy-MM-dd"))

  九.获取错误信息并到指定页面

  不用应用Response.Redirect,而应当施用Server.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的发生故此丢掉了错误信息,之所以页面引向应当直接在服务器端实施,这么就可以在错误处理页面失去疏失信息并开展呼应的处置

  十.清空Cookie

  Cookie.Expires=[DateTime];

  Response.Cookies("UserName").Expires = 零

  11.自定义异常处置

  //自定义异常处置种

  using System;

  using System.Diagnostics;

  namespace MyAppException

  {

  ///

  /// 从系统异常种ApplicationException承继的应用程序异常处置种。

  /// 自动将异常内容记要到Windows NT/2000的应用程序日记

  ///

  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

  {

  ///

  /// 事件日记记要种,提供事件日记记要支持

  ///

  /// 定义了4个日记记要步骤 (error, warning, info, trace)

  ///

  ///

  public class ApplicationLog

  {

  ///

  /// 将错误信息记要到Win2000/NT事件日记中

  ///急需记要的文本信息

  ///

  public static void WriteError(String message)

  {

  WriteLog(TraceLevel.Error, message);

  }

六.报表超联接本纪递参数

  ' & name='' />

  七.报表点击改变色彩

  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='';");

  }

  八.对于日期格式

  日期格式设定

  DataFormatString="{零:yyyy-MM-dd}"

  我觉着应该在itembound事件中

  e.items.cell["你的列"].text=DateTime.Parse(e.items.cell["你的列"].text.ToString("yyyy-MM-dd"))

  九.获取错误信息并到指定页面

  不用运用Response.Redirect,而应当施用Server.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的发生故此丢掉了错误信息,之所以页面导引应当直接在服务器端施行,这么就可以在错误处理页面失去疏失信息并进展呼应的处置

  十.清空Cookie

  Cookie.Expires=[DateTime];

  Response.Cookies("UserName").Expires = 零

  11.自定义异常处置

  //自定义异常处置种

  using System;

  using System.Diagnostics;

  namespace MyAppException

  {

  ///

  /// 从系统异常种ApplicationException承继的应用程序异常处置种。

  /// 自动将异常内容记要到Windows NT/2000的应用程序日记

  ///

  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

  {

  ///

  /// 事件日记记要种,提供事件日记记要支持

  ///

  /// 定义了4个日记记要步骤 (error, warning, info, trace)

  ///

  ///

  public class ApplicationLog

  {

  ///

  /// 将错误信息记要到Win2000/NT事件日记中

  ///亟需记要的文本信息

  ///

  public static void WriteError(String message)

  {

  WriteLog(TraceLevel.Error, message);

  }

本文来源:我的异常网 DotNet Exception

原创粉丝点击