国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > 綜合 > 正文

使用嵌套的Repeater控件

2024-07-21 02:24:16
字體:
供稿:網(wǎng)友


這個程序適用于:
•microsoft asp.net
•microsoft vs.net 正式版



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



綁定到父表



1.添加一個新的web form 到應(yīng)用程序項(xiàng)目中,名稱為nestedrepeater.aspx.
2.從工具箱托動一個repeater 控件到這個頁面上, 設(shè)定其id 屬性為 parent .
3.切換到html 視圖.
4.選中下列<itemtemplate> 代碼,復(fù)制到repeater 控件對應(yīng)的位置。注意,粘貼的時候請使用“粘貼為html”功能。這些語句包含了數(shù)據(jù)綁定語法,很簡單。
<itemtemplate>
<b><%# databinder.eval(container.dataitem, "au_id") %></b><br>
</itemtemplate>
5.打開nestedrepeater.aspx.cs 這個代碼分離文件。降下列代碼添加到page_load 事件中,其作用是建立一個到 pubs (這個數(shù)據(jù)庫是sql server的演示數(shù)據(jù)庫。另外在安裝.net framework sdk的時候也會安裝這個數(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.在瀏覽器中打開這個頁面,輸出結(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.在頁面頂部添加下列指令(請注意,是在.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)用程序.
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);
      }
   }
}


發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 五原县| 彰武县| 扬州市| 航空| 黄梅县| 康平县| 延安市| 郓城县| 界首市| 襄汾县| 石狮市| 惠安县| 牡丹江市| 马龙县| 寿阳县| 孝义市| 德昌县| 万安县| 青岛市| 昌江| 东乌珠穆沁旗| 邓州市| 岳普湖县| 剑河县| 辽宁省| 桓台县| 蕉岭县| 旬阳县| 农安县| 淮安市| 通州区| 哈巴河县| 黄冈市| 周宁县| 漯河市| 吴忠市| 东兴市| 图片| 黑龙江省| 酒泉市| 大同市|