利用Repeater控件显示主-从关系数据表

作者:袖梨 2022-07-02
NestedRepeater.aspx










NestedRepeater.aspx.csusing 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 eMeng
{
///
/// NestedRepeater 的摘要说明。
/// 本文介绍如何利用Repeater控件显示主-从关系的表格
/// 原文参见:
/// http://support.microsoft.com/default.aspx?scid=kb;en-us;306154
///
public class NestedRepeater : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Repeater parentRepeater;
public void Page_Load(object sender, EventArgs e)
{
// 为Authors表创建 Connection 和 DataAdapter
string cnnString = @"server=(local)NetSDK;database=pubs; Integrated Security=SSPI;";
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();

相关文章

精彩推荐