Hi,


I've been struggling with this for hours now, and I feel I'm getting
nowhere.  Maybe one of you knows the answer.



In a nutshell, I want to put a templated control inside a FormView, and be
able to use two-way binding on controls inside the templated control.



Here's some pseudo code to show you what I'm trying to achive:



<asp:FormView DataSourceID="..." ...>

                <ItemTemplate>

                                <local:MyTemplateControl runat="server" ...>

                                                <Contents>

                                                                Name:

                                                                <asp:TextBox
runat="server" Text='<%# Bind ("Name") %>' ... />

                                                </Contents>

                                </local:MyTemplateControl>

                </ItemTemplate>

</asp:FormView>



Get the picture?  There's a templated control inside the FormView's
ItemTemplate, and there's two-way databinding between the TextBox and the
FormView.



This seems to be a problem.  I've been looking at the generated code (it
hasn't been a fun day) and the ExtractValues method that's automatically
created for the FormView's ItemTemplate never refers to controls in my
nested templated control.  (Moving the TextBox above outside the
MyTemplateControl makes the TextBox visible to the generated ExtractValues
method.)



So, what magical incantation do I need to do to make the nested templated
control 'transparent' to the FormView, so that it can pick up data bound
controls inside the templated control.  Or how can I make the templated
control propagate the values from its own ExtractValues call.



I thought it would just be a simple matter of marking the template
properties with:

                [TemplateContainer (typeof (MyTemplateControl),
BindingDirection.TwoWay)]



but that doesn't work.



Really, I just want a simple templated control that has no impact on
databinding.  It looks like it has to be a data-bound control though because
if it's just an INamingContainer then data-bound expressions give the error:



CS1061: 'OpinionatedGeek.MyTemplateControl ' does not contain a definition
for 'DataItem' and no extension method 'DataItem' accepting a first argument
of type 'OpinionatedGeek.MyTemplateControl ' could be found (are you missing
a using directive or an assembly reference?)



I don't even particularly want it to be an INamingContainer either, but
templated controls need to be INamingContainers, don't they?



Now that I've typed all that, I think really all I want is a templated
control that can be in a FormView (or other data bound control) but doesn't
have to be, that is  transparent to data-bound expressions - the expressions
talk to the parent BindingContainer as if the templated control wasn't
there.



Any idea how I do that?  I've reduced my templated control down to a very
simple state and posted the code below so you can see what I've done.



I'd really appreciate any help on this.



Cheers,



                                                Geoff



using System;

using System.ComponentModel;

using System.Web.UI;

using System.Web.UI.WebControls;



namespace OpinionatedGeek

{

    [ParseChildren (true)]

    [PersistChildren (true)]

    public class MyTemplateControl : Panel

    {

        //============================================================

        // Private Data

        //============================================================



        private ITemplate _header = null;

        private ITemplate _item = null;

        private ITemplate _footer = null;



        private PlaceHolder _headerHolder = null;

        private PlaceHolder _itemHolder = null;

        private PlaceHolder _footerHolder = null;



        //============================================================

        // Constructors

        //============================================================



        public MyTemplateControl ()

        {

            return;

        }



        //============================================================

        // Properties

        //============================================================



        [DefaultValue (null)]

        [PersistenceMode (PersistenceMode.InnerProperty)]

        [TemplateContainer (typeof (IDataItemContainer),
BindingDirection.TwoWay)]

        public ITemplate HeaderTemplate

        {

            get

            {

                return _header;

            }

            set

            {

                _header = value;



                return;

            }

        }



        [DefaultValue (null)]

        [PersistenceMode (PersistenceMode.InnerProperty)]

        [TemplateContainer (typeof (IDataItemContainer),
BindingDirection.TwoWay)]

        public ITemplate ItemTemplate

        {

            get

            {

                return _item;

            }

            set

            {

                _item = value;



                return;

            }

        }



        [DefaultValue (null)]

        [PersistenceMode (PersistenceMode.InnerProperty)]

        [TemplateContainer (typeof (IDataItemContainer),
BindingDirection.TwoWay)]

        public ITemplate FooterTemplate

        {

            get

            {

                return _footer;

            }

            set

            {

                _footer = value;



                return;

            }

        }



        //============================================================

        // Events

        //============================================================



        protected override void OnInit (EventArgs e)

        {

            base.OnInit (e);



            EnsureID ();



            _headerHolder = new PlaceHolder ();

            Controls.Add (_headerHolder);



            _itemHolder = new PlaceHolder ();

            Controls.Add (_itemHolder);



            _footerHolder = new PlaceHolder ();

            Controls.Add (_footerHolder);



            if (_header != null)

            {

                _header.InstantiateIn (_headerHolder);

            }



            if (_item != null)

            {

                _item.InstantiateIn (_itemHolder);

            }



            if (_footer != null)

            {

                _footer.InstantiateIn (_footerHolder);

            }



            EnsureChildControls ();



            return;

        }



        //============================================================

        // Methods

        //============================================================

    }

}








===================================
This list is hosted by DevelopMentorĀ®  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com

Reply via email to