码迷,mamicode.com
首页 > Web开发 > 详细

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

时间:2015-10-15 21:54:12      阅读:139      评论:0      收藏:0      [点我收藏+]

标签:

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);
}

 

}

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

标签:

原文地址:http://www.cnblogs.com/Leon-Jenny/p/4883625.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!