使用者控制項和伺服器控制項的資料繫結

來源:互聯網
上載者:User

一、綁定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 %>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.