在ASP.NET 2.0中操作数据之四十五:DataList和Repeater里的自定
在这个例子里,将LinkButton的CommandName设为“ShowProducts”,并将当前记录的主键– CategoryID –通过绑定语法绑定到CommandArgument(CategoryArgument='<%# Eval("CategoryID") %>')。完成这些后,LinkButton的声明语法看起来应该和下面差不多: <asp:LinkButton runat="server" CommandName="ShowProducts" CommandArgument='<%# Eval("CategoryID") %>' ID="ShowProducts"> Show Products</asp:LinkButton> 当button被点时,产生postback并激发DataList或Repeater的ItemCommand事件。Button的CommandName和CommandArgument值被传到event handler里。 为ItemCommand事件创建一个event handler,注意event handler的第二个参数(名字为e)。这个参数的类型为RepeaterCommandEventArgs,它有以下4个属性: CommandArgument – 被点的 button'的CommandArgument property 的值 由于选择的category的CategoryID通过CommandArgument传入,我们可以在ItemCommand event handler里获取与之相关的products。这些products在ItemTemplate(我们已经添加过了)里绑定到一个BulletedList。剩下的事就是添加BulletedList,在ItemCommand event handler里引用它,然后将选择的category的products绑定到BulletedList,我们将在第四步完成这个。 注意:DataList的ItemCommand event handler传入了一个DataListCommandEventArgs类型的对象,它提供和RepeaterCommandEventArgs 一样的4个属性。 第四步: 显示选择的Category的 Products 在ItemTemplate里显示products可以使用很多控件,我们可以添加一个嵌套的Repeater,DataList,DropDownList,GridView等。在这里我们使用BulletedList。回到CustomButtons.aspx page页的声明代码,在“Show Products” LinkButton后添加一个BulletedList。将ID设为ProductsInCategory。BulletedList显示那些通过DataTextField属性指定的字段值。由于将有product信息绑定到这个属性,我们将DataTextField设为ProductName。 <asp:BulletedList ID="ProductsInCategory" DataTextField="ProductName" runat="server"></asp:BulletedList> 在ItemCommand event handler里通过e.Item.FindControl("ProductsInCategory")引用这个控件,并与products绑定。 protected void Categories_ItemCommand(object source, RepeaterCommandEventArgs e) { if (e.CommandName == "ShowProducts") { // Determine the CategoryID int categoryID = Convert.ToInt32(e.CommandArgument); // Get the associated products from the ProudctsBLL and bind // them to the BulletedList BulletedList products = (BulletedList)e.Item.FindControl("ProductsInCategory"); ProductsBLL productsAPI = new ProductsBLL(); products.DataSource = productsAPI.GetProductsByCategoryID(categoryID); products.DataBind()); } } 在ItemCommand event handler里执行任何操作前,需要先检查传入的CommandName。由于ItemCommand event handler在任何button被点时都会执行,如果在template里有多个button时需要通过CommandName的值来辨别需要采取什么操作。由于我们这里只有一个button,因此在这里检查CommandName是没意义的,但是这是一个好习惯。然后,选择的category的CategoryID通过CommandArgument获取。然后引用Template里的BulletedList并绑定ProductsBLL类的GetProductsByCategoryID(categoryID)方法的结果。 在前面DataList里使用button的教程里,比如在DataList里编辑和删除数据概述,我们通过DataKeys集合来获取给定item的主键。这个方法在DataList里很好用,但是Repeater没有DataKeys属性。因此我们需要换一种方法来提供主键的值,比如通过button的 CommandArgument,或者在template使用一个隐藏的Label,然后通过e.Item.FindControl("LabelID")在ItemCommand event handler里读出它的值。 完成ItemCommand event handler后,浏览该页。见图7。点“Show Products” link会引起postback,并显示相关的products。而且,注意当点其它“Show Products” links时前面的product信息会保留。 注意:如果你需要修改这个报表的行为,比如一次只列出一个category的products,仅仅只需要将BulletedList的EnableViewState属性设为False。
总结 DataList和Repeater可以在templates里包含很多Buttons, LinkButtons, ImageButtons。这些button被点时会引起postback,并激发ItemCommand事件。为ItemCommand event.创建一个event handler可以将服务器端代码和点击button关联起来。在这个event handler里首先检查传入的CommandName的值来判断是哪个button被点了。其它另外的信息可以通过CommandArgument属性来提供。 祝编程快乐! 作者简介 本系列教程作者 Scott Mitchell,著有六本ASP/ASP.NET方面的书,是4GuysFromRolla.com的创始人,自1998年以来一直应用 微软Web技术。大家可以点击查看全部教程《[翻译]Scott Mitchell 的ASP.NET 2.0数据教程》,希望对大家的学习ASP.NET有所帮助。 (编辑:PHP编程网 - 黄冈站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |