Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Building Controls > Add a usercontrol to itself declaratively?

Reply
Thread Tools

Add a usercontrol to itself declaratively?

 
 
Jos
Guest
Posts: n/a
 
      06-23-2004
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:

<aspataList id="DataList1" OnItemDataBound="DataList1_ItemDataBound"
runat="server">
<ItemTemplate>
<TestControl id="testControl1" runat="server" />
</ItemTemplate>
</aspataList>

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"),TestContr ol)
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




 
Reply With Quote
 
 
 
 
Victor Garcia Aprea [MVP]
Guest
Posts: n/a
 
      06-24-2004
Hi Jos,

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


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

--
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
"Jos" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> 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:
>
> <aspataList id="DataList1" OnItemDataBound="DataList1_ItemDataBound"
> runat="server">
> <ItemTemplate>
> <TestControl id="testControl1" runat="server" />
> </ItemTemplate>
> </aspataList>
>
> 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"),TestContr ol)
> 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
>
>
>
>



 
Reply With Quote
 
 
 
 
Jos
Guest
Posts: n/a
 
      06-26-2004
Victor Garcia Aprea [MVP] wrote:
> Hi Jos,
>
>>>> Dim testControl1 As TestControl =
>>>> CType(e.Item.FindControl("testControl1"),TestContr ol)

>
> 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.

--

Jos


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
obtain element name, or attribute and value of the document name itself, and some elemnts and attributes from an ancestor or the node itself using xquery Jeff Kish XML 4 10-30-2008 05:47 PM
accessing usercontrol from another usercontrol Phl ASP .Net 2 11-18-2004 07:33 PM
Access a control inside an usercontrol from another control inside another usercontrol nail ASP .Net 0 09-15-2004 03:55 PM
Can we use a usercontrol inside a usercontrol Rajesh Tiwari ASP .Net 0 08-12-2003 03:56 PM
Use LoadControl to load a usercontrol but the webcontrol in the usercontrol can not AutoPostBack huobazi ASP .Net 1 07-03-2003 03:14 AM



Advertisments