您的位置:首页 > 编程语言 > ASP

ASP.NET嵌套Repeater

2009-08-14 09:57 232 查看

本来不怎么用Repeater的,可最近要显示留言板一样的东西,用Grid实现不好看,Grid比较适合正规的WEB系统,对于网站类型的,还是用Repeater了,遇到需要嵌套Repeater的情况,就收藏一下,以后可能还会用到哦。
Article ID:306154
Last Review:July 15, 2004
Revision:4.1
On This Page

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/ NestedRepeaterClick 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.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: