Add a usercontrol to itself declaratively?

Discussion in 'ASP .Net Building Controls' started by Jos, Jun 23, 2004.

  1. Jos

    Jos Guest

    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
     
    Jos, Jun 23, 2004
    #1
    1. Advertising

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

    --
    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" <> wrote in message
    news:%...
    > 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], Jun 24, 2004
    #2
    1. Advertising

  3. Jos

    Jos Guest

    Victor Garcia Aprea [MVP] wrote:
    > 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,


    It returns "HtmlGenericControl".

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

    --

    Jos
     
    Jos, Jun 26, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?UmljaGFyZCBCcm93bg==?=

    Set Custom Page properties declaratively

    =?Utf-8?B?UmljaGFyZCBCcm93bg==?=, Dec 2, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    417
    =?Utf-8?B?UmljaGFyZCBCcm93bg==?=
    Dec 2, 2005
  2. =?Utf-8?B?d2FsdGVy?=

    How to declaratively set page property in aspx file?

    =?Utf-8?B?d2FsdGVy?=, Mar 28, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    556
    =?Utf-8?B?d2FsdGVy?=
    Mar 29, 2006
  3. =?Utf-8?B?RGFiYmxlcg==?=

    datasource cannot be set declaratively

    =?Utf-8?B?RGFiYmxlcg==?=, Apr 3, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    3,155
    =?Utf-8?B?RGFiYmxlcg==?=
    Apr 3, 2006
  4. Maciek Zywno
    Replies:
    4
    Views:
    403
    Maciek Zywno
    Apr 30, 2004
  5. papaja
    Replies:
    0
    Views:
    694
    papaja
    Jul 3, 2006
Loading...

Share This Page