你的位置:首页 > ASP.net教程

[ASP.net教程]asp.net:repeater嵌套(常用于新闻等在首页归类显示)

using System;
using System.Configuration;
using System.Collections.Generic;
using System.Linq;
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;
using System.Data;
using System.Data.SqlClient;


public partial class index : System.Web.UI.Page
{
protected Repeater childRepeater;//parentRepeater;
public index()
{
Page.Init += new EventHandler(Page_Init);
}
public void Page_Load(object sender, EventArgs e)
{

//Create the connection and DataAdapter for the Authors table.//创建连接,DataAdapter作者表.
string s = "Data Source=(LocalDB)\\v11.0;AttachDbFilename=|DataDirectory|\\xlgameguide.mdf;Integrated Security=True;Connect Timeout=30";
SqlConnection con = new SqlConnection(s);
SqlDataAdapter cmd1 = new SqlDataAdapter("select * from gamestyle", con);
//Create and fill the DataSet.//创建和填充数据集。
DataSet ds = new DataSet();
cmd1.Fill(ds, "fenlei");
//Create a second DataAdapter for the Titles table.//创建第二个DataAdapter的标题表。
SqlDataAdapter cmd2 = new SqlDataAdapter("select * from newswords", con);
cmd2.Fill(ds, "wenzhang");
//Create the relation bewtween the Authors and Titles tables.//创建bewtween作者和标题的关系表。
ds.Relations.Add("myrelation",
ds.Tables["fenlei"].Columns["gamestyleid"],
ds.Tables["wenzhang"].Columns["gamestyleid"]);
//Bind the Authors table to the parent Repeater control, and call DataBind.//作者表绑定到父中继器控制和调用DataBind。
parentRepeater.DataSource = ds.Tables["fenlei"];
Page.DataBind();
//Close the connection.//关闭连接。
con.Close();
cmd1.Dispose();
cmd2.Dispose();

//这段是在页面绑定的格式
//<asp:repeater id="parentRepeater" runat="server">
// <itemtemplate>
// <b><%# DataBinder.Eval(Container.DataItem,"gamestyle") %></b><br>
// <!-- start child repeater -->
// <asp:repeater id="childRepeater" datasource='<%# ((System.Data.DataRowView)Container.DataItem).Row.GetChildRows("myrelation") %>' runat="server">
// <itemtemplate>
// <%# DataBinder.Eval(Container.DataItem, "[\"newsname\"]")%><br>
// </itemtemplate>
// </asp:repeater>
// <!-- end child repeater -->
// </itemtemplate>
//</asp:repeater>


}
//两个无返回值的必须的方法
private void Page_Init(object sender, EventArgs e)
{
InitializeComponent();
}
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}

 

}