zoukankan      html  css  js  c++  java
  • 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);
    }

    }

  • 相关阅读:
    精选文章
    Eclipse Git插件切换分支的时候不要Reset
    Spring ContentNegotiatingViewResolver
    Spring3 MVC 类型转换
    FTP DOS 命令行
    Java xml 解析
    Java 实现FTP上传和下载
    Hibernate update 和 merge 、saveOrUpdate的区别
    Spring MVC 文件下载时候 发现IE不支持
    Javasript 正则匹配任意字符
  • 原文地址:https://www.cnblogs.com/Leon-Jenny/p/4883625.html
Copyright © 2011-2022 走看看