repeater嵌套

发个以前学习ASP.NET的时候写的个Repeater嵌套 test.aspx:
<asp:Repeater ID="outItem" runat="server"> <ItemTemplate> <div class="bbs_item"> <div class="bbs_item_title"><a href="bbs.aspx?cid=<%#Eval("dq_cid") %>"><%#Eval("dq_name") %></a></div> <div class="bbs_item_content"> <table> <asp:Repeater ID="inItem" runat="server"> <ItemTemplate> <tr><td width="450px"> <a href="tzSHow.aspx?aid=<%#Eval("dq_aid") %>"><%#common.cutStr(Eval("dq_title").ToString(),25) %></a></td><td width="70px"><%#common.uid2username(int.Parse(Eval("dq_uid").ToString())) %></td><td width="70px"><%#Eval("dq_repNo") %>/<%#Eval("dq_readNo") %></td><td><%#Eval("dq_time") %></td></tr> </ItemTemplate> </asp:Repeater> </table> </div> </div> </ItemTemplate> </asp:Repeater>
test.aspx.cs
protected void outItemDataBind() { //SqlConnection conn = new SqlConnection(this.myConnectionString); string sql = "SELECT [dq_cid],[dq_name],[dq_banzhu] FROM [dq_bbsclass] ORDER BY [dq_no],[dq_cid]"; SqlDataSource ds = new SqlDataSource(this.myConnectionString,sql); outItem.DataSource = ds; outItem.DataBind(); /* SqlCommand comm = new SqlCommand(sql, conn); conn.Open(); SqlDataReader myData = comm.ExecuteReader(); if (myData.Read()) { outItem.DataSource = myData; outItem.DataBind(); }*/ } protected void inItemDataBound(object sender, RepeaterItemEventArgs e) { if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item) { Repeater inRepeater = (Repeater)e.Item.FindControl("inItem"); //System.Data.Common.DbDataRecord newRow = (System.Data.Common.DbDataRecord)e.Item.DataItem; DataRowView newRow = (DataRowView)e.Item.DataItem; int parentId = Convert.ToInt32(newRow["dq_cid"]); //Response.Write(parentId); if (parentId != this.tzFbCid) { SqlConnection conn = new SqlConnection(this.myConnectionString); string cmdStr = "SELECT TOP 10 * FROM [dq_bbsMain] WHERE [dq_isTop]=1 and [dq_cid]='" + parentId.ToString() + "' ORDER BY [dq_aid] DESC"; SqlCommand comm = new SqlCommand(cmdStr, conn); DataSet ds = new DataSet(); conn.Open(); //SqlDataReader dr = comm.ExecuteReader(); SqlDataAdapter sda = new SqlDataAdapter(comm); sda.Fill(ds, "list"); int nCount = ds.Tables["list"].Rows.Count; for (int i = 0; i < nCount; i++) { ds.Tables["list"].Rows[i]["dq_title"] = "【置顶】" + ds.Tables["list"].Rows[i]["dq_title"].ToString(); } if (nCount < 10) { int topCount = 10 - nCount; string sql2 = "SELECT TOP " + topCount.ToString() + " * FROM [dq_bbsMain] WHERE [dq_isTop]!=1 and [dq_cid]='" + parentId.ToString() + "' ORDER BY [dq_aid] DESC"; SqlCommand comm2 = new SqlCommand(sql2, conn); SqlDataAdapter sda2 = new SqlDataAdapter(comm2); sda2.Fill(ds, "list"); } inRepeater.DataSource = ds.Tables["list"]; inRepeater.DataBind(); conn.Close(); } else { SqlConnection conn = new SqlConnection(this.myConnectionString); string cmdStr = "SELECT TOP 10 * FROM [dq_bbsMain] WHERE [dq_isTop]=1  AND [dq_shjg]=1   and [dq_cid]='" + parentId.ToString() + "' ORDER BY [dq_aid] DESC"; SqlCommand comm = new SqlCommand(cmdStr, conn); DataSet ds = new DataSet(); conn.Open(); //SqlDataReader dr = comm.ExecuteReader(); SqlDataAdapter sda = new SqlDataAdapter(comm); sda.Fill(ds, "list"); int nCount = ds.Tables["list"].Rows.Count; for (int i = 0; i < nCount; i++) { ds.Tables["list"].Rows[i]["dq_title"] = "【置顶】" + ds.Tables["list"].Rows[i]["dq_title"].ToString(); } if (nCount < 10) { int topCount = 10 - nCount; string sql2 = "SELECT TOP " + topCount.ToString() + " * FROM [dq_bbsMain] WHERE [dq_isTop]!=1  AND [dq_shjg]=1   and [dq_cid]='" + parentId.ToString() + "' ORDER BY [dq_aid] DESC"; SqlCommand comm2 = new SqlCommand(sql2, conn); SqlDataAdapter sda2 = new SqlDataAdapter(comm2); sda2.Fill(ds, "list"); } inRepeater.DataSource = ds.Tables["list"]; inRepeater.DataBind(); conn.Close(); } //dr.Close(); } }
Page_Load:
outItem.ItemDataBound += new System.Web.UI.WebControls.RepeaterItemEventHandler(this.inItemDataBound);

“repeater嵌套”   No comments

No comments yet.

Leave a comment:

XHTML: You can use these tags: <a href="" title=""> <b> <blockquote cite=""> <em> <i> <strong> <p> <br><br />