repeater显示主从表关系

运行结果如下图所示:
repeater显示主从表关系
WebForm1.aspx
<% @ Import Namespace="System.Data" %>
<% @ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="HibernateTest.WebForm1"  %>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"  >
< HTML >
    
< HEAD >
        
< title > WebForm1 </ 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 >
        
< form  id ="Form1"  method ="post"  runat ="server" >
            
< FONT  face ="宋体" >
                
<!--  父Repeater开始  -->
                
< asp:Repeater  id ="Repeater1"  runat ="server" >
                    
< itemtemplate >
                        
< b >
                            
< br >
                            CustomerID:
                            
<% # DataBinder.Eval(Container.DataItem,"CustomerID" %>
                        
</ b >
                        
< br >
                        
< b >
                            
< br >
                            CompanyName:
                            
<% # DataBinder.Eval(Container.DataItem,"CompanyName" %>
                        
</ b >
                        
< br >
                        OrderID:
                        
< br >
                        
<!--  子Repeater开始  -->
                        
< asp:repeater  id ="childRepeater"  runat ="server"  datasource ='<%#  ((DataRowView)Container.DataItem).Row.GetChildRows("CustOrder") % > '>
                            
< itemtemplate >
                                
<% # DataBinder.Eval(Container.DataItem, "[\"ORDERID\"]") %>
                                
< br >
                            
</ itemtemplate >
                        
</ asp:repeater >
                        
< hr >
                        
<!--  子Repeater结束  -->
                    
</ itemtemplate >
                
</ asp:Repeater >
                
<!--  父Repeater结束  -->
                
</ FONT >
        
</ form >
    
</ body >
</ HTML >
WebForm1.aspx.cs
// ***********************************************************
// *公司:
// *作者:YK
// *模块:WebForm1
// *功能:
// *创建日期:
// *修改日期:
// ***********************************************************
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  HibernateTest
{
    
/// <summary>
    
/// WebForm1 的摘要说明。
    
/// </summary>

    public class WebForm1 : System.Web.UI.Page
    
{
        
protected System.Web.UI.WebControls.Repeater Repeater1;
    
        
private void Page_Load(object sender, System.EventArgs e)
        
{
            
// 在此处放置用户代码以初始化页面
            if(!Page.IsPostBack)
            
{
                
this.GetData();
            }

        }


        
Web 窗体设计器生成的代码


        
private void GetData()
        
{
            SqlConnection con 
= new SqlConnection("server=(local);database=Northwind;uid=sa;pwd=;");
            System.Data.SqlClient.SqlDataAdapter da 
= new SqlDataAdapter("SELECT * FROM Customers",con);
            System.Data.SqlClient.SqlDataAdapter da1 
=new SqlDataAdapter("SELECT * FROM ORDERS",con);
            DataSet ds
= new DataSet();
            
try
            
{                
                con.Open();
                
//将主表Customer添加到DataSet中
                da.Fill(ds,"Customers");
                
//将子表Orders添加到DataSet中
                da1.Fill(ds,"Orders");
                
//添加表Customer和表Orders之间的关系CustOrder;关联字段CustomerID
                ds.Relations.Add("CustOrder",ds.Tables["Customers"].Columns["CustomerID"],ds.Tables["Orders"].Columns["CustomerID"]);

                
this.Repeater1.DataSource = ds;
                
this.Repeater1.DataBind();
            
            }

            
catch(Exception ex)
            
{
                
throw ex;
            }

            
finally
            
{
                con.Close();
                con.Dispose();
            }

        }

    }

}

你可能感兴趣的:(显示)