在嵌套的repeater中加ItemDataBound事件
一. 在开发过程中用到了嵌套repeater来显示页面,想在嵌套的repeater中的ItemDataBound中处理一些控件的显示。
用以下代码可以实现
private void repeaterMain_ItemCreated(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)
{
// 确保处理的是数据行,而不是Header或者Footer
if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType==ListItemType.SelectedItem)
{
Repeater _subRepeater=(Repeater)e.Item.FindControl("repeaterSub");
_subRepeater.ItemDataBound += new System.Web.UI.WebControls.RepeaterItemEventHandler(this.initSubRepeater);
}
}
private void initRepeater(object sender,System.Web.UI.WebControls.RepeaterItemEventArgs e)
{
..
}
ItemDataBound 事件的绑定语句
_subRepeater.ItemDataBound += new System.Web.UI.WebControls.RepeaterItemEventHandler(this.initSubRepeater);
要在
_subRepeater.DataBind();
之前
二. 利用Repeater控件显示主-从关系数据表的方法。
原文参见:http://support.microsoft.com/default.aspx?scid=kb;en-us;306154
NestedRepeater.aspx
<%@ Import Namespace="System.Data" %>
<%@ Page language="c#" Codebehind="NestedRepeater.aspx.cs"
AutoEventWireup="false" Inherits="eMeng.NestedRepeater" %>
<html>
<body>
<form runat="server">
<!-- 父Repeater开始 -->
<asp:repeater id="parentRepeater" runat="server">
<itemtemplate>
<strong>
<%# DataBinder.Eval(Container.DataItem,"au_id") %>
</strong>
<br />
<!-- 子Repeater开始 -->
<asp:repeater id="childRepeater" runat="server"
datasource='<%# ((DataRowView)Container.DataItem).Row.GetChildRows("myrelation") %>'>
<itemtemplate>
<%# DataBinder.Eval(Container.DataItem, "[\"title_id\"]")%>
<br />
</itemtemplate>
</asp:repeater>
<!-- 子Repeater结束 -->
</itemtemplate>
</asp:repeater>
<!-- 父Repeater结束 -->
</form>
</body>
</html>
NestedRepeater.aspx.cs
using 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
{
/// <summary>
/// NestedRepeater 的摘要说明。
/// 本文介绍如何利用Repeater控件显示主-从关系的表格
/// 原文参见:
/// http://support.microsoft.com/default.aspx?scid=kb;en-us;306154
/// </summary>
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();
cnn.Dispose();
}
private void Page_Init(object sender, EventArgs e)
{
InitializeComponent();
}
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
public NestedRepeater()
{
Page.Init += new System.EventHandler(Page_Init);
}
}
}