Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / Building Controls / June 2004

Tip: Looking for answers? Try searching our database.

Add a usercontrol to itself declaratively?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Jos - 23 Jun 2004 16:52 GMT
Can you add a user control recursively to itself?

Imagine some kind of tree control, where every node is again a tree control.

Each node is populated with a DataList containing the child nodes.

I manage to add additional instances of the user control to the
item template in the ItemDataBound handler, like this:

   Sub DataList1_ItemDataBound(sender As Object, e As
DataListItemEventArgs)
       If(e.Item.ItemType=ListItemType.Item Or
e.Item.ItemType=ListItemType.AlternatingItem) Then
           Dim testControl1 As New TestControl()
           e.Item.Controls.Add(testControl1)
           testControl1.BindDataList1()
       End If
   End Sub

In order to have a class name, I use this Control directive:
<%@ Control Language="VB" ClassName="TestControl" %>

This works perfectly.

But my question is: can I add the user control to the ItemTemplate
declaratively instead of programmatically, like this:

   <asp:DataList id="DataList1" OnItemDataBound="DataList1_ItemDataBound"
runat="server">
       <ItemTemplate>
            <TestControl id="testControl1" runat="server" />
       </ItemTemplate>
   </asp:DataList>

This still gives me no error, but now I need to refer to the control for
binding.

I try to bind it like this:
   Sub DataList1_ItemDataBound(sender As Object, e As
DataListItemEventArgs)
       If(e.Item.ItemType=ListItemType.Item Or
e.Item.ItemType=ListItemType.AlternatingItem) Then
           Dim testControl1 As TestControl =
CType(e.Item.FindControl("testControl1"),TestControl)
           testControl1.BindDataList1()
       End If
   End Sub

I get the error: specified cast is not valid.

How can I get this cast to work?

Thanks for your help,

Jos
Victor Garcia Aprea [MVP] - 24 Jun 2004 06:17 GMT
Hi Jos,

>>>             Dim testControl1 As TestControl =
>>> CType(e.Item.FindControl("testControl1"),TestControl)

Please debug your code and check what is the FindControl call returning...
that should give you a good clue of what may be wrong,

Signature

Victor Garcia Aprea
Microsoft MVP | ASP.NET
Looking for insights on ASP.NET? Read my blog:
http://obies.com/vga/blog.aspx

a) If you're looking

> Can you add a user control recursively to itself?
>
[quoted text clipped - 51 lines]
>
> Jos
Jos - 26 Jun 2004 20:14 GMT
> Hi Jos,
>
[quoted text clipped - 3 lines]
> Please debug your code and check what is the FindControl call
> returning... that should give you a good clue of what may be wrong,

It returns "HtmlGenericControl".

I guess what's wrong is that ASP.NET can't handle the tag
properly, because it is recursive.

Signature

Jos


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.