你说的是datalist嵌套吧 我举个例子 你看下 行不行
无标题页 using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
//
using System.Data.SqlClient;
using DAL;
public partial class NewClassDisplay : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
yasbind();
}
}
protected void yasbind()
{
//DataSet ds = new DataSet();
//String cnnstr = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
//SqlConnection cnn = new SqlConnection(cnnstr);
//String cmdstr = "";
//SqlCommand cmd = new SqlCommand();
String cmdstr = "select * from newstype";
//cmd.CommandText = cmdstr;
//cmd.Connection = cnn;
//SqlDataAdapter sda = new SqlDataAdapter(cmd);
try
{
//sda.Fill(ds);
//DataList1.DataSource = ds.Tables[0].DefaultView;
//DataList1.DataBind();
//DataList2.DataSource = ds.Tables[1].DefaultView;
//DataList2.DataBind();
// DataList1.DataSource = ds.Tables[0].DefaultView;
Dal yy = new Dal();
DataList1.DataSource = yy.dal_DS(cmdstr);
DataList1.DataBind();
}
catch (Exception ex)
{
throw ex;
}
finally
{
// dr.Close();
// cnn.Close();
}
}
protected void yasbind_s(string sql,DataList dl)
{
DataSet ds = new DataSet();
String cnnstr = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
SqlConnection cnn = new SqlConnection(cnnstr);
String cmdstr = "";
SqlCommand cmd = new SqlCommand();
cmdstr = sql;
cmd.CommandText = cmdstr;
cmd.Connection = cnn;
SqlDataAdapter sda = new SqlDataAdapter(cmd);
try
{
sda.Fill(ds);
//DataList1.DataSource = ds.Tables[0].DefaultView;
//DataList1.DataBind();
//DataList2.DataSource = ds.Tables[1].DefaultView;
//DataList2.DataBind();
dl.DataSource = ds.Tables[0].DefaultView;
dl.DataBind();
}
catch (Exception ex)
{
throw ex;
}
finally
{
// dr.Close();
// cnn.Close();
}
}
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
// e.Item.FindControl(
DataRowView rowv = (DataRowView)e.Item.DataItem; //一定知道该代码的含义?
String newstypeid = rowv["newstypeid"].ToString();
string sql = "select top 10 newsid, newstitle,newsdate from news where newstypeid="+newstypeid+" order by newsid ";
DataList dl = (DataList)e.Item.FindControl("DataList2");
if (dl!=null)
yasbind_s(sql, dl);
}
}