Repeater如何嵌套绑定数据?
RT,内层Repeater如何绑定数据
------解决方案--------------------帮别人做的例子,请参考
//aspx
<body>
<form id= "form1 " runat= "server ">
<asp:repeater id= "repeaterMain " Runat= "server " OnItemDataBound= "repeaterMain_ItemDataBound ">
<ItemTemplate>
<a href= ' <%# "Details.aspx?au_id= " + DataBinder.Eval(Container, "DataItem.au_id ")%> '> <%#DataBinder.Eval(Container, "DataItem.au_id ")%> </a>
<asp:repeater ID= "repeaterSub " Runat= "server ">
<ItemTemplate>
<a href= ' <%# "Details.aspx?title_id= " + DataBinder.Eval(Container, "DataItem.title_id ")%> '> <%#DataBinder.Eval(Container, "DataItem.title_id ")%> </a>
</ItemTemplate>
</asp:repeater>
</ItemTemplate>
</asp:repeater>
</form>
//aspx.cs
private void BindMain()
{
SqlConnection cn = new SqlConnection(@ "server=.\SQLExpress;uid=sa;pwd=password;database=pubs ");
SqlDataAdapter da = new SqlDataAdapter( "select au_id from authors ", cn);
DataSet ds = new DataSet();
cn.Open();
da.Fill(ds);
repeaterMain.DataSource = ds.Tables[0].DefaultView;
repeaterMain.DataBind();
cn.Close();
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindMain();
}
}
protected void repeaterMain_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
string au_id = ((DataRowView)e.Item.DataItem).Row[ "au_id "].ToString();
Repeater repeaterSub = (Repeater)e.Item.FindControl( "repeaterSub ");
if (repeaterSub != null)
{
SqlConnection cn = new SqlConnection(@ "server=.\SQLExpress;uid=sa;pwd=password;database=pubs ");
SqlCommand cmd = new SqlCommand( "select title_id from titleauthor where au_id = @au_id ", cn);
cmd.Parameters.Add( "@au_id ", SqlDbType.VarChar, 11).Value = au_id;
cn.Open();
repeaterSub.DataSource = cmd.ExecuteReader();
repeaterSub.DataBind();
cn.Close();
}
}