2012-06-13 26 views
4

我認爲最簡單的答案是你不能,但必須有一定的方法才能實現。我想列出一個項目列表,然後列出其中的項目列表,只嵌套一個級別。即:如何在自己內部嵌套一個ASP.Net用戶控件?

Repeater 
    UserControl1 
     UserControl1 
     UserControl1 
    UserControl1 
     UserControl1 
    UserControl1 
    UserControl1 

我真的想避免,如果在所有可能的使用LoadControl因爲我加入到這個列表的服務器端的Click事件裏面,所以我不能做PageInit loadControl讓所有的視圖狀態東西上班。

我要去嘗試寫出一個快速的僞代碼示例,它看起來像這樣:

Page.aspx

<asp:repeater runat="Server" id="someRepeater"> 
<uc:UserControl1 runat="Server" id="ctrlParent" /> 
</asp:repeater> 

UserControl1.ascx

<asp:label id="label1" runat="server" /> 
<asp:repeater runat="server" id="childRepeater"> 
    <uc:UserControl1 runat="server" id="ctrlChild" /> 
</asp:repeater> 

UserControl1.ascx.vb

If me.HasChildren then 
    'BindChildRepeater' 
end if 

sub Fill(Data as RelevantData) 
    label1.Text = Data.SomeText 
end sub 

sub ChildRepeater_ItemDataBound(object as sender, e as someArgs) 
    Dim childCtrl = e.item.findcontrol("ctrlChild") 
    childCtrl.Fill(e.item.dataitem) 
end sub 
+0

你可以有一個_different instance_嵌套。 – Oded

+0

如果你把它放在自身內部,它會給你一個循環引用錯誤。我甚至試圖將用戶控件放在一個新的UserControl中......即:UserControl1Child,它不過是父項的容器UserControl。它也給我一個循環的參考錯誤。將它們放在不同的目錄中。 –

+1

沒有看到你的代碼,也不知道用戶控件是如何實現的,所以不可能完全回答這個問題。 – Oded

回答

2

您可以嵌套一個轉發器

<asp:repeater runat="Server" id="someRepeater1"> 
    <uc:UserControl1 runat="Server" id="ctrlParent1" /> 
    <asp:repeater runat="Server" id="someRepeater2"> 
     <uc:UserControl1 runat="Server" id="ctrlParent2" /> 
    </asp:repeater> 
</asp:repeater> 
+0

這樣做,如果有一些方法讓實際的用戶控件成爲其父項中的子項目控制層次結構,並且在中繼器中沒有子項目列表。 –

0

只需撥打在你要撥打的另一個用戶控制用戶的控逆變.. <%@ Register src="" tagname="" tagprefix=""%>裏面調用另一個用戶控件的用戶控件同樣喜歡叫正常的aspx頁面的用戶控件。

+0

在同一個用戶控件中不起作用 –

+0

發佈確切的源代碼,我們不能在沒有粘土的情況下構建磚塊 – user721

0

這裏面一箇中繼器是我做的。基本上控制您添加使用LoadControl會回傳後消失了,所以我所做的是簡單的調用這個函數回傳過程中Page_Load

Private Sub AddSubCategorizedQuestionItemControls() 

    If rptSubCategorizedquestionList.Items IsNot Nothing AndAlso 
     rptSubCategorizedquestionList.Items.Count > 0 Then 

     For Each _item As RepeaterItem In rptSubCategorizedquestionList.Items 

      If _item.ItemType = ListItemType.AlternatingItem OrElse 
        _item.ItemType = ListItemType.Item Then 

       Dim _hfCategoryID As HiddenField = _item.FindControl("hfCategoryID") 

       Dim _placeHolderSubCategoryQuestionItem As PlaceHolder = _item.FindControl("placeHolderSubCategoryQuestionItem") 

       Dim _nestedCategorizedListItemControlUserControlObject As UserControl = 
                   LoadControl("__categorizedQuestionListItem.ascx") 

       _nestedCategorizedListItemControlUserControlObject.ID = 
             String.Format("ucCategorizedQuestionListItem", _hfCategoryID.Value) 

       _placeHolderSubCategoryQuestionItem.Controls.Add(_nestedCategorizedListItemControlUserControlObject) 

       Dim _nestedCategorizedListItem As Common_Questions__categorizedQuestionListItem = 
        DirectCast(_nestedCategorizedListItemControlUserControlObject, Common_Questions__categorizedQuestionListItem) 

      End If 

     Next 

    End If 

End Sub 
相關問題