How can you embed ASP.Net user control inside you?

I think the short answer is that you cannot, but there must be something to be done to make this happen. I want to make a list of elements, which then have a list of elements inside them, nested only one level. i.e:

Repeater UserControl1 UserControl1 UserControl1 UserControl1 UserControl1 UserControl1 UserControl1 

I would really like to avoid using LoadControl, if at all possible, since I am adding to this list inside the server side. Click events, so I cannot execute loadControl on the Init page to get all viewstate properties to work.

I will try to write a quick example psuedo code, it looks like this:

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 
+4
source share
3 answers

You can insert a repeater inside the repeater

 <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> 
+2
source

Just call <%@ Register src="" tagname="" tagprefix=""%> in the user loop in which you want to call another user control. Calling a user control inside another user control is similar to calling a user control on a regular aspx page.

0
source

This is what I did. Basically, the controls you add with LoadControl will disappear after the postback, so I just called this function during postback in 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 
0
source

All Articles