这篇文章是我应该早早就写出来的,但是一直没有时间就耽误了,我去年学习asp.net2.0也遇到过类似的问题,然后就呀找
最后好像是看了外国的的网站最后解决了,然后看到一个朋友的blog上遇到了同样的问题无法解决,就留了一个言,
结果去年的留言,今年还有人顺着那个留言来问我解决方法,而且不止一个,比较郁闷,所以今天把代码放出来
供大家参考:

前台代码:

<!--第一层Repeater开始-->
<asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_ItemDataBound">
 <ItemTemplate>
 <%# Eval("log_id")%>
 <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
 ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>"
 SelectCommand="SELECT * FROM [blog_Category] WHERE ([cat_id] = @cat_id)">
 <SelectParameters>
 <asp:Parameter DefaultValue="cat_id" Name="cat_id" />
 </SelectParameters>
 </asp:SqlDataSource>
    <!--第二层Repeater2开始-->
 <asp:Repeater ID="Repeater2" runat="server" DataSourceID="SqlDataSource2">
 <ItemTemplate><%#Eval("cat_name")%></a>
 </ItemTemplate>
 </asp:Repeater>
<!--第二层Repeater2结束-->
 </ItemTemplate>
</asp:Repeater>
<!--第一层Repeater结束-->

后台代码:
 protected void Repeater1()
 {
 OleDbConnection conn;
 OleDbCommand cmd;
 System.Configuration.ConnectionStringSettings s = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"];
 conn = new OleDbConnection();
 conn.ConnectionString = s.ConnectionString;
 cmd = new OleDbCommand("select count(*) from blog_Article where log_catID=" + Request["cat"].ToString(), conn);
 conn.Open();
 AspNetPager1.RecordCount = (int)cmd.ExecuteScalar();
 conn.Close();
 BindData();
 }

 protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
 {
 DataRowView drv = (DataRowView)(e.Item.DataItem);
 SqlDataSource tempSqlDataSource = (SqlDataSource)e.Item.FindControl("SqlDataSource2");
 tempSqlDataSource.SelectParameters["cat_id"].DefaultValue = drv.Row["log_catID"].ToString();
 }

这段代码是我从我未写完的blog程序里面摘取的代码片段,我们来分析一下思路,首先用Repeater1显示主表[blog_Article]的
数据,然后获取当前行的[log_catID]字段的值,根据这个值来查找[blog_Category]表中的值并输出[cat_name]字段。

哪其中最关键的部分就是Repeater的Repeater1_ItemDataBound事件:

引用msdn
当 Repeater 中的某项被数据绑定时引发该事件。此事件为您提供了在客户端显示数据项之前访问该数据项的最后机会。当引发此事件后,该数据项将被设为空,并且不再可用。

就是说在页面里的Repeater的一项已经绑定了数据,但是这个时候数据并没有显示出来,就再这个时候我们把id值取出来从新读取数据库其他表,然后把数据显示出来。

基本就是这样,多重嵌套没有实验过,如果那位朋友实验过可以给个反馈。:)
http://www.d8in.com/blog/article.asp?id=309
posted on 2007-02-25 12:41  mbskys  阅读(111)  评论(0)    收藏  举报