zoukankan      html  css  js  c++  java
  • Repeater控件的详细讲解

    Repeater一般只用来展示数据,如果要增删改查(CRUD)则用ListView更方便。使用向导(强类型数据)来使用ListView会自动生成很多模板,免去手写模板代码的麻烦,再进行手工调整即可。

    首先设定数据源,然后点击智能提示中的“配置ListView”,选择一种布局和样式,然后根据需要勾选“启用编辑”、“启用删除”、“启用插入”、“启用分页”,就会自动生成常用的模板。注意这只是提高开发效率的一个快捷方式,不是唯一的途径。 LayoutTemplate为布局模板,布局模板中必须有一个ID为itemPlaceholder的服务端控件(4.0以后不需要),什么类型无所谓,不会被显示,itemPlaceholder前面就是相当于Repeater中的HeaderTemplate,itemPlaceholder后面就是相当于Repeater中的FooterTemplate,因此ListView中没有这两个模板。 ItemTemplate是每一项的模板,AlternatingItemTemplate是隔行显示的模板,和Repeater一样。EmptyDataTemplate为数据源没有数据的时候显示的内容(Insert也算数据),这样的话可以实现“没有查找结果”、“对不起,找不到您要找的数据”等,InsertItemTemplate为插入数据界面的模板,EditItemTemplate为编辑数据的模板,InsertItemTemplate,为插入数据的模板,SelectedItemTemplate为标记为Selected的行的模板。

    1、生成的样式要提到style中,不要内联样式。

    2、ItemTemplate里面一般也没必要用<asp:Label展示只读数据,所以直接输出<%# Eval("Name") %>

    3、LayoutTemplate中必须有一个id为itemPlaceholder的服务端控件,之上为头,之下为尾。

    4、LayoutTemplate表头内容要汉化,所有Template中的不需要显示的字段,比如Id,都要删掉。

    LayoutTemplate:指定用来定义ListView控件布局的根模板。它包括占位符对象,例如:table row (tr), div, 或 span 元素. 这个元素将被定义在ItemTemple模板或GroupTemple模板中的内容替换。也可以包含一个DataPager对象。 ItemTemplate:为 TemplateField 对象中的项指定要显示的内容。 ItemSeparatorTemplate:在 TemplateField 对象中的项之间指定要显示的内容。 GroupTemplate:为分组布局指定内容。它包括占位符对象,例如:table row (tr), div, 或 span 元素.这个元素将被定义在ItemTemple模板或EmptyItemTemplate模板中的内容替换。 GroupSeparatorTemplate:为分组项之间指定要显示的内容。 EmptyItemTemplate:指定使用GroupTemplate时的空项内容。例如,如果GroupItemCount属性设置为5,并且数据源返回的总数为8,ListView控件最后一行将有3项根据ItemTemple显示,两项根据EmptyTemplate显示。 EmptyDataTemplate:指定数据源为空时的内容。 SelectedItemTemplate:为选中项指定显示内容。 AlternatingItemTemplate:为交替项指定要显示的内容。 EditItemTemplate:为编辑项指定要显示的内容。当数据进行编辑时EditItemTemplate将替换ItemTemple的数据。 InsertItemTemplate:为插入项指定要显示的内容。当数据进行编辑时InsertItemTemplate将替换ItemTemple的数据。

    参考代码:(控件事件代码)

    //项绑定事件(也就是说:每个项生成之后都会执行此事件) protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e) { //判断当前生成是否是使用的 项模板 或 交替项模板 if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { MODEL.Aticle modelNow = e.Item.DataItem as MODEL.Aticle; if (modelNow.AContent.Length > 10) {

    HtmlTableCell cell = e.Item.FindControl("tdContent") as HtmlTableCell; //cell.BgColor = "Red"; } } } //Repeater中的按钮提交事件(被模板中所有的服务器按钮共用)

    protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e) {

    if (e.CommandName.Equals("Del")) {

    if (new BLL.Aticle().DelById(e.CommandArgument.ToString()) == true) {

    Response.Write(e.CommandArgument + "被删除啦!"); Repeater1.DataBind();//重新装载数据

    }

    } if (e.CommandName.Equals("Modify")) { Response.Write(e.CommandArgument + "被修改啦!");

    }

    }

  • 相关阅读:
    教程:在 Visual Studio 中开始使用 Flask Web 框架
    教程:Visual Studio 中的 Django Web 框架入门
    vs2017下发现解决python运行出现‘No module named "XXX""的解决办法
    《sqlite权威指南》读书笔记 (一)
    SQL Server手工插入标识列
    hdu 3729 I'm Telling the Truth 二分图匹配
    HDU 3065 AC自动机 裸题
    hdu 3720 Arranging Your Team 枚举
    virtualbox 虚拟3台虚拟机搭建hadoop集群
    sqlserver 数据行统计,秒查语句
  • 原文地址:https://www.cnblogs.com/sybboy/p/2863209.html
Copyright © 2011-2022 走看看