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

本文介绍了一种利用Repeater控件显示主-从关系数据表的方法。

原文参见:http://support.microsoft.com/default.aspx?scid=kb;en-us;306154

NestedRepeater.aspx

 

ExpandedBlockStart.gif ContractedBlock.gif <% dot.gif @ Import Namespace="System.Data"  %>
ExpandedBlockStart.gifContractedBlock.gif
<% dot.gif @ Page language="c#" Codebehind="NestedRepeater.aspx.cs" 
ExpandedBlockEnd.gif AutoEventWireup
="false" Inherits="eMeng.NestedRepeater" 
%>
None.gif
< HTML >
None.gif
< body >
None.gif
< form  runat ="server" >
None.gif
<!--  父Repeater开始  -->
None.gif
< asp:repeater  id ="parentRepeater"  runat ="server" >
None.gif  
< itemtemplate >
None.gif    
< b >
ExpandedBlockStart.gifContractedBlock.gif      
<% dot.gif # DataBinder.Eval(Container.DataItem,"au_id" %>
None.gif    
</ b >
None.gif    
< br >
None.gif    
<!--  子Repeater开始  -->
None.gif    
< asp:repeater  id ="childRepeater"  runat ="server"  
None.gif     datasource
='<%#  ((DataRowView)Container.DataItem).Row.GetChildRows("myrelation") % > '>
None.gif      
< itemtemplate >
ExpandedBlockStart.gifContractedBlock.gif        
<% dot.gif # DataBinder.Eval(Container.DataItem, "[\"title_id\"]") %>
None.gif        
< br >
None.gif      
</ itemtemplate >
None.gif    
</ asp:repeater >
None.gif    
<!--  子Repeater结束  -->
None.gif  
</ itemtemplate >
None.gif
</ asp:repeater >
None.gif
<!--  父Repeater结束  -->
None.gif
</ form >
None.gif
</ body >
None.gif
</ HTML >




NestedRepeater.aspx.cs

None.gif using  System;
None.gif
using  System.Data;
None.gif
using  System.Data.SqlClient;
None.gif
using  System.Web;
None.gif
using  System.Web.SessionState;
None.gif
using  System.Web.UI;
None.gif
using  System.Web.UI.WebControls;
None.gif
None.gif
namespace  eMeng
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//// 
InBlock.gif
/// NestedRepeater 的摘要说明。
InBlock.gif
/// 本文介绍如何利用Repeater控件显示主-从关系的表格
InBlock.gif
/// 原文参见:
InBlock.gif
/// http://support.microsoft.com/default.aspx?scid=kb;en-us;306154
ExpandedSubBlockEnd.gif
/// 

InBlock.gifpublic class NestedRepeater : System.Web.UI.Page
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
protected System.Web.UI.WebControls.Repeater parentRepeater;
InBlock.gif
InBlock.gif
public void Page_Load(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif  
// 为Authors表创建 Connection 和 DataAdapter
InBlock.gif
  string cnnString = @"server=(local)\NetSDK;database=pubs; Integrated Security=SSPI;";
InBlock.gif  SqlConnection cnn 
= new SqlConnection(cnnString);
InBlock.gif  SqlDataAdapter cmd1 
= new SqlDataAdapter("select * from authors",cnn);
InBlock.gif
InBlock.gif  
//创建填充 DataSet.
InBlock.gif
  DataSet ds = new DataSet();
InBlock.gif  cmd1.Fill(ds,
"authors");
InBlock.gif
InBlock.gif  
// 为Titles表创建 DataAdapter
InBlock.gif
  SqlDataAdapter cmd2 = new SqlDataAdapter("select * from titleauthor",cnn);
InBlock.gif  cmd2.Fill(ds,
"titles");
InBlock.gif
InBlock.gif  
// 创建 Authors 表和 Titles 表之间的关系.
InBlock.gif
  ds.Relations.Add("myrelation",
InBlock.gif    ds.Tables[
"authors"].Columns["au_id"],
InBlock.gif    ds.Tables[
"titles"].Columns["au_id"]);
InBlock.gif
InBlock.gif  
// 绑定Authors到父Repeater
InBlock.gif
  parentRepeater.DataSource = ds.Tables["authors"];
InBlock.gif  Page.DataBind();
InBlock.gif
InBlock.gif  cnn.Close();
InBlock.gif  cnn.Dispose();
ExpandedSubBlockEnd.gif}

InBlock.gif
private void Page_Init(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif  InitializeComponent();
ExpandedSubBlockEnd.gif}

InBlock.gif
private void InitializeComponent()
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{    
InBlock.gif  
this.Load += new System.EventHandler(this.Page_Load);
ExpandedSubBlockEnd.gif}

InBlock.gif
public NestedRepeater()
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif  Page.Init 
+= new System.EventHandler(Page_Init);
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

ExpandedBlockEnd.gif}

None.gif
None.gif 
None.gif
None.gif


Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=27695

转载于:https://www.cnblogs.com/sharewind/archive/2007/02/11/647220.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值