有的网友在问怎样显示子表的前N条记录,下面是我的做法:
C#
aspx文件:
<%...@ Page language="c#" Codebehind="WebForm13.aspx.cs" AutoEventWireup="false" Inherits="test1.WebForm13" %><%...@ Import Namespace="System.Data" %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" ><HTML> <HEAD> <title>WebForm13</title> <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1"> <meta name="CODE_LANGUAGE" Content="C#"> <meta name="vs_defaultClientScript" content="JavaScript"> <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5"> </HEAD> <body MS_POSITIONING="GridLayout"> <form id="Form1" method="post" runat="server"> <!-- 父Repeater开始 --> <asp:repeater id="parentRepeater" runat="server"> <itemtemplate> <b> <%...# DataBinder.Eval(Container.DataItem,"au_id") %> </b> <br>
<!-- 子Repeater开始 --> <asp:repeater id="childRepeater" runat="server" datasource='<%# getrow((DataRowView)Container.DataItem,1)%>'> <itemtemplate> <%...# DataBinder.Eval(Container.DataItem, "[\"title_id\"]")%> <br>
</itemtemplate> </asp:repeater> <!-- 子Repeater结束 --> </itemtemplate> </asp:repeater> <!-- 父Repeater结束 --> </form> </body></HTML>cs文件:
using System;using System.Collections;using System.ComponentModel;using System.Data;using System.Drawing;using System.Web;using System.Web.SessionState;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.HtmlControls;using System.Data.SqlClient;namespace test1...{ /**//// <summary> /// WebForm13 的摘要说明。 /// </summary> public class WebForm13 : System.Web.UI.Page ...{ protected System.Web.UI.WebControls.Repeater parentRepeater; private void Page_Load(object sender, System.EventArgs e) ...{ // 在此处放置用户代码以初始化页面 string cnnString = @"server=(local);database=pubs;uid=sa;pwd=sa"; SqlConnection cnn = new SqlConnection(cnnString); SqlDataAdapter cmd1 = new SqlDataAdapter("select * from authors",cnn); //创建填充 DataSet. DataSet ds = new DataSet(); cmd1.Fill(ds,"authors"); // 为Titles表创建 DataAdapter SqlDataAdapter cmd2 = new SqlDataAdapter("select * from titleauthor",cnn); cmd2.Fill(ds,"titles"); // 创建 Authors 表和 Titles 表之间的关系. ds.Relations.Add("myrelation", ds.Tables["authors"].Columns["au_id"], ds.Tables["titles"].Columns["au_id"]); // 绑定Authors到父Repeater parentRepeater.DataSource = ds.Tables["authors"]; Page.DataBind(); cnn.Close(); cnn.Dispose(); } Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码 override protected void OnInit(EventArgs e) ...{ // // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。 // InitializeComponent(); base.OnInit(e); } /**//// <summary> /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// </summary> private void InitializeComponent() ...{ //this.parentRepeater.DataBinding += new System.EventHandler(this.parentRepeater_DataBinding); this.Load += new System.EventHandler(this.Page_Load); } #endregion protected DataRow[] getrow(DataRowView drv,int num) ...{ DataRow[] dr=drv.Row.GetChildRows("myrelation"); if(dr.Length>num) ...{ DataRow[] drtemp=new DataRow[num]; for(int i=0;i<num;i++) ...{ drtemp[i]=dr[i]; } return drtemp; } else return drv.Row.GetChildRows("myrelation"); } }}