关于对孟子E章《利用Repeater控件显示主-从关系数据表》的一点补充

王朝other·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

原文参看 利用Repeater控件显示主-从关系数据表

有的网友在问怎样显示子表的前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");

}

}

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有  導航