Repeater使用详细指南

ASP.NET WebForm开发中尽量少用系统提供的runat="server"的服务器控件,尤其像GridView之类的“重量级”武器,自动生成的ViewState实在让人不敢恭维。但是用Repeater做数据绑定、展示以及表格记录处理还是很方便的。

如页面要实现下图效果:

绑定数据

数据可以用 <%#Eval("字段名")%> 这种形式在标签中绑定,参考之前写的文章

 <asp:Repeater ID="rpt" runat="server" onitemdatabound="rep_ItemDataBound">
<ItemTemplate>
<%#Eval("ID") %>、<%#Eval("Name") %>、<asp:Label ID="lblSex" runat="server" Text=""></asp:Label>
<br />
</ItemTemplate>
</asp:Repeater>

也可以用另一种方式,在CodeBehind方法rpt_ItemDataBound(object sender, RepeaterItemEventArgs e)中进行数据绑定。

 protected void rpt_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
Label lblTopicID = (Label)e.Item.FindControl("lblTopicID");
lblTopicID.Text = ((TopicType)e.Item.DataItem).TopicID.ToString(); TextBox txtTopicText = (TextBox)e.Item.FindControl("txtTopicText");
txtTopicText.Text = ((TopicType)e.Item.DataItem).Name; Label lblCreateTime = (Label)e.Item.FindControl("lblCreateTime");
lblCreateTime.Text = ((TopicType)e.Item.DataItem).CreateTime.ToString(); Label lblLastUpdateTime = (Label)e.Item.FindControl("lblLastUpdateTime");
lblLastUpdateTime.Text = ((TopicType)e.Item.DataItem).LastTime.ToString();
}

操作每条记录

如果想对每条记录做操作(修改、删除、置顶等),可以在后台方法rpt_ItemCommand(object source, RepeaterCommandEventArgs e)中进行。

 protected void rpt_ItemCommand(object source, RepeaterCommandEventArgs e)
{
RepeaterItem ri = rpt.Items[e.Item.ItemIndex]; //选中行
Label lblTopicID = (Label)ri.FindControl("lblTopicID");
TextBox txtTopicText = (TextBox)ri.FindControl("txtTopicText");
LinkButton lbUpdateTopic = (LinkButton)ri.FindControl("lbUpdateTopic"); int topicID = CommonFunc.ToInt(lblTopicID.Text.Trim());
string topicName = txtTopicText.Text.Trim(); switch (e.CommandName)
{
case "top"://置顶 break;
case "update"://修改 break;
case "del"://删除 break;
}
}

或者使用另一种方法,在每条记录的操作按钮事件上处理,例如:

 protected void lbDeleteUser_Click(object sender, EventArgs e)
{
LinkButton lbUpdateUser = (LinkButton)sender;
RepeaterItem ri = (RepeaterItem)lbUpdateUser.NamingContainer; //获取当前操作的记录所在行 TextBox txtUID = (TextBox)ri.FindControl("txtUID"); //获取当前行的ID
Label lblCn1Account = (Label)ri.FindControl("lblCn1Account");
Label lbhiddenID = (Label)ri.FindControl("lbhiddenID"); // Do Something
}

Repeater数据绑定和操作的更多相关文章

  1. Webform(七)——内置对象(Session、Application)和Repeater的Command操作

    内置对象:用于页面之间的数据交互 为什么要使用这么内置对象?因为HTTP的无状态性. 一.内置对象 (一)Session 跟Cookies一样用来存储用户数据 1.Session.Cookies对比 ...

  2. WebForm(Application,ViewState,Repeater的Command操作)

    一.AppliCation: 1.存储在服务器端,占用服务器内存 2.生命周期:永久 3.所有人都可访问的共有对象,一般用作服务器缓存 4.赋值:Application["key" ...

  3. Webform 内置对象2(Session、Application)、Repeater的Command操作

    内置对象: 1.Session:跟Cookies一样用来存储用户数据,但保存位置不同,保存在服务器内存上 每一台电脑访问服务器,都会是独立的一套session,key值都一样,但是内容都是不一样的 S ...

  4. Repeater的Command操作

    Repeater的Command操作 1.ItemCommand事件 :在Repeater中所有能触发事件的控件,都会来触发这一个事件 后台创建:在Page_Load中 Repeater1.ItemC ...

  5. Repeater数据绑定

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Index.aspx.cs& ...

  6. asp.net repeater控件操作

    Repeater控件和DataList控件,可以用来一次显示一组数据项.比如,可以用它们显示一个数据表中的所有行. Repeater控件完全由模板驱动,提供了最大的灵活性,可以任意设置它的输出格式. ...

  7. asp.net动态网站repeater控件使用及分页操作介绍

    asp.net动态网站repeater控件使用及分页操作介绍 1.简单介绍 Repeater 控件是一个容器控件,可用于从网页的任何可用数据中创建自定义列表.Repeater 控件没有自己内置的呈现功 ...

  8. C# WebForm内置对象2+Repeater的Command

    内置对象:用于页面之间的数据交互 为什么要使用这么内置对象?因为HTTP的无状态性. Session:在计算机中,尤其是在网络应用中,称为“会话控制”.Session 对象存储特定用户会话所需的属性及 ...

  9. Application、 session、iewstate,以及repeater 的commang用法

      Session:在不同的浏览器之间传值,像银行之类的网站为了安全把用户名密码保存在session里面.每一台电脑访问服务器,都会是独立的一套session,key值都一样,但是内容都是不一样的 以 ...

随机推荐

  1. Csharp 高级编程 C7.1.2

    第七章 代理(1) 一.代理要声明 二.代理使用步骤 声明代理 初始化代理(使用 实例的方法名 作为参数) 使用代理 代码示例: /*C7.1.2*/ using System; using Syst ...

  2. Java关键字transient和volatile

    transient标记的变量,在进行序列化的时候,这个字段不进行序列化操作. volatile标记的变量,在进行读写时,必须强制的与内存同步,即在读的时候需要从内存中读取,写的时候也需要回写到内存中. ...

  3. PDO事务管理DEMO

    try { $dsn = "mysql:host=127.0.0.1;port=3306;dbname=dab"; $pdo = new PDO($dsn, 'root', '') ...

  4. 关于extern对变量的使用

    extern 是声明全局的变量的意思. 例如在一个工程中有两个cpp,一个是test.cpp一个是main.cpp . 我们在test.cpp中定义了一个int num;但是我们在main.cpp中想 ...

  5. 常见的DoDataExchange什么意思

    该函数中的代码是由ClassWizard自动加入的.DoDataExchange只有一个参数,即一个CDataExchange对象的指针pDX.在该函数中调用了DDX函数来完成数据交换,调用DDV函数 ...

  6. Asp.net mvc 3 file uploads using the fileapi

    Asp.net mvc 3 file uploads using the fileapi I was recently given the task of adding upload progress ...

  7. php环境安装及搭建

    最近由于项目需要 转战 PHP .  在做了差不多两年java后 说实话看php代码还是有些难受的. 毕竟不习惯.废话不说 先说一下 PHP环境的部署等等,也就是最近几天学习的心得吧.方便以后参考. ...

  8. C++第二课(2013.9.27 )

    //引用的作用:代码简洁 //形参和实参同地址,实现的方式和指针的一样 //引用和指针没有本质的区别 //强转引用 float f = 3.14f; cout<< hex << ...

  9. CentOS终端操作mysql

    1.停用mysql服务:service mysqld stop 重启mysql服务:service mysql restart 2.mysql 1045ERROR:mysqld_safe --user ...

  10. 开心菜鸟学习系列学习笔记------------nodejs util公共函数

    global  在最外层定义的变量:    全局对象的属性:    隐式定义的变量(未定义直接赋值的变量).  一.process   process 是一个全局变量,即 global 对象的属性 ...