利用Repeater控件显示主-从关系数据表 (NestedRepeater.aspx.cs)

NestedRepeater.aspx.cs

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace SNHY
{
 /// <summary>
 /// NestedRepeater 的摘要说明。
 /// </summary>
 public class NestedRepeater : System.Web.UI.Page
 {
  protected System.Web.UI.WebControls.Repeater Repeater1;
 
  private void Page_Load(object sender, System.EventArgs e)
  {
   //ClassInfo(CID,ClassName,ClassDep)
   //Topic(AID,CID,CTitle)
   DataSet myds=new DataSet();
   string dbFile=(new Common.PageBase()).ConfigTb.Rows[0]["AccessFile"].ToString();
   string connstr="provider=microsoft.jet.oledb.4.0;data source="+HttpContext.Current.Server.MapPath(dbFile);
   OleDbConnection dbconn=new OleDbConnection(connstr);

   OleDbDataAdapter cm1=new OleDbDataAdapter("select * from ClassInfo",dbconn);
   cm1.Fill(myds,"CTb");

   OleDbDataAdapter cm2=new OleDbDataAdapter("select * from Topic",dbconn);
   cm2.Fill(myds,"ATb");

   myds.Relations.Add("myrelation",myds.Tables["CTb"].Columns["CID"],myds.Tables["ATb"].Columns["CID"]);


   this.Repeater1.DataSource=myds.Tables["CTb"].DefaultView;
   this.Repeater1.DataBind();
  }

  #region Web 窗体设计器生成的代码
  override protected void OnInit(EventArgs e)
  {
   //
   // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
   //
   InitializeComponent();
   base.OnInit(e);
  }
  
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {   
   this.Load += new System.EventHandler(this.Page_Load);

  }
  #endregion
 }
}

转载于:https://www.cnblogs.com/YrRoom/archive/2006/01/04/310976.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
很抱歉,由于这是一个虚拟场景,我无法提供完整的 ASP.NET 网站的 .aspx 和 .aspx.cs 代码。但我可以给你一些可能会包含在首页中的代码片段作为参考: 在 .aspx 页面中,可能包含以下代码片段: ```html <div class="hot-items"> <h2>热门二手商品</h2> <ul> <asp:Repeater ID="rptItems" runat="server"> <ItemTemplate> <li> <a href='<%# Eval("ItemUrl") %>'> <img src='<%# Eval("ImageUrl") %>' alt='<%# Eval("ItemName") %>' /> <h3><%# Eval("ItemName") %></h3> <p><%# Eval("ItemDescription") %></p> <span><%# Eval("ItemPrice") %></span> </a> </li> </ItemTemplate> </asp:Repeater> </ul> </div> ``` 在 .aspx.cs 页面中,可能包含以下代码片段: ```csharp protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { // 从数据库中获取热门商品数据 DataTable dtItems = GetHotItems(); rptItems.DataSource = dtItems; rptItems.DataBind(); } } private DataTable GetHotItems() { // 连接数据库,查询热门商品数据 string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; SqlConnection conn = new SqlConnection(connectionString); string sql = "SELECT * FROM Items WHERE IsHot = 1 ORDER BY ItemPrice DESC"; SqlDataAdapter adapter = new SqlDataAdapter(sql, conn); DataTable dt = new DataTable(); adapter.Fill(dt); return dt; } ``` 以上代码片段仅供参考,实际情况可能有所不同,具体代码需要根据实际需求和数据库结构进行编写。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值