六月婷婷综合激情-六月婷婷综合-六月婷婷在线观看-六月婷婷在线-亚洲黄色在线网站-亚洲黄色在线观看网站

明輝手游網中心:是一個免費提供流行視頻軟件教程、在線學習分享的學習平臺!

使用嵌套的Repeater控件

[摘要]這個程序適用于:Microsoft ASP.NET Microsoft VS.NET 正式版 簡介本文描述如何使用嵌套的Repeater 控件來顯示分級數據 。當然了,你也可以將這一技術應用到其他的列表綁定控件上去,比如DataGrid包含DataGrid,DataList包含DataList等等...

這個程序適用于:
•Microsoft ASP.NET 
•Microsoft VS.NET 正式版 



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



綁定到父表 



1.添加一個新的Web Form 到應用程序項目中,名稱為Nestedrepeater.aspx. 
2.從工具箱托動一個Repeater 控件到這個頁面上, 設定其ID 屬性為 parent . 
3.切換到HTML 視圖. 
4.選中下列<itemtemplate> 代碼,復制到Repeater 控件對應的位置。注意,粘貼的時候請使用“粘貼為html”功能。這些語句包含了數據綁定語法,很簡單。 
<itemtemplate>
<b><%# DataBinder.Eval(Container.DataItem, "au_id") %></b><br>
</itemtemplate>
5.打開Nestedrepeater.aspx.cs 這個代碼分離文件。降下列代碼添加到Page_Load 事件中,其作用是建立一個到 Pubs (這個數據庫是sql server的演示數據庫。另外在安裝.net framework sdk的時候也會安裝這個數據庫)數據庫的連接,并綁定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");
   //這里將要插入子表的數據綁定
   parent.DataSource = ds.Tables["authors"];
   Page.DataBind();
   cnn.Close();
}
6.在文件的頭部添加下面的名稱空間 
using System.Data.SqlClient;
7.根據你自己的情況修改一下連接字符串 
8.保存并編譯應用程序 
9.在瀏覽器中打開這個頁面,輸出結果類似于下面的格式 
172-32-1176 
213-46-8915 
238-95-7766 
267-41-2394 
... 



綁定到子表 



1.在頁面的HTML視圖中,添加下列代碼。其目的是增加子Repeater 控件到父Repeater的項目模板中,形成嵌套。 
<asp:repeater id="child" runat="server">
   <itemtemplate>
   <%# DataBinder.Eval(Container.DataItem, "[\"title_id\"]") %><br>
   </itemtemplate>
</asp:repeater>
2.設置子Repeater 控件的DataSource 屬性: 
<asp:repeater ... datasource="<%# ((DataRowView)Container.DataItem)
.Row.GetChildRows("myrelation") %>">
3.在頁面頂部添加下列指令(請注意,是在.aspx文件中): 
<%@ Import Namespace="System.Data" %>
在.cs文件中,將Page_Load中的注釋部分(//這里將要插入子表的數據綁定)替換成下列代碼: 
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.保存并編譯應用程序. 
5.在瀏覽器中察看修改后的頁面。顯示格式類似于下面的格式: 
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>
      <b><%# DataBinder.Eval(Container.DataItem,"au_id") %></b><br>



      <!-- start child repeater -->
      <asp:repeater id="child" datasource="<%# ((DataRowView)Container.DataItem)
      .Row.GetChildRows("myrelation") %>" runat="server">
         <itemtemplate>
            <%# 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);
      }
   }
}



參考
更詳細的信息,參考Microsoft .NET Framework SDK中下列文章: 
在表之間增加關系
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguidnf/html/cpconaddingrelationshipbetweentwotables.asp 
在表之間導航關系
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguidnf/html/cpconnavigatingrelationshipbetweentwotables.asp 
Repeater Web 服務器控件
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgenref/html/cpconrepeaterwebcontrol.asp 



主站蜘蛛池模板: 五月婷婷操 | 亚洲天堂男人 | 日本成人xxx | 色在线网 | 一二三四手机在线观看视频播放 | 色偷偷狠狠色综合网 | 四虎免费最新在线永久 | 色视频综合 | 三级黄色片在线播放 | 青青视频在线播放 | 亚洲成a人在线观看 | 欧美洲大黑香蕉在线视频 | 欧美怡红院高清在线 | 三级国产精品一区二区 | 我要色综合网 | 视频在线观看40分钟免费 | 日韩毛片一级 | 中文字幕在线精品视频入口一区 | 亚洲国产日韩综合久久精品 | 亚洲网站黄色 | 日韩三级视频在线 | 中文字幕日韩在线一区国内 | 亚洲婷婷综合中文字幕第一页 | 五月综合久久 | 综合久久综合久久 | 欧美怡红院视频一区二区三区 | 青青国产视频 | 天天干在线观看 | 日本成人一区二区三区 | 日韩欧美网址 | 婷婷激情丁香 | 日日视频 | 午夜影皖| 速度与激情9全集免费观看 速度与激情9免费完整版高清 | 欧美婷婷| 中文字幕不卡在线高清 | 日韩中文字幕免费版 | 中文字幕 一区 婷婷 在线 | 日美一级毛片 | 日本免费观看网站 | 日韩一级片免费看 |