1、ASPNET五大数据控件数据绑定控件比较 (ReapterDataListGridViewDatailsViewFormView):1.插入功能方面 :DetailsView 和 FormView 具有插入功能 ,其它控件没有2.模板DataListFormViewRepeater 三种必须编辑模板 ,而GridView 和 DetailsView 只有在将列转换成模板列以后才会出现各种模板 .3.自动分页功能GridView ,DetailsView 和 FormView 都是 2.0 版本新增控件 ,内置了分页 ,排序等等功能 其他需要手工定义4.数据呈现方式 : GridView,Dat
2、aList,Repeator 用于呈现多列数据 , DetailsView,FormView 用于呈现单列数据 ,即常用的数据明细 .DataList 和 Reapter 都需要编辑模板列,而在模板列当中可以添加 TextBox ,同时可以指定 TextBox 的 ID 从而实现提取用户输入的值, 但是 DataGrid 和 GridView 两个件是不需要编辑 模板的, 它的编辑功能是自动生成的我们无法知道那些文本框的 ID ,也就无法通过 ID 来获 取用户的输入,那么可以通过对单元格的引用来实现 : private void DataGrid1_UpdateCommand(object
3、source,xx)string bkid=DataGrid1.DataKeyse.Item.ItemIndex.toString();/ 提取主键string bktitle=(TextBox)e.Item.Cells1.Controls0).Text;/ 提取用户的输入1.进入编辑状态:DataList1.EditItemIndex = e.Item.ItemIndex;DataGrid1.EditItemIndex = e.Item.ItemIndex;GridView1.EditIndex = e.NewEditIndex;DetailsView1.ChangeMode(Details
4、ViewMode.Edit);/ 进入编辑状态 DetailsView1.ChangeMode(DetailsViewMode.ReadOnly);/ 退出编辑状态2.设置主键 :DataList1.DataKeyField = bkid;DataGrid1.DataKeyField = bkid;string str=bkid;GridView1.DataKeyNames = str;3.提取主键:string bkid = DataList1.DataKeyse.Item.ItemIndex.ToString();/DataListstring bkid = DataGrid1.DataK
5、eyse.Item.ItemIndex.ToString();/DataGridstring bkid = GridView1.DataKeyse.RowIndex.Value.ToString();/GridViewstring bkid = DetailsView1.DataKey0.ToString();4.查找控件:string bktitle = (TextBox)e.Item.FindControl(txtTile).Text;/DataListstring bktitle = (TextBox)e.Item.Cells1.Controls0).Text;/DataGridstri
6、ng bktitle = (TextBox)GridView1.Rowse.RowIndex.Cells1.Controls0).Text;string bktitle = (TextBox)DetailsView1.Rows1.Cells1.Controls0).Text;注意查找控件有两种方法: (各数据绑定控件的都可以用下面两种方法进行查找 )1.如果知道控件的 ID 可以用这种方法 (TextBox)e.Item.FindControl(txtTile).Text;/ 这是查找 2.如果不知道控件的 ID 可用这种方法 (TextBox)e.Item.Cells1.Controls0)
7、.Text;/ 这是索引5.给删除按钮添加确认 :protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)if (e.Item.ItemType = ListItemType.Item | e.Item.ItemType ListItemType.AlternatingItem)LinkButton lbtn =(LinkButton) e.Item.FindControl(lbtndelete);lbtn.Attributes.Add(OnClick,return confirm( 确定要删除
8、吗 ? );protected void DataGrid1_ItemDataBound(object sender, DataGridItemEventArgs e)if(e.Item.ItemType=ListItemType.Item | e.Item.ItemType=ListItemType.AlternatingItem)LinkButton lbtn = (LinkButton)e.Item.Cells3.Controls0; lbtn.Attributes.Add(OnClick,return confirm( 确认删除? );protected void GridView1_
9、RowDataBound(object sender, GridViewRowEventArgs e) if(e.Row.RowType= DataControlRowType.DataRow) string strid = e.Row.Cells0.Text;/ 获取第一行的字段值 ; e.Row.Cells3.Attributes.Add(OnClick, return confirm( 确 认 删 除 +strid+ ? );/用了两个转义符将第一列的值用引号括起来 ,注意转义符后面一个将不被解释,是直接放上去 ;ASP.NET 程序中常用的三十三种代码 (1)1.打开新的窗口并传送参数
10、: 传送参数:response.write( v script window.open( ASPx?id=+this.DropDownListl.Selectlndex+&id1=+.+ v /script )接收参数:string a = Request.QueryString(id);string b = Request.QueryString(id1);2.为按钮添加对话框Button1.Attributes.Add(onclick,return confirm( 确认 ?);button.attributes.add(onclick,if(confirm( are yousure.?
11、)return true;elsereturn false;)3.删除表格选定记录int intEmplD = (int)MyDataGrid.DataKeyse.ltem.ltemlndex; string deleteCmd = DELETE from Employee where emp_id = + intEmplD.ToString()4.删除表格记录警告private void DataGrid_ltemCreated(Object sender,DataGridltemEventArgs e)switch(e.ltem.ltemType)case ListltemType.lte
12、m :case ListltemType.Alternatingltem :case ListItemType.EditItem:TableCell myTableCell; myTableCell = e.Item.Cells14;LinkButton myDeleteButton ;myDeleteButton = (LinkButton)myTableCell.Controls0; myDeleteButton.Attributes.Add(onclick,re turn confirm( 您是 否确定要删除这条信息 ););break; default:break;5.点击表格行链接另
13、一页 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.Cells0.Text + );); 双击表格连接到另一
14、页 在 itemDataBind 事件中if(e.Item.ItemType = ListItemType.Item e.Item.ItemType = ListItemType.AlternatingItem) string OrderItemID =e.item.cells1.Text;e.item.Attributes.Add(ondblclick, location.href= ./ShippedGrid.aspx?id= + OrderItemID + );ASP.NET 程序中常用的三十三种代码 (2) 双击表格打开新一页if(e.Item.ItemType = ListItemT
15、ype.Item e.Item.ItemType = ListItemType.AlternatingItem) string OrderItemID =e.item.cells1.Text;e.item.Attributes.Add(ondblclick, open( ./ShippedGrid.aspx?id= + OrderItemID + );6.表格超连接列传递参数v asp:HyperL in kColu mn Target=_bla nk headertext=ID 号DataTextField=id NavigateUrl=aaa.aspx?id= v %#DataB in d
16、er.Eval(Co ntai ner.Dataltem, 数据字段 1)% & name=v%# DataBinder.Eval(Container.Dataltem, 数据字段 2)% / 7.表格点击改变颜色if (e.ltem.ltemType = ListltemType.ltem e.ltem.ltemType = ListltemType.Alternatingltem) e.ltem.Attributes.Add(onclick,this.style.backgroundColor= #99cc00 ;this.style.color= buttontext ;this.sty
17、le.cursor= default ;); 写在 DataGrid 的temDataBound 里if (e.ltem.ltemType = ListltemType.ltem e.ltem.ltemType = ListltemType.Alternatingltem) e.ltem.Attributes.Add(onmouseover,this.style.backgroundColor= #99cc00 ;this.style.color= buttontext ;this.style.cursor= default ;); e.ltem.Attributes.Add(onmouseo
18、ut,this.style.backgroundColor= ;this.style.color= ;); 8.关于日期格式日期格式设定DataFormatString=0:yyyy-MM-dd我觉得应该在 itembound 事件中e.items.cell 你的列.text=DateTime.Parse(e.items.cell 你的列.text.ToString(yyyy-MM-dd)9.获取错误信息并到指定页面不要使用 Response.Redirect, 而应该使用 Server.Transfere.g/ in global.asaxprotected void Application
19、_Error(Object sender, EventArgs e) if (Server.GetLastError() is HttpUnhandledException) Server.Transfer(MyErrorPage.aspx);/其余的非HttpUnhandledException 异常交给ASP.NET自己处理就okay 了 :) Redirect 会导致 postback 的产生从而丢失了错误信息,所以页面导向 应该直接在服务器端执行,这样就可以在错误处理页面得到出错信息并进行相应 的处理10.清空 CookieASP.NET 程序中常用的三十三种代码 (3)Cookie.
20、E XPires=DateTime;Response.Cookies(UserName).Expires = 011.自定义异常处理/ 自定义异常处理类using System;using System.Diagnosti CS;namespace MyAppException / v summary/ 从系统异常类 ApplicationException 继承的应用程序异常处理类。/ 自动将异常内容记录到 Windows NT/2000 的应用程序日志/ v /summarypublic class AppException:System.ApplicationException publ
21、ic 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;u
22、sing System.Configuration;using System.Diagnostics;using System.IO;using System.Text;using System.Threading;namespace MyEventLog / v summary/ 事件日志记录类,提供事件日志记录支持/ v remarks/ 定义了 4 个日志记录方法 (error, warning, info, trace)/ v /remarks / v /summarypublic class ApplicationLog / v summary/ 将错误信息记录到 Win2000/N
23、T 事件日志中/ v param name=message 需要记录的文本信息v /param / v /summarypublic static void WriteError(String message)WriteLog(TraceLevel.Error, message);/ v summary/ 将警告信息记录到 Win2000/NT 事件日志中/ v param n ame=message 需要记录的文本信息v /param / v /summarypublic static void WriteWarning(String message)WriteLog(TraceLevel.
24、Warning, message);ASP.NET 程序中常用的三十三种代码 (4) / v summary / 将提示信息记录到 Win2000/NT 事件日志中III v param name=message 需要记录的文本信息 v /param / v /summary public static void WriteInfo(String message)WriteLog(TraceLevel.Info, message);/ v summary / 将跟踪信息记录到 Win2000/NT 事件日志中/ v param name=message 需要记录的文本信息 v /param /
25、 v /summary public static void WriteTrace(String message)WriteLog(TraceLevel.Verbose, message);/ v summary / 格式化记录到事件日志的文本信息格式/ v param name=ex 需要格式化的异常对象v /param / v param name=catchInfo 异常信息标题字符串 .v /param/ v retvalue / v para格式后的异常信息字符串,包括异常内容和跟踪堆栈 .v/para/ v /retvalue / v /summary public static
26、String FormatException(Exception ex, String catchInfo)StringBuilder strBuilder = new StringBuilder();if (catchInfo != String.Empty) strBuilder.Append(catchInfo).Append(rn);strBuilder.Append(ex.Message).Append(rn).Append(ex.StackTrace);return strBuilder.ToString();/ v summary / 实际事件日志写入方法/ v param na
27、me=level 要记录信息的级别( error,warning,info,trace). v /param/ v param name=messageText 要记录的文本 .v /param/ v /summaryprivate static void WriteLog(TraceLevel level, String messageText)tryEventLogEntryType LogEntryType;switch (level)case TraceLevel.Error:LogEntryType = EventLogEntryType.Error; break;case Trac
28、eLevel.Warning:LogEntryType = EventLogEntryType.Warning; break;case TraceLevel.Info:LogEntryType = EventLogEntryType.Information; break;case TraceLevel.Verbose:LogEntryType = EventLogEntryType.S UCcessAudit;ASP.NET 程序中常用的三十三种代码 (5) break;default:LogEntryType = EventLogEntryType.SuccessAudit; break;E
29、ventLog eventLog = new EventLog(Application,ApplicationConfiguration.EventLogMachineName, ApplicationConfiguration.EventLogSourceName ); /写入事件日志eventLog.WriteEntry(messageText, LogEntryType);catch / 忽略任何异常 /class ApplicationLog12.Panel 横向滚动,纵向自动扩展v asp:panel style=overflow-x:scroll;overflow-y:auto; v /asp:panel13.回车转换成 Tab&v script la nguage= Javascript for=docume nt eve nt=o nkeydow n if(event.keyCode=13 & event.srcElement.type!= button event.srcElement.type!= submit & event.srcElement.type!= reset & event.srcElement.type!= event.srcElement.t
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1