Repeater巢狀繫結Repeater

wangsen2235068發表於2013-09-06
C#:
private void RpTypeBind()
        {
//GetQuestionTypeAndCount() 返回一個datatable
            this.rptypelist.DataSource = LiftQuestionCtr.GetQuestionTypeAndCount();                   
            
this.rptypelist.DataBind();
        }
               
protected void rptypelist_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {

//判斷裡層repeater處於外層repeater的哪個位置( AlternatingItemTemplate,FooterTemplate,

//HeaderTemplate,,ItemTemplate,SeparatorTemplate)
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Repeater rep 
= e.Item.FindControl("rpquestionlist"as Repeater;//找到裡層的repeater物件
                DataRowView rowv = (DataRowView)e.Item.DataItem;//找到分類Repeater關聯的資料項 
                int typeid = Convert.ToInt32(rowv["qtid"]); //獲取填充子類的id 
                rep.DataSource = LiftQuestionCtr.GetSomeQuestionsByTypeid(typeid, 2);
                rep.DataBind();
            }
        }

 

aspx:


<table border="0" cellpadding="0" cellspacing="0" style="margin-bottom: 5px" width="100%">
    
<asp:Repeater runat="server" ID="rptypelist" OnItemDataBound="rptypelist_ItemDataBound">
        
<ItemTemplate>
            
<tr>
                
<td class="fb">
                    
&nbsp;&nbsp;<a href="Lists.aspx?Qtid=<%#Eval("qtid") %>"><%#Eval("qtypename"%></a> <span id="askCount_15"
                        
class="date2">[<%#Eval("typecount")%>]</span>
                
</td>
            
</tr>
            
<tr>
                
<td>
                    
<div id="askTop2_15" class="ask_list">
                        
<asp:Repeater runat="server" ID="rpquestionlist">
                            
<ItemTemplate>
                                
<a href="Question.aspx?qid=<%#Eval("liftquestionid")%>">
                                    
<%#Eval("questiontitle")%></a><br />
                            
</ItemTemplate>
                        
</asp:Repeater>
                    
</div>
                
</td>
            
</tr>
        
</ItemTemplate>
    
</asp:Repeater>
</table>
如果繫結的是物件Model:
 protected void rptHomeHelpCategory_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Repeater rptHomeHelp = (Repeater)e.Item.FindControl("rptHomeHelp");
                //找到分類Repeater關聯的資料項
                HiddenField hd_CategoryId = e.Item.FindControl("hd_CategoryId") as HiddenField;
                //提取分類ID
                //int CategorieId = Convert.ToInt32(hd_CategoryId.Value);
                var helpCategoryInfo = e.Item.DataItem as HelpCategoryInfo;//找到分類Repeater關聯的資料項 
                if (helpCategoryInfo != null)
                {
                    int CategorieId = helpCategoryInfo.CategoryID; //獲取填充子類的id 
                    //根據分類ID查詢該分類下的產品,並繫結產品Repeater
                    string filter = " IsToHome=1 and categoryid =" + CategorieId + " ";
                    string argument = string.Empty;
                    string value = string.Empty;
                    string order = "[helpId],[TopLevel] desc";
                    int totalRecords = 0;

                    IHelp dal = Factory<IHelp>.Create("Help");
                    var data = dal.HelpList(filter, string.Empty, string.Empty, order, 1, 100, out totalRecords);

                    if (data != null)
                    {
                        rptHomeHelp.DataSource = data;
                        rptHomeHelp.DataBind();
                    }
                }
            }
        }

相關文章