博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
用户控件和服务器控件的数据绑定
阅读量:7041 次
发布时间:2019-06-28

本文共 4270 字,大约阅读时间需要 14 分钟。

一、绑定Repeater控件的数据源

aspx.cs文件中绑定Repeater控件的数据源在BindDataSource()中:
protected override void BindDataSource() {
   this.rpID.DataSource = this.dataList;    this.rpID.DataBind(); }
Repeater控件事件OnItemDataBound,表示在循环加载
列表时候,会对每一项Item进行具体的操作。 例子:
  Protected void rp_ItemDataBound(object sender,RepeaterItemEventArgs e)   {
      if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)       {
          List
Infos = this.rpID.DataSource as List
;                     Info info = e.Item.DataItem as Info;
          Literal lt = e.Item.FindControl("lt") as Literal;
          lt.Text = Info.Title;       }   }
可以看出来对于Repeater控件中的
里面的用户控件、服务器控件的赋值是在Repeater控件事件OnItemDataBound中进行的。
二、用户控件
用户控件T.ascx代码有:
字段: private string title;
属性: public string Title {
    get     {        return this.title;     } }
方法: SetTitle() {
    this.title = "Hello world!"; } 其实这里的字段赋值也是可以用属性进行赋值的,不过要在属性声明中加上set部分。 那么这个T的用户控件的要是在Repeater控件中的
里面出现的话,是要在 Repeater控件事件OnItemDataBound里面进行具体的赋值的。 例子:   Protected void rp_ItemDataBound(object sender,RepeaterItemEventArgs e)   {
      if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)       {
          List
Infos = this.rpID.DataSource as List
;                     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页面中:
   
       
   
在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
Infos = this.rpID.DataSource as List
;                     Info info = e.Item.DataItem as Info;
          Literal lt = e.Item.FindControl("lt") as Literal;
          lt.Text = Info.Title;       }   } 注意我前面已经提到了服务器控件和用户控件在这里面的使用方式其实是一摸一样的。 可以看出来BindDataSource()是对aspx页面中最外层的用户控件、服务器控件的数据源进行绑定, 而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 e)   {
      if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)       {
          List
Infos = this.rpID.DataSource as List
;                     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页面中:
在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 Title { set { this.title = value; } } public List
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控件中的
里面绑定数据:
<%#Eval("Detail") == null || Eval("Detail").ToString() == string.Empty ? " " : Eval("Detail")%>
<%#(Container.DataItem as BannerInfo).BannerTitle %>
在aspx页面中Repeater控件中Repeater控件的
里面绑定数据:
<%#Container.DataItem == null || Container.DataItem.ToString()==string.Empty ? " " : Container.DataItem%>
在aspx页面直接赋值和在用户控件中一样:
<%= Title %>
 

转载地址:http://kfxal.baihongyu.com/

你可能感兴趣的文章
Linux常用快捷键
查看>>
无损音乐资源
查看>>
Foxmail配置IMAP账号
查看>>
linux下查看一个进程的启动时间和运行时间
查看>>
网页常用js代码汇集
查看>>
【HM】第9课:Cookie与HttpSession详解
查看>>
NEC面部识别系统助力台北世界大学生运动会
查看>>
nfs
查看>>
UltraEdit实现“删除包含某个关键字的所有行”
查看>>
WSFC 维护模式操作粒度控制
查看>>
linux kill 命令
查看>>
为什么使用useLegacyV2RuntimeActivationPolicy?
查看>>
Shell工作笔记01
查看>>
windows 2008 R2搭建简单WEB服务器
查看>>
hyper-v故障转移群集之4、创建群集
查看>>
webpack命令行
查看>>
多网卡的7种bond模式原理
查看>>
用update和replace在sql中替换某一个字段的部分内容
查看>>
Web框架原理
查看>>
HEX解码
查看>>