關於對孟子E章《利用Repeater控制元件顯示主-從關係資料表》的一點補充(轉)

weixin_34304013發表於2007-02-11

 

None.gif
None.gif
None.gif
None.gif有的網友在問怎樣顯示子表的前N條記錄,下面是我的做法:
None.gifC#
None.gifASPx檔案:
None.gif
<%@ Page language="c#" Codebehind="WebForm13.aspx.cs" AutoEventWireup="false" Inherits="test1.WebForm13" %>
None.gif
<%@ Import Namespace="System.Data" %>
None.gif
None.gif
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
None.gif
<HTML>
None.gif    
<HEAD>
None.gif        
<title>WebForm13</title>
None.gif        
<meta name="GENERATOR" Content="Microsoft Visual Studio .net 7.1">
None.gif        
<meta name="CODE_LANGUAGE" Content="C#">
None.gif        
<meta name="vs_defaultClientScript" content="JavaScript">
None.gif        
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
None.gif    
</HEAD>
None.gif    
<body MS_POSITIONING="GridLayout">
None.gif        
<form id="Form1" method="post" runat="server">
None.gif            
<!-- 父Repeater開始 -->
None.gif            
<asp:repeater id="parentRepeater" runat="server">
None.gif                
<itemtemplate>
None.gif                    
<b>
None.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" datasource='<%# getrow((DataRowView)Container.DataItem,1)%>'>
None.gif                        
<itemtemplate>
None.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>
None.gifcs檔案:
None.gif
using System;
None.gif
using System.Collections;
None.gif
using System.ComponentModel;
None.gif
using System.Data;
None.gif
using System.Drawing;
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
using System.Web.UI.HtmlControls;
None.gif
using System.Data.SqlClient;
None.gif
None.gif
namespace test1
ExpandedBlockStart.gifContractedBlock.gifdot.gif
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//**//**//// <summary>
InBlock.gif    
/// WebForm13 的摘要說明。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class WebForm13 : System.Web.UI.Page
ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif
dot.gif{
InBlock.gif        
protected System.Web.UI.WebControls.Repeater parentRepeater;
InBlock.gif    
InBlock.gif        
private void Page_Load(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif
dot.gif{
InBlock.gif            
// 在此處放置使用者程式碼以初始化頁面
InBlock.gif
            string cnnString = @"server=(local);database=pubs;uid=sa;pwd=sa";
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();
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        Web 窗體設計器生成的程式碼
Web 窗體設計器生成的程式碼#region Web 窗體設計器生成的程式碼
InBlock.gif        
override protected void OnInit(EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif
dot.gif{
InBlock.gif            
//
InBlock.gif            
// CODEGEN: 該呼叫是 ASP.NET Web 窗體設計器所必需的。
InBlock.gif            
//
InBlock.gif
            InitializeComponent();
InBlock.gif            
base.OnInit(e);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//**//**//// <summary>
InBlock.gif        
/// 設計器支援所需的方法 - 不要使用程式碼編輯器修改
InBlock.gif        
/// 此方法的內容。
ExpandedSubBlockEnd.gif        
/// </summary>

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

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
InBlock.gif        
protected DataRow[] getrow(DataRowView drv,int num)
ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif
dot.gif{
InBlock.gif            DataRow[] dr
=drv.Row.GetChildRows("myrelation");
InBlock.gif
InBlock.gif            
if(dr.Length>num)
ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif
dot.gif{
InBlock.gif                DataRow[] drtemp
=new DataRow[num];
InBlock.gif                
for(int i=0;i<num;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif
dot.gif{
InBlock.gif                    drtemp[i]
=dr[i];
ExpandedSubBlockEnd.gif                }

InBlock.gif                
return drtemp;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
InBlock.gif                
return drv.Row.GetChildRows("myrelation");
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
None.gif

相關文章