小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

使用嵌套的Repeater控件顯示分級(jí)數(shù)據(jù)

 滅星之破 2012-02-10

  簡(jiǎn)介

  本文描述如何使用嵌套的Repeater 控件來顯示分級(jí)數(shù)據(jù) 。當(dāng)然了,你也可以將這一技術(shù)應(yīng)用到其他的列表綁定控件上去,比如DataGrid包含DataGrid,DataList包含DataList等等的組合。

  綁定到父表

  1.添加一個(gè)新的Web Form 到應(yīng)用程序項(xiàng)目中,名稱為Nestedrepeater.aspx.
  2.從工具箱托動(dòng)一個(gè)Repeater 控件到這個(gè)頁面上, 設(shè)定其ID 屬性為 parent .
  3.切換到HTML 視圖.
  4.選中下列<itemtemplate> 代碼,復(fù)制到Repeater 控件對(duì)應(yīng)的位置。注意,粘貼的時(shí)候請(qǐng)使用“粘貼為html”功能。這些語句包含了數(shù)據(jù)綁定語法,很簡(jiǎn)單。

<itemtemplate>
<b><%# DataBinder.Eval(Container.DataItem, "au_id") %></b><br>
</itemtemplate>

  5.打開Nestedrepeater.aspx.cs 這個(gè)代碼分離文件。降下列代碼添加到Page_Load 事件中,其作用是建立一個(gè)到 Pubs (這個(gè)數(shù)據(jù)庫是sql server的演示數(shù)據(jù)庫。另外在安裝.net framework sdk的時(shí)候也會(huì)安裝這個(gè)數(shù)據(jù)庫)數(shù)據(jù)庫的連接,并綁定Authors 表到Repeater 控件

public void Page_Load()
{
 SqlConnection cnn = new SqlConnection("server=(local);database=pubs;uid=sa;pwd=;");
 SqlDataAdapter cmd1 = new SqlDataAdapter("select * from authors",cnn);
  DataSet ds = new DataSet();
  cmd1.Fill(ds,"authors");

  //這里將要插入子表的數(shù)據(jù)綁定

  parent.DataSource = ds.Tables["authors"];
  Page.DataBind();
 cnn.Close();
}

  6.在文件的頭部添加下面的名稱空間
  using System.Data.SqlClient;
  7.根據(jù)你自己的情況修改一下連接字符串
  8.保存并編譯應(yīng)用程序
  9.在瀏覽器中打開這個(gè)頁面,輸出結(jié)果類似于下面的格式

172-32-1176
213-46-8915
238-95-7766
267-41-2394
...

  綁定到子表

  1.在頁面的HTML視圖中,添加下列代碼。其目的是增加子Repeater 控件到父Repeater的項(xiàng)目模板中,形成嵌套。

<asp:repeater id="child" runat="server">
<itemtemplate>
<%# DataBinder.Eval(Container.DataItem, "[/"title_id/"]") %><br>
</itemtemplate>
</asp:repeater>

  2.設(shè)置子Repeater 控件的DataSource 屬性:

<asp:repeater ... datasource='<%# ((DataRowView)Container.DataItem)
.Row.GetChildRows("myrelation") %>'>

  3.在頁面頂部添加下列指令(請(qǐng)注意,是在.aspx文件中):

  <%@ Import Namespace="System.Data" %>

  在.cs文件中,將Page_Load中的注釋部分(//這里將要插入子表的數(shù)據(jù)綁定)替換成下列代碼:

SqlDataAdapter cmd2 = new SqlDataAdapter("select * from titleauthor",cnn);
cmd2.Fill(ds,"titles");
ds.Relations.Add("myrelation",
ds.Tables["authors"].Columns["au_id"],
ds.Tables["titles"].Columns["au_id"]);

  4.保存并編譯應(yīng)用程序。
  .在瀏覽器中察看修改后的頁面。顯示格式類似于下面的格式:

172-32-1176
PS3333
213-46-8915
BU1032
BU2075
238-95-7766
PC1035
267-41-2394
BU1111
TC7777
...

完整的代碼

Nestedrepeater.aspx
<%@ Page Language=C# Inherits="yourprojectname.nestedrepeater" %>
<%@ Import Namespace="System.Data" %>

<html>
<body>
<form runat=server>

<!-- start parent repeater -->
<asp:repeater id="parent" runat="server">
  <itemtemplate>
  ?。糱><%# DataBinder.Eval(Container.DataItem,"au_id") %></b><br>

   <!-- start child repeater -->
  ?。糰sp:repeater id="child" datasource='<%# ((DataRowView)Container.DataItem)
.Row.GetChildRows("myrelation") %>' runat="server">
   ?。糹temtemplate>
     ?。?# DataBinder.Eval(Container.DataItem, "[/"title_id/"]")%><br>
     </itemtemplate>
  ?。?asp:repeater>
  ?。?-- end child repeater -->

?。?itemtemplate>
</asp:repeater>
<!-- end parent repeater -->

</form>
</body>
</html>
Nestedrepeater.aspx.cs
using System;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace yourprojectname
{
 public class nestedrepeater : System.Web.UI.Page
  {
   protected System.Web.UI.WebControls.Repeater parent;
   public nestedrepeater()
   {
     Page.Init += new System.EventHandler(Page_Init);
   }
   public void Page_Load(object sender, EventArgs e)
   {
     //Create the connection and DataAdapter for the Authors table.
     SqlConnection cnn = new SqlConnection("server=(local);database=pubs;uid=sa;pwd=;");
     SqlDataAdapter cmd1 = new SqlDataAdapter("select * from authors",cnn);

     //Create and fill the DataSet.
     DataSet ds = new DataSet();
     cmd1.Fill(ds,"authors");

     //Create a second DataAdapter for the Titles table.
     SqlDataAdapter cmd2 = new SqlDataAdapter("select * from titleauthor",cnn);
     cmd2.Fill(ds,"titles");

     //Create the relation bewtween the Authors and Titles tables.
     ds.Relations.Add("myrelation",
     ds.Tables["authors"].Columns["au_id"],
     ds.Tables["titles"].Columns["au_id"]);

     //Bind the Authors table to the parent Repeater control, and call DataBind.
     parent.DataSource = ds.Tables["authors"];
     Page.DataBind();

     //Close the connection.
     cnn.Close();
   }
   private void Page_Init(object sender, EventArgs e)
   {
     InitializeComponent();
   }
   private void InitializeComponent()
   {
    this.Load += new System.EventHandler(this.Page_Load);
   }
  }
}

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多