用户控件与Repeate的数据绑定_第1页
用户控件与Repeate的数据绑定_第2页
用户控件与Repeate的数据绑定_第3页
用户控件与Repeate的数据绑定_第4页
免费预览已结束,剩余1页可下载查看

付费下载

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、一、绑定 Repeater 控件的数据源aspx.cs 文件中绑定 Repeater 控件的数据源在 BindDataSource() 中: protected override void BindDataSource()this.rpID.DataSource = this.dataList; this.rpID.DataBind();Repeater控件事件OnltemDataBound,表示在循环加载列表时 候,会对每一项 Item 进行具体的操作。例子:Protected void rp_ltemDataBound(object sender,RepeaterltemEventArgs

2、 e)if (e.ltem.ltemType = ListltemType.Alternatingltem | e.ltem.ltemType = ListltemType.ltem)List lnfos = this.rplD.DataSource as List;lnfo info = e.ltem.Dataltem as lnfo;Literal lt = e.ltem.FindControl(lt) as Literal; lt.Text = lnfo.Title;可以看出来对于 Repeater 控件中的 里面的用户控件、服务器控 件的赋值是在Repeater控件事件OnItemDa

3、taBound中进行的。二、用户控件 用户控件 T.ascx 代码有: 字段:private string title;属性:public string Titlegetreturn this.title;方法:SetTitle()this.title = Hello world!; 其实这里的字段赋值也是可以用属性进行赋值的, 不过要在属性声明中加上 set 部分。那么这个 T 的用户控件的要是在 Repeater 控件中的 里面出现的 话,是要在Repeater控件事件OnltemDataBound里面进行具体的赋值的。 例子:Protected void rp_ltemDataBound

4、(object sender,RepeaterltemEventArgs e)if (e.ltem.ltemType = ListltemType.Alternatingltem | e.ltem.ltemType = ListltemType.ltem)List lnfos = this.rplD.DataSource asList;lnfo info = e.ltem.Dataltem as lnfo;T t = e.ltem.FindControl(t) as T; t.SetTitle(info.Title); 可以看到上面的操作已经把数据存到 title 字段里面了,在用户空间的 T

5、.ascx 代码中用来访问Title 属性, 因为之前已经声明了 Title 属性的 get 部分。三、一个简单的 Reapter 控件的使用全貌 在 aspx 页面中: 在 aspx.cs 页面中: protected override void BindDataSource()this.rplD.DataSource = this.dataList; this.rplD.DataBind();Protected void rp_ltemDataBound(object sender,RepeaterltemEventArgs e)if (e.ltem.ltemType = Listltem

6、Type.Alternatingltem | e.ltem.ltemType = ListltemType.ltem)List Infos = this.rpID.DataSource asList;Info info = e.Item.DataItem as Info;Literal lt = e.Item.FindControl(lt) as Literal;lt.Text = Info.Title; 注意我前面已经提到了服务器控件和用户控件在这里面的使用方式其实是一摸 一样的。可以看出来 BindDataSource() 是对 aspx 页面中最外层的用户控件、 服务器控件 的数据源进行

7、绑定,而 OnItemDataBound 事件是对 Repeater 控件中的 里面的用户控 件、服务器控件的数据源 进行绑定。四、在 Repeater 控件中嵌套使用 Repeater 控件 这其实是一个特例: 在 aspx 页面中: 在 aspx.cs 页面中: protected override void BindDataSource() this.rpID.DataSource = this.dataList;this.rpID.DataBind();Protected void rp_ItemDataBound(object sender,RepeaterItemEventArgs

8、 e)if (e.Item.ItemType = ListItemType.AlternatingItem | e.Item.ItemType = ListItemType.Item)List Infos = this.rpID.DataSource asList;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 页面

9、中:在 aspx.cs 页面中: protected override void BindDataSource() this.UCT.ItemList = dataList; this.UCT.Title = hello world!; 是在给用户控件的属性赋值。 用户控件隐藏文件 T.ascx.cs 代码有: 字段:private List itemList;private string title;属性:public string Titlesetthis.title = value;public List ItemListsetthis.itemList = value; 这里面就是用户控件定义了属性对字段进行赋值的。 如果用户控件中还有用户控件的赋值情况: 用户控件隐藏文件 T.ascx.cs 代码有: protected override void BindDataSource()T_T.SelectList = dataList;T_T.Title = hello world!;这里和 aspx 页面一样是在 BindDataSource() 中对外层的用户控件进行赋值的。 当然 T_T 中会声明相应的字段和属性保存数据。最后会在自己的 BindDataSource() 里面再

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论