您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 销售管理 > (ASP[1].net课件)第七章数据绑定控件_(一)
数据绑定控件(一)第七章©2007iSoftStoneHoldingsLtd.AllRightsReserved.2回顾•Global.asax文件包含常用的Application_Start、Application_End、Session_Start、Session_End等事件•Application对象是存储于服务器的全局变量•Cookie存储信息于客户端•Session对象用于在服务器端存储用户的信息,在用户结束会话时被清除•新用户访问应用程序时会激活Session_Start事件,而用户退出应用程序时会触发Session_End事件©2007iSoftStoneHoldingsLtd.AllRightsReserved.3目标•理解数据绑定•使用DataList控件•使用数据视图排序和筛选•在ASP.NET中使用ADO.NET的事务处理©2007iSoftStoneHoldingsLtd.AllRightsReserved.4数据绑定简介2-1控件数据检索到的数据数据欢迎“”数据绑定是将数据链接到显示该数据的控件的过程©2007iSoftStoneHoldingsLtd.AllRightsReserved.5数据绑定简介2-2用于绑定控件的表达式置于%#......%标记之间©2007iSoftStoneHoldingsLtd.AllRightsReserved.6简单属性绑定asp:Imageid=imgVote1runat=serverHeight=12pxWidth=%#vote1%ImageUrl=red.bmp/asp:Imageasp:Labelid=lblVote1Text=%#vote1%runat=server/asp:Labelasp:Imageid=imgVote2runat=serverHeight=12pxWidth=%#vote2%ImageUrl=red.bmp/asp:Imageasp:Labelid=lblVote2Text=%#vote2%runat=server/asp:Labelasp:Imageid=imgVote3runat=serverHeight=12pxWidth=%#vote3%ImageUrl=red.bmp/asp:Imageasp:Labelid=lblVote3Text=%#vote3%runat=server/asp:Label代码视图//定义成员变量protectedstaticintvote1=0;protectedstaticintvote2=0;protectedstaticintvote3=0;privatevoidbtnVote1_Click(objectsender,System.EventArgse){vote1+=1;this.DataBind();}privatevoidbtnVote2_Click(objectsender,System.EventArgse){vote2+=1;this.DataBind();}privatevoidbtnVote3_Click(objectsender,System.EventArgse){vote3+=1;this.DataBind();}HTML视图运行结果©2007iSoftStoneHoldingsLtd.AllRightsReserved.7表达式绑定asp:Imageid=imgVote1runat=serverHeight=12pxWidth=%#4*vote1%ImageUrl=red.bmp/asp:Imageasp:Labelid=lblVote1Text=%#vote1%runat=server/asp:Labelasp:Imageid=imgVote2runat=serverHeight=12pxWidth=%#4*vote2%ImageUrl=red.bmp/asp:Imageasp:Labelid=lblVote2Text=%#vote2%runat=server/asp:Labelasp:Imageid=imgVote3runat=serverHeight=12pxWidth=%#4*vote3%ImageUrl=red.bmp/asp:Imageasp:Labelid=lblVote3Text=%#vote3%runat=server/asp:Label代码视图HTML视图运行结果//定义成员变量protectedstaticintvote1=0;protectedstaticintvote2=0;protectedstaticintvote3=0;privatevoidbtnVote1_Click(objectsender,System.EventArgse){vote1+=1;this.DataBind();}privatevoidbtnVote2_Click(objectsender,System.EventArgse){vote2+=1;this.DataBind();}privatevoidbtnVote3_Click(objectsender,System.EventArgse){vote3+=1;this.DataBind();}©2007iSoftStoneHoldingsLtd.AllRightsReserved.8方法的结果绑定protectedstringGetVotePercent(intvote){intsumVote=vote1+vote2+vote3;if(sumVote==0){return0%;}else{decimalpercent=100*(Convert.ToDecimal(vote)/Convert.ToDecimal(sumVote));returnpercent.ToString(n2)+%;}}建立一个新方法asp:Imageid=imgVote1runat=serverHeight=12pxWidth=%#vote1%ImageUrl=red.bmp/asp:Imageasp:Labelid=lblVote1Text=%#GetVotePercent(vote1)%runat=server/asp:Labelasp:Imageid=imgVote2runat=serverHeight=12pxWidth=%#vote2%ImageUrl=red.bmp/asp:Imageasp:Labelid=lblVote2Text=%#GetVotePercent(vote2)%runat=server/asp:Labelasp:Imageid=imgVote3runat=serverHeight=12pxWidth=%#vote3%ImageUrl=red.bmp/asp:Imageasp:Labelid=lblVote3Text=%#GetVotePercent(vote3)%runat=server/asp:Label调用方法的结果绑定运行结果©2007iSoftStoneHoldingsLtd.AllRightsReserved.9使用DataList控件显示数据3-1使用Datalist控件可以指定数据流WELCOMEWELCOME水平方式垂直方式Datalist控件项模板交替项模板页脚模板页眉模板编辑项模板选择项模板分隔符模板也可以为DataList控件设置要显示的数据列数和行数©2007iSoftStoneHoldingsLtd.AllRightsReserved.10使用DataList控件显示数据3-2示例:DatalistDemo.aspxprivatevoidPage_Load(objectsender,System.EventArgse){Response.Write(“centerbu带有交替列的数据列表/center”+“/b/ubr);if(!IsPostBack){DataTablemydt=newDataTable();DataRowmydr;mydt.Columns.Add(newDataColumn(Numbers“,typeof(Int32)));mydt.Columns.Add(newDataColumn(Squares“,typeof(Int32)));mydt.Columns.Add(newDataColumn(Cubes“,typeof(Int32)));续…for(inti=0;i30;i++){mydr=mydt.NewRow();mydr[0]=i;mydr[1]=i*i;mydr[2]=i*i*i;mydt.Rows.Add(mydr);}dlMyList.DataSource=mydt;dlMyList.DataBind();}}为DataList控件指定数据源将数据绑定到DataList©2007iSoftStoneHoldingsLtd.AllRightsReserved.11使用DataList控件显示数据3-3示例的HTML视图asp:DataListid=dlMyListRepeatDirection=HorizontalRepeatColumns=10runat=serverItemTemplate%#DataBinder.Eval(Container.DataItem,Numbers)%br%#DataBinder.Eval(Container.DataItem,Squares)%br%#DataBinder.Eval(Container.DataItem,Cubes)%br/ItemTemplateAlternatingItemTemplateib%#DataBinder.Eval(Container.DataItem,Numbers)%/bibrib%#DataBinder.Eval(Container.DataItem,Squares)%bibrib%#DataBinder.Eval(Container.DataItem,Cubes)%bibr/AlternatingItemTemplate/asp:DataList输出结果©2007iSoftStoneHoldingsLtd.AllRightsReserved.12DataBinder.Eval方法DataBinder.Eval方法用于在运行时计算数据绑定表达式,并按照要求格式化输出结果此方法有三个参数对为其表达式求值的对象的引用格式字符串数据表中的数据列名称用于显示指定格式的值命名容器数据字段名©2007iSoftStoneHoldingsLtd.AllRightsReserved.13使用DataView控件3-1DataView用于呈现DataTable中的数据的自定义视图NAMESNoDESIGAGE1.2.3.4.ABCDEFLMNXYG27605733PLGMMDTL职员数据NAMESNoDESIGAGE2.3.DEFLMN6057GMMDDataViewAge55©2007iSoftStoneHoldingsLtd.AllRightsReserved.14使用DataView控件3-2employee表的DataView控件仅检索年龄大于55的职员的记录mydv=newDataView(ds.Tables[employee]);mydv.RowFilter=age55;Sort属性用于以递增或递减顺序为行排序mydv.Sort=fnameASC;递增排列©2007iSoftStoneHoldingsLtd
本文标题:(ASP[1].net课件)第七章数据绑定控件_(一)
链接地址:https://www.777doc.com/doc-3089134 .html