首页 > 开发 > 综合 > 正文

利用Repeater控件显示主-从关系数据表

2024-07-21 02:24:06
字体:
来源:转载
供稿:网友

nestedrepeater.aspx

&lt;%@ import namespace="system.data" %&gt;&lt;%@ page language="c#" codebehind="nestedrepeater.aspx.cs" autoeventwireup="false" inherits="emeng.nestedrepeater" %&gt;<html><body><form runat="server"> <itemtemplate> <b> &lt;%# databinder.eval(container.dataitem,"au_id") %&gt; </b> <br> <itemtemplate> &lt;%# databinder.eval(container.dataitem, "[/"title_id/"]")%&gt; <br> </itemtemplate> :repeater> </itemtemplate>: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&#123;/// <summary>/// nestedrepeater 的摘要说明。/// 本文介绍如何利用repeater控件显示主-从关系的表格/// 原文参见:/// http://support.microsoft.com/default.aspx?scid=kb;en-us;306154/// </summary>public class nestedrepeater : system.web.ui.page&#123;protected system.web.ui.webcontrols.repeater parentrepeater;public void page_load(object sender, eventargs e)&#123; // 为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();&#125;private void page_init(object sender, eventargs e)&#123; initializecomponent();&#125;private void initializecomponent()&#123; this.load += new system.eventhandler(this.page_load);&#125;public nestedrepeater()&#123; page.init += new system.eventhandler(page_init);&#125;&#125;&#125;

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表