用户控件和服务器控件的数据绑定
一、绑定Repeater控件的数据源
aspx.cs文件中绑定Repeater控件的数据源在BindDataSource()中:
protected override void BindDataSource(){ this.rpID.DataSource = this.dataList; this.rpID.DataBind();}
Repeater控件事件OnItemDataBound,表示在循环加载<ItemTemplate>列表时候,会对每一项Item进行具体的操作。例子:
Protected void rp_ItemDataBound(object sender,RepeaterItemEventArgs e) { if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item) { List<Info> Infos = this.rpID.DataSource as List<Info>; Info info = e.Item.DataItem as Info;
Literal lt = e.Item.FindControl("lt") as Literal;
lt.Text = Info.Title; } }
可以看出来对于Repeater控件中的<ItemTemplate>里面的用户控件、服务器控件的赋值是在Repeater控件事件OnItemDataBound中进行的。
二、用户控件
用户控件T.ascx代码有:
字段:private string title;
属性:public string Title{ get { return this.title; }}
方法:SetTitle(){ this.title = "Hello world!";}其实这里的字段赋值也是可以用属性进行赋值的,不过要在属性声明中加上set部分。那么这个T的用户控件的要是在Repeater控件中的<ItemTemplate>里面出现的话,是要在Repeater控件事件OnItemDataBound里面进行具体的赋值的。例子: Protected void rp_ItemDataBound(object sender,RepeaterItemEventArgs e) { if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item) { List<Info> Infos = this.rpID.DataSource as List<Info>; Info info = e.Item.DataItem as Info;
T t = e.Item.FindControl("t") as T;
t.SetTitle(info.Title); } }可以看到上面的操作已经把数据存到title字段里面了,在用户空间的T.ascx代码中用<%=Title%>来访问Title属性,因为之前已经声明了Title属性的get部分。
三、一个简单的Reapter控件的使用全貌
在aspx页面中:
<asp:Repeater ID="rpID" runat="server" OnItemDataBound="rp_ItemDataBound"> <ItemTemplate> <asp:Literal ID="lt" runat="server"></asp:Literal> </ItemTemplate></asp:Repeater>
在aspx.cs页面中:
protected override void BindDataSource(){ this.rpID.DataSource = this.dataList; this.rpID.DataBind();}
Protected void rp_ItemDataBound(object sender,RepeaterItemEventArgs e) { if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item) { List<Info> Infos = this.rpID.DataSource as List<Info>; Info info = e.Item.DataItem as Info;
Literal lt = e.Item.FindControl("lt") as Literal;
lt.Text = Info.Title; } }注意我前面已经提到了服务器控件和用户控件在这里面的使用方式其实是一摸一样的。可以看出来BindDataSource()是对aspx页面中最外层的用户控件、服务器控件的数据源进行绑定,而OnItemDataBound事件是对Repeater控件中的<ItemTemplate>里面的用户控件、服务器控件的数据源进行绑定。
四、在Repeater控件中嵌套使用Repeater控件
这其实是一个特例:在aspx页面中:
<asp:Repeater ID="rpID" runat="server" OnItemDataBound="rp_ItemDataBound"> <ItemTemplate> <asp:Repeater ID="rp_rpID" runat="server"> <asp:Literal ID="lt" runat="server"></asp:Literal> </asp:Repeater> </ItemTemplate></asp:Repeater>
在aspx.cs页面中:
protected override void BindDataSource(){ this.rpID.DataSource = this.dataList; this.rpID.DataBind();}
Protected void rp_ItemDataBound(object sender,RepeaterItemEventArgs e) { if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item) { List<Info> Infos = this.rpID.DataSource as List<Info>; Info info = e.Item.DataItem as Info;
Repeater rp_rpID = e.Item.FindControl("rp_rpID") as Repeater;
rp_rpID.DataSource = dataList;
rp_rpID.DataBind(); } }
五、一般用户控件的赋值情况
在aspx页面中:
<UCCommon:T ID="UCT" runat="server" />
在aspx.cs页面中:
protected override void BindDataSource(){ this.UCT.ItemList = dataList; this.UCT.Title = "hello world!";}是在给用户控件的属性赋值。
用户控件隐藏文件T.ascx.cs代码有:
字段:
private List<Info> itemList;private string title;
属性:
public string Title{ set { this.title = value; }}public List<Info> ItemList{ set { this.itemList = value; }}
这里面就是用户控件定义了属性对字段进行赋值的。
如果用户控件中还有用户控件的赋值情况:
用户控件隐藏文件T.ascx.cs代码有:
protected override void BindDataSource(){ T_T.SelectList = dataList; T_T.Title = "hello world!";}这里和aspx页面一样是在BindDataSource()中对外层的用户控件进行赋值的。
当然T_T中会声明相应的字段和属性保存数据。最后会在自己的BindDataSource()里面再次赋值给需要的控件,或者直接在页面上显示所得的数据。
可以在用户控件的T.ascx代码中直接获取属性例如:
<%= Title %>
在aspx页面中Repeater控件中的<ItemTemplate>里面绑定数据:
<%#Eval("Detail") == null || Eval("Detail").ToString() == string.Empty ? " " : Eval("Detail")%>
<%#(Container.DataItem as BannerInfo).BannerTitle %>
在aspx页面中Repeater控件中Repeater控件的<ItemTemplate>里面绑定数据:
<%#Container.DataItem == null || Container.DataItem.ToString()==string.Empty ? " " : Container.DataItem%>
在aspx页面直接赋值和在用户控件中一样:
<%= Title %>
转载于:https://www.cnblogs.com/splendidme/archive/2011/10/05/2199497.html
用户控件和服务器控件的数据绑定相关推荐
- 开发和使用Web用户控件
在 ASP.NET 的开发中 Web 用户控件的开发和使用是一项必不可少的技术,在对这项技术的一番研究后写下了这篇随笔,不过确实担心这么初级的东东放到原创首页上会被拍砖头. 1.简介 2.创建 Web ...
- Web页面获取用户控件页面中服务器控件的值
用户控件页面后台: public string P_Name { get { return txt_P_name.Value; } set { txt_P_name.Value = value; } ...
- 用户控件和自定义控件
关 键 词 Server Control 服务器控件 User Control 用户控件,ASP.NET服务器控件的一种(一般后缀名为.ASCX文件) Custom Control 自定义控件,ASP ...
- 第6章 自定义控件和用户控件
部署 创建 内容和布局 设计期行为 性能 ASP.NET 为创建自己的控件提供了两个模型--用户控件模型和自定义控件模型.这两个模型适合不同的情况.一般而言用户控件适合创建内部,应用程序特定的控件和相 ...
- 关于从页面中获取用户控件中的其它控件(如DropDownList)事件的方法
在项目中经常把一些经常使用的代码做成用户控件以提高代码的可重用性, 一个经常遇到的就是在页面中调用用户控件中的服务器控件的事件,下面给出简单的代码示列. 我们在一个用户控件(MaterialRepor ...
- 教你如何 构建基本的用户控件
创建一个简单用户控件是开始自定义控件的好方法.本章主要介绍创建一个基本的颜色拾取器.接下来分析如何将这个控件分解成功能更强大的基于模板的控件. 创建基本的颜色拾取器很容易.然而,创建自定义颜色拾取器仍 ...
- ASP.NET用户控件的创建和使用
ASP.NET用户控件的创建和使用 1. Web用户控件概述 Web用户控件是一种服务器控件,它与ASP.NET页面有着同样的所见即所得的特点,并以.ascx为扩展名存储为文本文件,同样具有自己的界面 ...
- ASP.NET中实现复用代码自定义用户控件UserControl的使用
场景 ASP.NET中新建Web网站并部署到IIS上(详细图文教程): https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/107199 ...
- ASP.NET 用户控件自定义属性、方法、事件
原文件链接:http://www.cnblogs.com/stalwart/archive/2010/12/16/1908302.html 之前一直写MVC的,本来就不大喜欢.NET中的服务器控件,里 ...
最新文章
- Spring越来越强,而我们越来越快餐!离开了Spring,居然API都写不出来了! 程序猿DD...
- xtrabackup之innobackupex命令参数
- python基础入门视频-Python基础入门视频课程——笔记
- 比较常用的几个正则表达式
- 20应用统计考研复试要点(part9)--应用多元分析
- [SCOI2015]情报传递(主席树+lca)
- python的render函数_带函数return的Flask render_模板
- jq 和java 多张图片_jQuery多个事件触发相同的功能
- 《数字电子技术基础》5.1 触发器概述、5.2 SR锁存器
- win7右键没有新建文本文档怎么办
- iOS开发系列--NextPrevious On-Demand Resource Basics
- 计算机技能大赛总结汇报大学,计算机专业技能大赛总结
- Flex + BlazeDS 学习笔记 (一) --- BlazeDS的功能原理及配置实例
- Enterprise Architect安装使用
- 空气开关A型、B型、C型、D型的区别
- python 视频硬字幕提取 内嵌字幕提取工具
- mac BigSur修改系统文件方法
- 计算机如何修改任务管理器,win7如何更改任务管理器快捷键_win7更改任务管理器快捷键的教程...
- 计算机怎么取消用户密码,怎么取消开机密码:计算机开机进入Windows时的开机密码怎么取消?...
- SECS/GEM300mm通讯协议概念