总结ASP.NET中用到的几种代码

在服务器端创建按钮添加对话框(可能用到的比较少,但是可以这么干)

1 Button1.Attributes.Add("onclick","return confirm(’确认?’)");  
2 Button1.attributes.add("onclick","if(confirm(’are you sure...?’)){return true;}else{return false;}")  
View Code 

字符型转换,转为字符串 ToString() 的几个重载

1 12345.ToString("n"); //生成 12,345.00  
2 12345.ToString("C"); //生成 ¥12,345.00  
3 12345.ToString("e"); //生成 1.234500e+004  
4 12345.ToString("f4"); //生成 12345.0000  
5 12345.ToString("x"); //生成 3039 (16进制)  
6 12345.ToString("p"); //生成 1,234,500.00%
View Code

删除表格选定记录 

1 int intEmpID = (int)MyDataGrid.DataKeys[e.Item.ItemIndex];  
2 string deleteCmd = "DELETE from Employee where emp_id = " + intEmpID.ToString()
View Code

删除表格记录警告

 1 private void DataGrid_ItemCreated(Object sender,DataGridItemEventArgs e)  
 2 {  
 3  switch(e.Item.ItemType)  
 4  {  
 5   case ListItemType.Item :  
 6   case ListItemType.AlternatingItem :  
 7   case ListItemType.EditItem:  
 8    TableCell myTableCell;  
 9    myTableCell = e.Item.Cells[14];  
10    LinkButton myDeleteButton ;  
11    myDeleteButton = (LinkButton)myTableCell.Controls[0];  
12    myDeleteButton.Attributes.Add("onclick","return confirm(’您是否确定要删除这条信息’);");  
13    break;  
14   default:  
15    break;  
16  }  
17 
18 }
View Code

日期格式

1 DataFormatString="{0:yyyy-MM-dd}"  
2 e.items.cell["你的列"].text=DateTime.Parse(e.items.cell["你的列"].text.ToString("yyyy-MM-dd"))
View Code

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

1 不使用Response.Redirect,而使用Server.Transfer  
2 e.g  
3 // in global.asax  
4 protected void Application_Error(Object sender, EventArgs e) {  
5 if (Server.GetLastError() is HttpUnhandledException)  
6 Server.Transfer("MyErrorPage.aspx");  
7 //其余的非HttpUnhandledException异常交给ASP.NET自己处理就okay了 :)  
8 }  
9 Redirect会导致post-back的产生从而丢失了错误信息,所以页面导向应该直接在服务器端执行,这样就可以在错误处理页面得到出错信息并进行相应的处
View Code

清空Cookie 

1 Cookie.Expires=[DateTime];  
2 Response.Cookies("UserName").Expires = 0
View Code

自定义异常处理

 1 /自定义异常处理类  
 2 using System;  
 3 using System.Diagnostics;  
 4 
 5 namespace MyAppException  
 6 {  
 7  /// <summary>  
 8  /// 从系统异常类ApplicationException继承的应用程序异常处理类。  
 9  /// 自动将异常内容记录到Windows NT/2000的应用程序日志  
10  /// </summary>  
11  public class AppException:System.ApplicationException  
12  {  
13   public AppException()  
14   {  
15    if (ApplicationConfiguration.EventLogEnabled)LogEvent("出现一个未知错误。");  
16   }  
17 
18  public AppException(string message)  
19  {  
20   LogEvent(message);  
21  }  
22 
23  public AppException(string message,Exception innerException)  
24  {  
25   LogEvent(message);  
26   if (innerException != null)  
27   {  
28    LogEvent(innerException.Message);  
29   }  
30  }
View Code

回车转换成Tab 

1 <script language="javascript" for="document"
2 event="onkeydown"3  if(event.keyCode==13 && event.srcElement.type!=’button’ && event.srcElement.type!=’submit’ &&     event.srcElement.type!=’reset’ && event.srcElement.type!=’’&& event.srcElement.type!=’textarea’);  
4    event.keyCode=9;  
5 </script>  
6 onkeydown="if(event.keyCode==13) event.keyCode=9"
View Code

DataGrid超级连接列  

1 DataNavigateUrlField="字段名" DataNavigateUrlFormatString="http://xx/inc/delete.aspx?ID={0}"  
View Code

DataGrid行随鼠标变色  

1 private void DGzf_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)  
2 {  
3  if (e.Item.ItemType!=ListItemType.Header)  
4  {  
5   e.Item.Attributes.Add( "onmouseout","this.style.backgroundColor=""+e.Item.Style["BACKGROUND-COLOR"]+""");  
6   e.Item.Attributes.Add( "onmouseover","this.style.backgroundColor=""+ "#EFF3F7"+""");  
7  }  
8 }
View Code

模板列  

<ASP:TEMPLATECOLUMN visible="False" sortexpression="demo" headertext="ID">  
<ITEMTEMPLATE>  
<ASP:LABEL text=’<%# DataBinder.Eval(Container.DataItem, "ArticleID")%>’ runat="server" width="80%" id="lblColumn" />  
</ITEMTEMPLATE>  
</ASP:TEMPLATECOLUMN>  

<ASP:TEMPLATECOLUMN headertext="选中">  
<HEADERSTYLE wrap="False" horizontalalign="Center"></HEADERSTYLE>  
<ITEMTEMPLATE>  
<ASP:CHECKBOX id="chkExport" runat="server" />  
</ITEMTEMPLATE>  
<EDITITEMTEMPLATE>  
<ASP:CHECKBOX id="chkExportON" runat="server" enabled="true" />  
</EDITITEMTEMPLATE>  
</ASP:TEMPLATECOLUMN>  

  后台代码  

protected void CheckAll_CheckedChanged(object sender, System.EventArgs e)  
{  
 //改变列的选定,实现全选或全不选。  
 CheckBox chkExport ;  
 if( CheckAll.Checked)  
 {  
  foreach(DataGridItem oDataGridItem in MyDataGrid.Items)  
  {  
   chkExport = (CheckBox)oDataGridItem.FindControl("chkExport");  
   chkExport.Checked = true;  
  }  
 }  
 else  
 {  
  foreach(DataGridItem oDataGridItem in MyDataGrid.Items)  
  {  
   chkExport = (CheckBox)oDataGridItem.FindControl("chkExport");  
   chkExport.Checked = false;  
  }  
 }  
}
View Code

大小写转换

1 HttpUtility.HtmlEncode(string);  
2 HttpUtility.HtmlDecode(string)
View Code

日期格式化

1 aspx页面内:<%# DataBinder.Eval(Container.DataItem,"Company_Ureg_Date")%2 显示为: 2004-8-11 19:44:28  
3 想要:2004-8-114 <%# DataBinder.Eval(Container.DataItem,"Company_Ureg_Date","{0:yyyy-M-d}")%5 格式化日期 
6 取出来,是object((DateTime)objectFromDB).ToString("yyyy-MM-dd");  
View Code

时间格式化

 1 string aa=DateTime.Now.ToString("yyyy年MM月dd日");  
 2 
 3   1.1 取当前年月日时分秒  
 4 
 5 currentTime=System.DateTime.Now;  
 6 
 7   1.2 取当前年  
 8 
 9 int 年= DateTime.Now.Year;  
10 
11   1.3 取当前月  
12 
13 int 月= DateTime.Now.Month;  
14 
15   1.4 取当前日  
16 
17 int 日= DateTime.Now.Day;  
18 
19   1.5 取当前时  
20 
21 int 时= DateTime.Now.Hour;  
22 
23   1.6 取当前分  
24 
25 int 分= DateTime.Now.Minute;  
26 
27   1.7 取当前秒  
28 
29 int 秒= DateTime.Now.Second;  
30 
31   1.8 取当前毫秒  
32 
33 int 毫秒= DateTime.Now.Millisecond;
View Code
原文地址:https://www.cnblogs.com/sening/p/4591861.html