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值都一样,但是内容都是不一样的 以 ...
随机推荐
- AngularJs练习Demo4
@{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewport&quo ...
- unison实时双向数据同步
软件下载 ocamlopt下载地址:http://caml.inria.fr Unison下载地址:http://www.seas.upenn.edu/~bcpierce/unison 1.安装uni ...
- zsh-替换掉黑白的控制台
官方地址:里面有详细的安装指南 http://ohmyz.sh/
- 16 Socket通信(简单例子)
服务端代码: import java.io.*; import java.net.ServerSocket; import java.net.Socket; import java.util.Da ...
- python学习视频整理
python3英文视频教程(全87集) http://pan.baidu.com/s/1dDnGBvV python从入门到精通视频(全60集)链接:http://pan.baidu.com/s/1e ...
- 转载:常见EXE文件反编译工具
PE Explorer V1.99 R5 绿色汉化特别版_强大的可视化汉化集成工具 功能极为强大的可视化汉化集成工具,可直接浏览.修改软件资源,包括菜单.对话框.字符串表等: 另外,还具备有 W32D ...
- Java开发工具与程序调试
开发工具:MyEclipse,Eclipse等. 程序调试: (1)断点:设置断点是程序调试中必不可少的手段,Java调试器每次遇到程序断点时都会将当前线程挂起,即暂停当前程序的运行.(在Eclip ...
- mysql下怎样运行脚本
假设要运行脚本: F:\hello world\niuzi.sql 第一种方法: 在命令行下(未连接数据库),输入 mysql -h localhost -u root -p < ...
- VS2010的openssl源码编译方法
http://download.csdn.net/download/soucula/9591308
- EXTJS 4.2 实现 gridpanel 鼠标悬停单元格以提示信息的方式显示单元格内容。
由于gridpanel的单元格里的文字太多时候,都由省略号代替,就想实现如题的功能,经过反复实验,终于搞定了!直接上代码: me.on('itemmouseenter', function (view ...