Repeater 嵌套使用

页面代码:

<asp:Repeater ID="Repeater1" runat="server">

       <itemtemplate>

       <table>

       <tr>

        <%# DataBinder.Eval(Container.DataItem, "c_name") %><br>

        <asp:Repeater ID="Repeater2" runat="server">

         <itemtemplate>

          <a href='Article_Detile.aspx?Id=<%# DataBinder.Eval(Container.DataItem, "a_id") %>'>

           <%# DataBinder.Eval(Container.DataItem, "a_title") %><br>

          </a>

         </itemtemplate>

        </asp:Repeater>

       </tr>

       </table>

       </itemtemplate>

      </asp:Repeater>


后台程序:
public void loadclassdata()

{

  string connstr = "Provider = Microsoft.Jet.OLEDB.4.0;Data Source = '" +

    Server.MapPath("../DataBase/DB.mdb") + "'";

  string commstr = "select * from tb_class";



  OleDbConnection conn = new OleDbConnection(connstr);

  OleDbCommand comm = new OleDbCommand(commstr, conn);



  DataSet myds = new DataSet();



  OleDbDataAdapter myadapter = new OleDbDataAdapter(comm);



  try

  {

    myadapter.Fill(myds, "tb_class");

    this.Repeater1.DataSource = myds.Tables["tb_class"].DefaultView;

    this.Repeater1.DataBind();

  }

  catch (Exception ex)

  {

    Response.Write(ex.Message);

  }

  finally{



  }

}



private void Repeater1_ItemCreated(object sender,

  System.Web.UI.WebControls.RepeaterItemEventArgs e)

{

  object cid;

  cid = DataBinder.Eval(e.Item.DataItem, "c_id");

  Repeater Repeater2;

  Repeater2 = (Repeater)e.Item.FindControl("Repeater2");

  string connstr = "Provider = Microsoft.Jet.OLEDB.4.0;Data Source = '" +

    Server.MapPath("../DataBase/DB.mdb") + "'";

  string commstr = "select * from tb_article";

  OleDbConnection conn = new OleDbConnection(connstr);

  OleDbCommand cmd = new OleDbCommand(commstr, conn);

  OleDbDataAdapter da = new OleDbDataAdapter(cmd);

  DataSet ds = new DataSet();

  da.Fill(ds, "tb_article");

  Repeater2.DataSource = ds.Tables["tb_article"].DefaultView;

  Repeater2.DataBind();

}


 

你可能感兴趣的:(使用)