版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、数据绑定控件比较 (ReapterDataListGridViewDatailsViewFormView):1.插入功能方面:DetailsView和FormView具有插入功能,其它控件没有2.模板DataListFormViewRepeater三种必须编辑模板,而GridView和DetailsView只有在将列转换成模板列以后才会出现各种模板.3.自动分页功能GridView ,DetailsView和FormView都是2.0版本新增控件,内置了分页,排序等等功能,其他需要手工定义4.数据呈现方式:GridView,DataList,Repeator用于呈现多列数据,DetailsVi
2、ew,FormView用于呈现单列数据,即常用的数据明细. DataList和Reapter都需要编辑模板列,而在模板列当中可以添加TextBox,同时可以指定TextBox的ID从而实现提取用户输入的值,但是DataGrid和GridView两个件是不需要编辑模板的,它的编辑功能是自动生成的我们无法知道那些文本框的ID,也就无法通过ID来获取用户的输入,那么可以通过对单元格的引用来实现:private void DataGrid1_UpdateCommand(object source,xx) string bkid=DataGrid1.DataKeyse.Item.ItemIndex.to
3、String();/提取主键 string bktitle=(TextBox)e.Item.Cells1.Controls0).Text;/提取用户的输入一.进入编辑状态:DataList1.EditItemIndex = e.Item.ItemIndex;DataGrid1.EditItemIndex = e.Item.ItemIndex;GridView1.EditIndex = e.NewEditIndex;DetailsView1.ChangeMode(DetailsViewMode.Edit);/进入编辑状态DetailsView1.ChangeMode(DetailsViewMod
4、e.ReadOnly);/退出编辑状态二.设置主键:DataList1.DataKeyField = "bkid"DataGrid1.DataKeyField = "bkid"string str="bkid"GridView1.DataKeyNames = str;三.提取主键:string bkid = DataList1.DataKeyse.Item.ItemIndex.ToString();/DataListstring bkid = DataGrid1.DataKeyse.Item.ItemIndex.ToString();
5、/DataGridstring bkid = GridView1.DataKeyse.RowIndex.Value.ToString();/GridViewstring bkid = DetailsView1.DataKey0.ToString();四.查找控件:string bktitle = (TextBox)e.Item.FindControl("txtTile").Text;/DataListstring bktitle = (TextBox)e.Item.Cells1.Controls0).Text;/DataGridstring bktitle = (TextB
6、ox)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).Text;/这是索引五.给删除按
7、钮添加确认: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","retur
8、n confirm(确定要删除吗?)"); 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 c
9、onfirm(确认删除?)"); protected void GridView1_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+""?)&qu
10、ot;); /用了两个转义符将第一列的值用引号括起来,注意转义符后面一个将不被解释,是直接放上去; ASP.NET 程序中常用的三十三种代码(1) 1. 打开新的窗口并传送参数:传送参数: response.write("scriptwindow.open(*.ASPx?id="+this.DropDownList1.SelectIndex+"&id1="+.+")/script") 接收参数: string a = Request.QueryString("id&q
11、uot;); string b = Request.QueryString("id1"); 2.为按钮添加对话框 Button1.Attributes.Add("onclick","return confirm(确认?)"); button.attributes.add("onclick","if(confirm(are you sure.?)return true;elsereturn false;") 3.删除表格选定记录 int intEmpID = (int)MyDataGrid.Dat
12、aKeyse.Item.ItemIndex; string deleteCmd = "DELETE from Employee where emp_id = " + intEmpID.ToString() 4.删除表格记录警告private void DataGrid_ItemCreated(Object sender,DataGridItemEventArgs e) switch(e.Item.ItemType) case ListItemType.Item : case ListItemType.AlternatingItem : case ListItemType.E
13、ditItem: TableCell myTableCell; myTableCell = e.Item.Cells14; LinkButton myDeleteButton ; myDeleteButton = (LinkButton)myTableCell.Controls0; myDeleteButton.Attributes.Add("onclick","return confirm(您是否确定要删除这条信息);"); break; default: break; 5.点击表格行链接另一页private void grdCustomer_Item
14、DataBound(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 + ");"); 双击表格连
15、接到另一页 在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 程序中常用的
16、三十三种代码(2) 双击表格打开新一页 if(e.Item.ItemType = ListItemType.Item e.Item.ItemType = ListItemType.AlternatingItem) string OrderItemID =e.item.cells1.Text; . e.item.Attributes.Add("ondblclick", "open(./ShippedGrid.aspx?id=" + OrderItemID + ")"); 6.表格超连接列传递参数asp:HyperLinkColumn T
17、arget="_blank" headertext="ID号" DataTextField="id" NavigateUrl="aaa.aspx?id= %# DataBinder.Eval(Container.DataItem, "数据字段1")% & name=%# DataBinder.Eval(Container.DataItem, "数据字段2")% / 7.表格点击改变颜色if (e.Item.ItemType = ListItemType.Item e.Item.
18、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.AlternatingIte
19、m) 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=;"); 8.关于日期格式日期格式设定
20、DataFormatString="0:yyyy-MM-dd" 我觉得应该在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,
21、 EventArgs e) if (Server.GetLastError() is HttpUnhandledException) Server.Transfer("MyErrorPage.aspx"); /其余的非HttpUnhandledException异常交给ASP.NET自己处理就okay了 :) Redirect会导致postback的产生从而丢失了错误信息,所以页面导向应该直接在服务器端执行,这样就可以在错误处理页面得到出错信息并进行相应的处理 10.清空Cookie<>ASP.NET 程序中常用的三十三种代码(3) Cookie.EXPires
22、=DateTime; Response.Cookies("UserName").Expires = 0 11.自定义异常处理/自定义异常处理类 using System; using System.DiagnostiCS; namespace MyAppException / summary / 从系统异常类ApplicationException继承的应用程序异常处理类。 / 自动将异常内容记录到Windows NT/2000的应用程序日志 / /summary public class AppException:System.ApplicationException p
23、ublic 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); /日志记
24、录类 using System; using System.Configuration; using System.Diagnostics; using System.IO; using System.Text; using System.Threading; namespace MyEventLog / summary / 事件日志记录类,提供事件日志记录支持 / remarks / 定义了4个日志记录方法 (error, warning, info, trace) / /remarks / /summary public class ApplicationLog / summary / 将
25、错误信息记录到Win2000/NT事件日志中 / param name="message"需要记录的文本信息/param / /summary public static void WriteError(String message) WriteLog(TraceLevel.Error, message); / summary / 将警告信息记录到Win2000/NT事件日志中 / param name="message"需要记录的文本信息/param / /summary public static void WriteWarning(String m
26、essage) WriteLog(TraceLevel.Warning, message); <>ASP.NET 程序中常用的三十三种代码(4) / summary / 将提示信息记录到Win2000/NT事件日志中 / param name="message"需要记录的文本信息/param / /summary public static void WriteInfo(String message) WriteLog(TraceLevel.Info, message); / summary / 将跟踪信息记录到Win2000/NT事件日志中 / param n
27、ame="message"需要记录的文本信息/param / /summary public static void WriteTrace(String message) WriteLog(TraceLevel.Verbose, message); / summary / 格式化记录到事件日志的文本信息格式 / param name="ex"需要格式化的异常对象/param / param name="catchInfo"异常信息标题字符串./param / retvalue / para格式后的异常信息字符串,包括异常内容和跟踪堆栈
28、./para / /retvalue / /summary public static 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.StackT
29、race); return strBuilder.ToString(); / summary / 实际事件日志写入方法 / param name="level"要记录信息的级别(error,warning,info,trace)./param / param name="messageText"要记录的文本./param / /summary private static void WriteLog(TraceLevel level, String messageText) try EventLogEntryType LogEntryType; swit
30、ch (level) case TraceLevel.Error: LogEntryType = EventLogEntryType.Error; break; case TraceLevel.Warning: LogEntryType = EventLogEntryType.Warning; break; case TraceLevel.Info: LogEntryType = EventLogEntryType.Information; break; case TraceLevel.Verbose: LogEntryType = EventLogEntryType.SUCcessAudit
31、;ASP.NET 程序中常用的三十三种代码(5) break; default: LogEntryType = EventLogEntryType.SuccessAudit; break; EventLog eventLog = new EventLog("Application", ApplicationConfiguration.EventLogMachineName, ApplicationConfiguration.EventLogSourceName ); /写入事件日志 eventLog.WriteEntry(messageText, LogEntryType)
32、; catch /忽略任何异常 /class ApplicationLog 12.Panel 横向滚动,纵向自动扩展asp:panel style="overflow-x:scroll;overflow-y:auto;"/asp:panel 13.回车转换成Tabscript language="Javascript" for="document" event="onkeydown" if(event.keyCode=13 && event.srcElement.type!=button &
33、& event.srcElement.type!=submit && event.srcElement.type!=reset && event.srcElement.type!=&& event.srcElement.type!=textarea); event.keyCode=9; /script onkeydown="if(event.keyCode=13) event.keyCode=9" 14.DataGrid超级连接列ataNavigateUrlField="字段名" DataNavig
34、ateUrlFormatString="http:/xx/inc/delete.aspx?ID=0" 15.DataGrid行随鼠标变色private void DGzf_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e) if (e.Item.ItemType!=ListItemType.Header) e.Item.Attributes.Add( "onmouseout","this.style.backgroundColor=&qu
35、ot;"+e.Item.Style"BACKGROUND-COLOR"+"""); e.Item.Attributes.Add( "onmouseover","this.style.backgroundColor=""+ "#EFF3F7"+"""); 16.模板列ASP:TEMPLATECOLUMN visible="False" sortexpression="demo" headertext
36、="ID" ITEMTEMPLATE ASP:LABEL text=%# DataBinder.Eval(Container.DataItem, "ArticleID")% runat="server" width="80%" id="lblColumn" / /ITEMTEMPLATE <>ASP.NET 程序中常用的三十三种代码(6) /ASP:TEMPLATECOLUMN ASP:TEMPLATECOLUMN headertext="选中" HEADERST
37、YLE wrap="False" horizontalalign="Center"/HEADERSTYLE ITEMTEMPLATE ASP:CHECKBOX id="chkExport" runat="server" / /ITEMTEMPLATE EDITITEMTEMPLATE ASP:CHECKBOX id="chkExportON" runat="server" enabled="true" / /EDITITEMTEMPLATE /ASP:TE
38、MPLATECOLUMN 后台代码 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 =
39、 true; else foreach(DataGridItem oDataGridItem in MyDataGrid.Items) chkExport = (CheckBox)oDataGridItem.FindControl("chkExport"); chkExport.Checked = false; 17.数字格式化【%#Container.DataItem("price")%的结果是500.0000,怎样格式化为500.00?】 %#Container.DataItem("price"
40、;,"0:¥#,#0.00")% int i=123456; string s=i.ToString("#,#.00"); 18.日期格式化 【aspx页面内:%# DataBinder.Eval(Container.DataItem,"Company_Ureg_Date")% 显示为: 2004-8-11 19:44:28 我只想要:2004-8-11 】 %# DataBinder.Eval(Container.DataItem,"Company_Ureg_Date","0:yyyy-M-d"
41、;)% 应该如何改? 【格式化日期】 取出来,一般是object(DateTime)objectFromDB).ToString("yyyy-MM-dd"); 【日期的验证表达式】 A.以下正确的输入格式: 2004-2-29, 2004-02-29 10:29:39 pm, 2004/12/31 (d2(02468048)(1357926)-/s?(0?13578)(102)-/s?(0?1-9)(1-20-9)(301)(0?469)(11)-/s?(0?1-9)(1-20-9)(30)(0?2-/s?(0?1-9)(1-20-9)(d2(024681235679)(1
42、357901345789)-/s?(0?13578)(102)-/s?(0?1-9)(1-20-9)(301)(0?469)(11)-/s?(0?1-9)(1-20-9)(30)(0?2-/s?(0?1-9)(10-9)(20-8)(s(0?1-9)(10-2):(0-50-9)(s)(:(0-50-9)s)(AMPMampm2,2)?$ <>ASP.NET 程序中常用的三十三种代码(7) B.以下正确的输入格式:0001-12-31, 9999 09 30, 2002/03/03 d4-/s?(013578)(102)-/s?(0-20-9)(301)(0469)(11)-/s?
43、(0-20-9)(30)(02-/s?0-20-9)$ 【大小写转换】 HttpUtility.HtmlEncode(string); HttpUtility.HtmlDecode(string) 19.如何设定全局变量Global.asax中 Application_Start()事件中 添加Application属性名 xxx; 就是你的全局变量 20.怎样作到HyperLinkColumn生成的连接后,点击连接,打开新窗口?HyperLinkColumn有个属性Target,将器值设置成"_blank"即可.(Target="_blank") 【A
44、SPNETMENU】点击菜单项弹出新窗口 在你的menuData.XML文件的菜单项中加入URLTarget="_blank",如: ?xml version="1.0" encoding="GB2312"? MenuData ImagesBaseURL="images/" MenuGroup MenuItem Label="内参信息" URL="Infomation.aspx" MenuGroup ID="BBC" MenuItem Label=&quo
45、t;公告信息" URL="Infomation.aspx" URLTarget="_blank" LeftIcon="file.gif"/ MenuItem Label="编制信息简报" URL="NewInfo.aspx" LeftIcon="file.gif" / . 最好将你的aspnetmenu升级到1.2版 21.读取DataGrid控件TextBox值foreach(DataGrid dgi in yourDataGrid.Items) TextBox
46、tb = (TextBox)dgi.FindControl("yourTextBoxId"); tb.Text. 23.在DataGrid中有3个模板列包含Textbox分别为 DG_ShuLiang (数量) DG_DanJian(单价) DG_JinE(金额)分别在5.6.7列,要求在录入数量及单价的时候自动算出金额即:数量*单价=金额还要求录入时限制为数值型.我如何用客户端脚本实现这个功能? 思归 asp:TemplateColumn HeaderText="数量" ItemTemplate asp:TextBox id="ShuLian
47、g" runat=server Text=%# DataBinder.Eval(Container.DataItem,"DG_ShuLiang")% onkeyup="javascript:DoCal()" / asp:RegularExpressionValidator id="revS" runat="server" ControlToValidate="ShuLiang" ErrorMessage="must be integer" ValidationExp
48、ression="d+$" / /ItemTemplate /asp:TemplateColumn asp:TemplateColumn HeaderText="单价" <>ASP.NET 程序中常用的三十三种代码(8) ItemTemplate asp:TextBox id="DanJian" runat=server Text=%# DataBinder.Eval(Container.DataItem,"DG_DanJian")% onkeyup="javascript:DoCal()&q
49、uot; / asp:RegularExpressionValidator id="revS2" runat="server" ControlToValidate="DanJian" ErrorMessage="must be numeric" ValidationExpression="d+(.d*)?$" / /ItemTemplate /asp:TemplateColumn asp:TemplateColumn HeaderText="金额" ItemTemplate
50、asp:TextBox id="JinE" runat=server Text=%# DataBinder.Eval(Container.DataItem,"DG_JinE")% / /ItemTemplate /asp:TemplateColumnscript language="javascript" function DoCal() var e = event.srcElement; var row = e.parentNode.parentNode; var txts = row.all.tags("INPUT&qu
51、ot;); if (!txts.length txts.length 3) return; var q = txtstxts.length-3.value; var p = txtstxts.length-2.value; if (isNaN(q) isNaN(p) return; q = parseInt(q); p = parseFloat(p); txtstxts.length-1.value = (q * p).toFixed(2); /script 24.datagrid选定比较底下的行时,为什么总是刷新一下,然后就滚动到了最上面,刚才选定的行因屏幕的关系就看不到了。page_loa
52、d page.smartNavigation=true 25.在Datagrid中修改数据,当点击编辑键时,数据出现在文本框中,怎么控制文本框的大小 ? private void DataGrid1_ItemDataBound(obj sender,DataGridItemEventArgs e) for(int i=0;ie.Item.Cells.Count-1;i+) if(e.Item.ItemType=ListItemType.EditType) e.Item.Cellsi.Attributes.Add("Width", "80px") 26.对
53、话框private static string ScriptBegin = "script language="JavaScript"" private static string ScriptEnd = "/script" public static void ConfirmMessageBox(string PageTarget,string Content) string ConfirmContent="var retValue=window.confirm("+Content+");"+
54、"if(retValue)window.location="+PageTarget+"" ConfirmContent=ScriptBegin + ConfirmContent + ScriptEnd;ASP.NET 程序中常用的三十三种代码(9) Page ParameterPage = (Page)System.Web.HttpContext.Current.Handler; ParameterPage.RegisterStartupScript("confirm",ConfirmContent); /Response.Write
55、(strScript); 27. 将时间格式化:string aa=DateTime.Now.ToString("yyyy年MM月dd日"); 1.1 取当前年月日时分秒 currentTime=System.DateTime.Now; 1.2 取当前年 int 年= DateTime.Now.Year; 1.3 取当前月 int 月= DateTime.Now.Month; 1.4 取当前日 int 日= DateTime.Now.Day; 1.5 取当前时 int 时= DateTime.Now.Hour; 1.6 取当前分 int
56、 分= DateTime.Now.Minute; 1.7 取当前秒 int 秒= DateTime.Now.Second; 1.8 取当前毫秒 int 毫秒= DateTime.Now.Millisecond; 28自定义分页代码: 先定义变量 : public static int pageCount; /总页面数 public static int curPageIndex=1; /当前页面 下一页: if(DataGrid1.CurrentPageIndex (DataGrid1.PageCount - 1) DataGrid1.CurrentPageIndex += 1; curPageIndex+=1; bind(); / DataGrid1数据绑定函数 上一页: if(DataGrid1.CurrentPageIndex 0) DataGrid1.CurrentPag
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026年博物馆教育资源与多元智能对接
- 2026年餐饮业预防食物中毒关键控制点培训
- 2026年止痛药合理使用讲座课件
- 2026年小学生美术创造力培养的实践与效果评估
- 2026年文化金融支持绿色文化项目发展
- 2026年银行客户经理信贷业务入门与风控要点
- 2026年原辅料入厂检验标准制定与检验频次优化
- 肌内注射:中职护理案例分析
- 2026年平台经济下外卖骑手权益保障与职业发展困境调研
- 2026年银行突发公共卫生事件演练
- 教育局中小学考试命题管理方案
- 光大金瓯资产管理有限公司笔试
- 2025年中国邮政集团有限公司湖北省分公司招聘笔试备考试题及完整答案详解1套
- 2025年建筑施工特种作业人员考试建筑电焊工题库(附答案)
- 构建人类命运共同体+课件-2025-2026学年高中政治统编版选择性必修一
- 2025年善意的谎言辩论会材料及流程
- 2025年辽宁卷历史高考试卷(原卷+答案)
- 检验科个人防护培训课件
- 小儿骨科课件
- 2025年不动产登记业务知识试题及答案
- 2025年内部审计人员考试题库
评论
0/150
提交评论