<asp:Repeater ID=”rp” runat=”server”> <ItemTemplate> <%# Eval(“id”) %> <asp:Repeater ID=”rp1″ runat=”server” DataSource='<%# GetData(Eval(“id”)) %>’> <ItemTemplate> <%# Eval(“name”) %><br /> </ItemTemplate> </asp:Repeater> <br /> </ItemTemplate> </asp:Repeater>
其中关键是<asp:Repeater ID=”rp1″ runat=”server” DataSource='<%# GetData(Eval(“id”)) %>’> GetData是一个自义函数,原型如下:
protected DataTable GetData(object obj) { DataTable dt = new DataTable(); //这里做一些事情 return dt; }
完整的代码如下:aspx
<%@ Page Language=”C#” AutoEventWireup=”true” CodeFile=”test.aspx.cs” Inherits=”test” %> <%@ Import Namespace=”System.Data” %>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml” > <head runat=”server”> <title>nid’s blog http://www.ljnid.cn</title> </head> <body> <form id=”form1″ runat=”server”> <div> <asp:Repeater ID=”rp” runat=”server”> <ItemTemplate> <%# Eval(“id”) %> <asp:Repeater ID=”rp1″ runat=”server” DataSource='<%# GetData(Eval(“id”)) %>’> <ItemTemplate> <%# Eval(“name”) %><br /> </ItemTemplate> </asp:Repeater> <br /> </ItemTemplate> </asp:Repeater> </div> </form> </body> </html>
aspx.cs文件
using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls;
public partial class test : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { DataTable dt = new DataTable(); dt.Columns.Add(“id”); for (int i = 0; i < 4; i++) { DataRow dr = dt.NewRow(); dr[“id”] = i; dt.Rows.Add(dr); }
rp.DataSource = dt; rp.DataBind(); } protected DataTable GetData(object obj) { DataTable dt = new DataTable(); dt.Columns.Add(“name”); for (int i = 0; i < 10; i++) { DataRow dr = dt.NewRow(); dr[“name”] = “name_” + obj + “_” + i; dt.Rows.Add(dr); } return dt; } }
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/183307.html原文链接:https://javaforall.cn