用户控件和服务器控件的数据绑定

一、绑定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 ? "&nbsp;" : Eval("Detail")%>
<%#(Container.DataItem as BannerInfo).BannerTitle %>
在aspx页面中Repeater控件中Repeater控件的<ItemTemplate>里面绑定数据:
<%#Container.DataItem == null || Container.DataItem.ToString()==string.Empty ? "&nbsp;" : Container.DataItem%>
在aspx页面直接赋值和在用户控件中一样:
<%= Title %>
原文地址:https://www.cnblogs.com/lmfeng/p/2196313.html