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

< %@ Page Language = " vb "  AutoEventWireup = " false "  Codebehind = " WebForm3.aspx.vb "   Inherits = " test.WebForm3 " % >
< !DOCTYPE HTML  PUBLIC   " -//W3C//DTD HTML 4.0 Transitional//EN " >
< HTML >
    
< HEAD >
        
< title > WebForm3 </ title >
        
< meta name = " GENERATOR "  content = " Microsoft Visual Studio .NET 7.1 " >
        
< meta name = " CODE_LANGUAGE "  content = " Visual Basic .NET 7.1 " >
        
< 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, " name " ) % >
    
</ b >
    
< br >
    
< ! --  子Repeater开始  -->
    
< asp:repeater id = " childRepeater "  runat = " server "  datasource = ' <%# Container.DataItem("myrelation") %>'>
       < itemtemplate >
        
< %# DataBinder.Eval(Container.DataItem,  " title " )% >
        
< br >
      
</ itemtemplate >
    
</ asp:repeater >
    
< ! --  子Repeater结束  -->
  
</ itemtemplate >
</ asp:repeater >
< ! --  父Repeater结束  -->

        
</ form >
    
</ body >
</ HTML >
^_webform3.aspx
Public   Class WebForm3
    
Inherits System.Web.UI.Page

#Region 
" Web 窗体设计器生成的代码 "

    '该调用是 Web 窗体设计器所必需的。
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    
End Sub

    
Protected WithEvents parentRepeater As System.Web.UI.WebControls.Repeater

    
'注意: 以下占位符声明是 Web 窗体设计器所必需的。
    '不要删除或移动它。
    Private designerPlaceholderDeclaration As System.Object

    
Private Sub Page_Init(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles MyBase.Init
        
'CODEGEN: 此方法调用是 Web 窗体设计器所必需的
        '不要使用代码编辑器修改它。
        InitializeComponent()
    
End Sub


#
End Region

    
Private Sub Page_Load(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles MyBase.Load
        
'在此处放置初始化页的用户代码
        ' 为Authors表创建 Connection 和 DataAdapter
        Dim myConnection As New SqlClient.SqlConnection
        myConnection.ConnectionString 
= "server=localhost;database=lhx;uid=sa;pwd=eeeee;Connect Timeout=9999"
        myConnection.Open()
        
Dim rs As New SqlClient.SqlDataAdapter("select * from authors", myConnection)
        
Dim ds As New DataSet


        
'创建填充 DataSet.
        rs.Fill(ds, "authors")

        
'为Titles表创建 DataAdapter
        Dim rs1 As New SqlClient.SqlDataAdapter("select * from titleauthor", myConnection)
        rs1.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()

        myConnection.Close()
        myConnection.Dispose()

    
End Sub


End Class

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值