ASP.NET程序中常用的三十三种代码

来源:互联网 发布:我的兄弟是逗比网络剧 编辑:程序博客网 时间:2024/05/18 03:45
1. 打开新的窗口并传送参数:   

  传送参数:
Code
接收参数:
Code
2.为按钮添加对话框
Code
3.删除表格选定记录
Code
4.删除表格记录警告
Code
5.点击表格行链接另一页
Code
双击表格连接到另一页 

  在itemDataBind事件中
Code
双击表格打开新一页
Code

★特别注意:【?id=】 处不能为 【?id =】

Code

7.表格点击改变颜色



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里

Code

8.关于日期格式

  日期格式设定

Code

我觉得应该在itembound事件中



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

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

  不要使用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的产生从而丢失了错误信息,所以页面导向应该直接在服务器端执行,这样就可以在错误处理页面得到出错信息并进行相应的处理

  10.清空Cookie

Code