Repeater数据绑定和操作
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数据绑定和操作的更多相关文章
- Webform(七)——内置对象(Session、Application)和Repeater的Command操作
内置对象:用于页面之间的数据交互 为什么要使用这么内置对象?因为HTTP的无状态性. 一.内置对象 (一)Session 跟Cookies一样用来存储用户数据 1.Session.Cookies对比 ...
- WebForm(Application,ViewState,Repeater的Command操作)
一.AppliCation: 1.存储在服务器端,占用服务器内存 2.生命周期:永久 3.所有人都可访问的共有对象,一般用作服务器缓存 4.赋值:Application["key" ...
- Webform 内置对象2(Session、Application)、Repeater的Command操作
内置对象: 1.Session:跟Cookies一样用来存储用户数据,但保存位置不同,保存在服务器内存上 每一台电脑访问服务器,都会是独立的一套session,key值都一样,但是内容都是不一样的 S ...
- Repeater的Command操作
Repeater的Command操作 1.ItemCommand事件 :在Repeater中所有能触发事件的控件,都会来触发这一个事件 后台创建:在Page_Load中 Repeater1.ItemC ...
- Repeater数据绑定
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Index.aspx.cs& ...
- asp.net repeater控件操作
Repeater控件和DataList控件,可以用来一次显示一组数据项.比如,可以用它们显示一个数据表中的所有行. Repeater控件完全由模板驱动,提供了最大的灵活性,可以任意设置它的输出格式. ...
- asp.net动态网站repeater控件使用及分页操作介绍
asp.net动态网站repeater控件使用及分页操作介绍 1.简单介绍 Repeater 控件是一个容器控件,可用于从网页的任何可用数据中创建自定义列表.Repeater 控件没有自己内置的呈现功 ...
- C# WebForm内置对象2+Repeater的Command
内置对象:用于页面之间的数据交互 为什么要使用这么内置对象?因为HTTP的无状态性. Session:在计算机中,尤其是在网络应用中,称为“会话控制”.Session 对象存储特定用户会话所需的属性及 ...
- Application、 session、iewstate,以及repeater 的commang用法
Session:在不同的浏览器之间传值,像银行之类的网站为了安全把用户名密码保存在session里面.每一台电脑访问服务器,都会是独立的一套session,key值都一样,但是内容都是不一样的 以 ...
随机推荐
- Activity-在ListFragment中为ListView增加空白视图
有两种方法可以实现为ListView添加空白视图.但是原理都一样: 第一种方法是XML+代码添加: 1.定义emptyView视图 2.调用AdapterView的setEmptyView(empt ...
- Qt窗口句柄
关键字: 透明效果,异形,子窗口,控件,浮窗,同级句柄
- POJ2446 二分图最大匹配
问题:POJ2446 分析: 采用黑白相间的方法把棋盘分成两个点集,是否可以用1*2的卡片实现全覆盖等价于二分图是否有完全匹配. AC代码 //Memory: 172K Time: 32MS #inc ...
- javascript中遍历EL表达式List集合中的值
http://www.cnblogs.com/limeiky/p/6002900.html
- 安装 adobe flash player
安装方法: 1. 下载Adobe Flash Player: http://fpdownload.macromedia.com/get/flashplayer/pdc/11.2. ...
- CoreData (三)备
NSFetchedResultsController 什么是NSFetchedResultsController NSFetchedResultsController是一个让人爱恨交加的一个类.如果使 ...
- LeetCode_Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
- python中的继承原则
继承是面向对象的重要特征之一,继承是两个类或者多个类之间的父子关系,子进程继承了父进程的所有公有实例变量和方法.继承实现了代码的重用.重用已经存在的数据和行为,减少代码的重新编写,python在类名 ...
- Buffer lock
buffer lock Oracle 提供非常精确,有效的Row Level Lock机制,多个用户同时修改数据时,为了保护数据. 以块为单位挂起锁的情况不会发生,但这不太正确. 以块为单位的锁 ...
- 【转】树莓派学习笔记——I2C Tools 学习笔记
原文网址:http://blog.csdn.net/xukai871105/article/details/15029843 1.安装 I2C驱动载入和速率修改请查看博文[树莓派学习笔记——I ...