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

首頁 > 編程 > .NET > 正文

ASP.NET 嵌套Repeater

2024-07-10 13:06:54
字體:
來源:轉載
供稿:網友

本來不怎么用repeater的,可最近要顯示留言板一樣的東西,用grid實現不好看,grid比較適合正規(guī)的web系統,對于網站類型的,還是用repeater了,遇到需要嵌套repeater的情況,就收藏一下,以后可能還會用到哦。
原文來自ms:http://support.microsoft.com/default.aspx?scid=kb;en-us;306154

iew products that this article applies to.
article id : 306154
last review : july 15, 2004
revision : 4.1

this article was previously published under q306154
on this page
 summary
   bind to the parent table
   bind to the child table
   complete code list
     nestedrepeater.aspx
     nestedrepeater.aspx.cs
 references
 applies to

summary
this article describes how to use nested repeater controls to display hierarchical data. you can apply this concept to other list-bound controls.

 

 back to the top

bind to the parent table
1. start microsoft visual studio .net.
2. on the file menu, point to new, and then click project.
3. click visual c# projects under project types, and then click asp.net web application under templates.
4. in the location box, delete the webapplication#, and then type nestedrepeater. if you use the local server, leave the server name as http://localhost. the following path appears in the location box:
http://localhost/ nestedrepeater
click ok. 
5. in solution explorer, right-click the nestedrepeater project name node, point to add, and then click add web form.
6. to name the web form, type nestedrepeater, and click open.
7. the new web form is created. it opens in design view in the integrated development environment (ide) of microsoft visual studio .net. from the toolbox, select the repeater control, and then drag it to the web form page.
8. change the id property of this repeater control to parentrepeater.
9. switch to the html view for this web form. to do so, click the html tab in the lower-left corner of the designer. the repeater control generates the following html code:
<asp:repeater id="parentrepeater" runat="server"></asp:repeater>
     
 
10. add the following code in the repeater tags:
<itemtemplate>
     <b><%# databinder.eval(container.dataitem, "au_id") %></b><br>
</itemtemplate>
     
after you do that, the html code for the repeater is as follows:
<asp:repeater id="parentrepeater" runat="server">
 <itemtemplate>
      <b><%# databinder.eval(container.dataitem, "au_id") %></b><br>
      </itemtemplate>
</asp:repeater>
     
 
11. in solution explorer, right-click nestedrepeater.aspx, and then click view code to switch to the nestedrepeater.aspx.cs code-behind file.
12. add the following namespace declaration to the top of the file:
using system.data;
using system.data.sqlclient;
     
 
13. add the following code to the page_load event to create a connection to the pubs database, and then to bind the authors table to the repeater control:
      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; integrated security=sspi");
         sqldataadapter cmd1 = new sqldataadapter("select * from authors",cnn);

         //create and fill the dataset.
         dataset ds = new dataset();
         cmd1.fill(ds,"authors");
         //insert code in step 4 of the next section here.
         //bind the authors table to the parent repeater control, and call databind.
         parentrepeater.datasource = ds.tables["authors"];
         page.databind();

         //close the connection.
         cnn.close();
       }
     
note: you may have to modify the database connection string as appropriate for your environment.


14. save all of the files.
15. in solution explorer, right-click the nestedrepeater.aspx, and then click set as start page. 
16. on the build menu click build solution to compile the project.
17. view the .aspx page in the browser, and then verify that the page works thus far.

the output should appear as follows: • 172-32-1176
• 213-46-8915
• 238-95-7766
• 267-41-2394
• ...  
 


 back to the top

bind to the child table
1. in the html view of the nestedrepeater.aspx page, locate the following line of code:
<b><%# databinder.eval(container.dataitem, "au_id") %></b><br>
      
add the following code after this code:

<asp:repeater id="childrepeater" runat="server">
  <itemtemplate>
             <%# databinder.eval(container.dataitem, "[/"title_id/"]")%><br>
  </itemtemplate>
</asp:repeater>
      
this new code adds a second repeater control to the itemtemplate property of the parent repeater control. 
2. set the datasource property for the child repeater control as follows:
<asp:repeater ... datasource='<%# ((datarowview)container.dataitem)
      .row.getchildrows("myrelation") %>' >
     
after you set the datasource property for the child repeater control, the html code for the two repeater controls (parent and child) appears as follows:

<asp:repeater id="parentrepeater" runat="server">
 <itemtemplate>
  <b>
   <%# databinder.eval(container.dataitem, "au_id") %>
  </b>
  <br>
  <asp:repeater id="childrepeater" runat="server"
                    datasource='<%# ((datarowview)container.dataitem)
      .row.getchildrows("myrelation") %>' >
   <itemtemplate>
    <%# databinder.eval(container.dataitem, "[/"title_id/"]")%><br>
   </itemtemplate>
  </asp:repeater>
 </itemtemplate>
</asp:repeater>
     
 
3. add the following page directive to the top of the page:
<%@ import namespace="system.data" %>
     
 
4. in the code-behind page, replace the following line in the page_load event
//insert code in step 4 of the next section here.
      
with the following code:
         //create a second dataadapter for the titles table.
         sqldataadapter cmd2 = new sqldataadapter("select * from titleauthor",cnn);
         cmd2.fill(ds,"titles");

         //create the relation between the authors and titles tables.
         ds.relations.add("myrelation",
         ds.tables["authors"].columns["au_id"],
         ds.tables["titles"].columns["au_id"]);

     
this adds the titles table to the dataset, and then adds the relationships between the authors and titles tables.
 
5. save and compile the application.
6. view the page in the browser, and then verify that the page works so far. the output should appear as follows:
172-32-1176
ps3333
213-46-8915
bu1032
bu2075
238-95-7766
pc1035
267-41-2394
bu1111
tc7777
... 


 back to the top

complete code list
nestedrepeater.aspx
<%@ page language="c#" codebehind="nestedrepeater.aspx.cs" autoeventwireup="false" inherits="nestedrepeater.nestedrepeater" %>
<%@ import namespace="system.data" %>

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

<!-- start parent repeater -->
<asp:repeater id="parentrepeater" runat="server">
   <itemtemplate>
      <b><%# databinder.eval(container.dataitem,"au_id") %></b><br>

      <!-- start child repeater -->
      <asp:repeater id="childrepeater" 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 nestedrepeater
{
   public class nestedrepeater : system.web.ui.page
   {
      protected system.web.ui.webcontrols.repeater parentrepeater;
      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; integrated security=sspi ;");
         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.
         parentrepeater.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ā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 永和县| 临颍县| 汉川市| 定陶县| 张家口市| 丹寨县| 响水县| 石柱| 郓城县| 红安县| 郴州市| 沙湾县| 绥化市| 泗洪县| 五家渠市| 英德市| 静海县| 福鼎市| 色达县| 普兰县| 昭觉县| 颍上县| 富民县| 法库县| 六枝特区| 玛多县| 邹城市| 如东县| 芜湖市| 平湖市| 垣曲县| 理塘县| 宁波市| 凤山市| 专栏| 陆河县| 海安县| 岳阳县| 彭水| 太保市| 嵩明县|