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. Windows命令行(DOS命令)教程-3(转载)http://arch.pconline.com.cn//pcedu/rookie/basic/10111/15325_2.html

    五.常用命令 DOS命令总共大约有一百个(包括文本编辑.查杀病毒.配置文件.批处理等),我们这里详细介绍二十个常用的DOS命令. 先介绍一下通配符的概念. 通配符*和? *表示一个字符串 ?只代表一个 ...

  2. oracle 11g 64位安装32位客户端和PL/SQL

    转自:http://www.360doc.com/content/14/0602/10/4903283_382949382.shtml   这个你需要安装一个32位的oracle客户端才能使用plsq ...

  3. oracle 数据库 if...elsif...语句

    CREATE OR REPLACE FUNCTION "UFN_GETIDS" (    OPEKIND   IN   VARCHAR2,-- 查询类型    PARAMS IN ...

  4. QT5-控件-QComboBox

    #ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QComboBox> cl ...

  5. c#中serialPort1_DataReceived串口接收事件处理

    1.缓冲区不定字节读取(波特率很高也没问题) //Thread.sleep(1000);//处理事件这块可以加上延时确保不定数的数据可以全部收到缓冲后,才去读缓冲内容--单位:毫秒 byte[] da ...

  6. chart.js 示例

    一个简单的例子. 1.html代码 <div id="pie" style="width: 250px;float:left"> <h3> ...

  7. Oracle 10g体系结构及安全管理

    (1)               Oracle数据库服务器:数据库,实例 (2)               数据库:(1)物理存储—数据文件,控制文件,日志文件,(2)逻辑存储—表空间,段,区间, ...

  8. AFNetworking 官方文档

    AFNetworking Version Minimum iOS Target Minimum OS X Target Notes 2.x iOS 6 OS X 10.8 Xcode 5 is req ...

  9. hdu 1599 find the mincost route

    http://acm.hdu.edu.cn/showproblem.php?pid=1599 floyd找最小环. #include <cstdio> #include <cstri ...

  10. 伪造队形(FFT)

    题目描述Tukkun带着他的合唱队去环形音乐厅参加演出.上场前,Tukkun发现了严重的问题:音乐厅的工作人员把他们的合唱队形搞错了.具体来说,Tukkun的合唱队有N个人围成一圈,身高按照顺时针顺序 ...