Custom Control, ITemplate and nested bound controls

Discussion in 'ASP .Net Web Controls' started by Brook, Mar 17, 2008.

  1. Brook

    Brook Guest

    Hi Everyone,

    I've been interested in creating a custom control that wraps up a few common
    UI elements and some styling. But, in order to make this earier to
    understand, let's say my control is just a posh panel. It has curvy edges
    rather than straight ones.

    Now i have written a custom control which allows me to do the following in
    aspx code :

    <cc1:CurvyPanel runat="server">
    <Content>
    <asp:Label runat="server" Text="Hello World!"></asp:Label>
    </Content>
    </cc1:CurvyPanel>

    The control is visible at runtime and design time.

    However, when i try something a little more adventurous such as:

    <cc1:CurvyPanel runat="server">
    <Content>
    <asp:Repeater ID="rptYears" runat="server">
    <ItemTemplate>
    <a href="<%#Eval("link") %>"><%# Eval("name")
    %></a>
    </ItemTemplate>
    </asp:Repeater>
    </Content>
    </cc1:CurbyPanel>

    Now, when i try to bind some data to my embedded repeater from my parent
    page, it gives me the error

    "InvalidOperationException was unhandled by user code
    Databinding methods such as Eval(), XPath(), and Bind() can only be used in
    the context of a databound control."

    So, I was hoping my "curvypanel" would behave in the same way as a normal
    ..NET panel in terms of allowing content inside it to be independantly bound
    to a set of data... so either it doesn't allow this or ive done something
    wrong.

    Can anyone help me by clarifying whether what im trying to do is possible?
    and if so, provide any input on how to go about it? :)

    thanks,
    Andrew
     
    Brook, Mar 17, 2008
    #1
    1. Advertising

  2. Hello Andrew

    > "InvalidOperationException was unhandled by user code
    > Databinding methods such as Eval(), XPath(), and Bind() can only be used
    > in
    > the context of a databound control."

    Look what google says about that error message.

    I guess, its for the reason, that your control doesnt represent a databound
    control.
    Anyway, that should work:

    <%#DataBinder.Eval(Container.DataItem, "field").ToString()%>

    --
    Gruss, Peter Bucher
    Microsoft MVP - Visual Developer ASP / ASP.NET, Switzerland
    http://www.aspnetzone.de/ - ASP.NET Zone, die ASP.NET Community
    http://www.aspnetzone.de/blogs/peterbucher/ - Auf den Spuren von .NET
     
    Peter Bucher [MVP], Mar 17, 2008
    #2
    1. Advertising

  3. Brook

    Brook Guest

    Thanks Peter - i made the changes and the control no longer throws the error.
    In fact the control works in every way i'd expect.

    The only thing that i wonder about now, is why my control is different to
    say a normal asp:panel. Does an asp:panel perhaps inherit from some classes
    that allow child controls to bind to their data sources using Eval?
    Afterall, i don't want to bind any data to my control, i want to bind to the
    child controls that get added into the controls ITemplate

    At the moment, my custom control is inheriting from WebControl, and
    implementing INamingContainer

    thanks,
    Andrew

    "Peter Bucher [MVP]" wrote:

    > Hello Andrew
    >
    > > "InvalidOperationException was unhandled by user code
    > > Databinding methods such as Eval(), XPath(), and Bind() can only be used
    > > in
    > > the context of a databound control."

    > Look what google says about that error message.
    >
    > I guess, its for the reason, that your control doesnt represent a databound
    > control.
    > Anyway, that should work:
    >
    > <%#DataBinder.Eval(Container.DataItem, "field").ToString()%>
    >
    > --
    > Gruss, Peter Bucher
    > Microsoft MVP - Visual Developer ASP / ASP.NET, Switzerland
    > http://www.aspnetzone.de/ - ASP.NET Zone, die ASP.NET Community
    > http://www.aspnetzone.de/blogs/peterbucher/ - Auf den Spuren von .NET
    >
    >
     
    Brook, Mar 18, 2008
    #3
  4. Brook

    Teemu Keiski Guest

    "Databinding methods such as Eval(), XPath(), and Bind() can only be used in
    the context of a databound control"

    Sounds familiar error to me. ;-)

    How's the template's instantiation in the control - regarding how it gets
    added to Controls collection? And is template container specified
    (TemplateContainer attribute)?

    --
    Teemu Keiski
    AspInsider, ASP.NET MVP
    http://blogs.aspadvice.com/joteke
    http://teemukeiski.net


    "Peter Bucher [MVP]" <> wrote in message
    news:...
    > Hello Andrew
    >
    >> "InvalidOperationException was unhandled by user code
    >> Databinding methods such as Eval(), XPath(), and Bind() can only be used
    >> in
    >> the context of a databound control."

    > Look what google says about that error message.
    >
    > I guess, its for the reason, that your control doesnt represent a
    > databound control.
    > Anyway, that should work:
    >
    > <%#DataBinder.Eval(Container.DataItem, "field").ToString()%>
    >
    > --
    > Gruss, Peter Bucher
    > Microsoft MVP - Visual Developer ASP / ASP.NET, Switzerland
    > http://www.aspnetzone.de/ - ASP.NET Zone, die ASP.NET Community
    > http://www.aspnetzone.de/blogs/peterbucher/ - Auf den Spuren von .NET
     
    Teemu Keiski, Mar 20, 2008
    #4
  5. Brook

    Brook Guest

    Hi - i think this is the code you're interested in seeing :-

    private ITemplate _PanelContent;

    private PlaceHolder phContent = new PlaceHolder();

    [PersistenceMode(PersistenceMode.InnerProperty),
    TemplateInstance(TemplateInstance.Single),
    TemplateContainer(typeof(TemplateControl))]
    public ITemplate Content
    {
    get { return _PanelContent; }
    set { _PanelContent = value; }
    }


    protected override void OnInit(EventArgs e)
    {
    base.OnInit(e);
    if (_PanelContent != null)
    {

    _PanelContent.InstantiateIn(phContent);
    }
    EnsureChildControls();
    }



    thanks,
    Andrew


    "Teemu Keiski" wrote:

    > "Databinding methods such as Eval(), XPath(), and Bind() can only be used in
    > the context of a databound control"
    >
    > Sounds familiar error to me. ;-)
    >
    > How's the template's instantiation in the control - regarding how it gets
    > added to Controls collection? And is template container specified
    > (TemplateContainer attribute)?
    >
    > --
    > Teemu Keiski
    > AspInsider, ASP.NET MVP
    > http://blogs.aspadvice.com/joteke
    > http://teemukeiski.net
    >
    >
    > "Peter Bucher [MVP]" <> wrote in message
    > news:...
    > > Hello Andrew
    > >
    > >> "InvalidOperationException was unhandled by user code
    > >> Databinding methods such as Eval(), XPath(), and Bind() can only be used
    > >> in
    > >> the context of a databound control."

    > > Look what google says about that error message.
    > >
    > > I guess, its for the reason, that your control doesnt represent a
    > > databound control.
    > > Anyway, that should work:
    > >
    > > <%#DataBinder.Eval(Container.DataItem, "field").ToString()%>
    > >
    > > --
    > > Gruss, Peter Bucher
    > > Microsoft MVP - Visual Developer ASP / ASP.NET, Switzerland
    > > http://www.aspnetzone.de/ - ASP.NET Zone, die ASP.NET Community
    > > http://www.aspnetzone.de/blogs/peterbucher/ - Auf den Spuren von .NET

    >
    >
    >
     
    Brook, Mar 25, 2008
    #5
    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. cosine... zero
    Replies:
    0
    Views:
    695
    cosine... zero
    Jun 9, 2004
  2. Mario Krsnic
    Replies:
    0
    Views:
    400
    Mario Krsnic
    Jun 23, 2006
  3. LaptopHeaven
    Replies:
    0
    Views:
    425
    LaptopHeaven
    Oct 5, 2006
  4. cindy
    Replies:
    0
    Views:
    817
    cindy
    Dec 9, 2009
  5. cindy
    Replies:
    0
    Views:
    789
    cindy
    Dec 9, 2009
Loading...

Share This Page