Re: Template data is not being replaced in a Templated ASP.NET server control...

Adam Sills <[email protected]>
Newsgroups gmane.comp.windows.devel.dotnet.web
Message-ID <[email protected]>
Actually I just went through reflector to see what's different.

System.Web.UI.WebControls.WebControl's DataBind method goes through all
child controls and calls DataBind on them.
System.Web.UI.WebControls.CompositeControl's DataBind method first calls
EnsureChildControls, then calls WebControl's DataBind method.

Since your ScannerControl is creating its children in CreateChildControls,
calling DataBind on it if it inherits from WebControl does not guarantee
that all child controls are created first. By switching it to inherit from
CompositeControl that guarantees that all children are created before the
actual data bind.

HTH

Adam..

-----Original Message-----
From: Adam Sills [mailto:[email protected]]
Sent: Thursday, March 29, 2007 4:33 PM
To: 'Discussion of building .NET applications targeted for the Web'
Subject: RE: [DOTNET-WEB] Template data is not being replaced in a Templated
ASP.NET server control...

public partial class _Default : System.Web.UI.Page {
                protected void Page_Load( object sender, EventArgs e ) {
                        this.ScannerControl1_1.DataBind();
                }
        }

The data binding syntax (<%# xxx %>) only works when you've called DataBind
on *something*.

So leaving your code as-is (with it inheriting from WebControl), it will
databind but not call databind on all its children. Since it's a child that
has the data binding syntax, what's actually happening is the <%# %> section
of your ASP.NET markup is actually getting ignored and never evaluated. The
easy way to prove that is to put something in there that will return a
value.

<h1><%# Container.GetType().Name %></h1>

Change your ScannerControl to inherit from CompositeControl, recompile, and
you'll see the type name in there.

Adam..

-----Original Message-----
From: Discussion of building .NET applications targeted for the Web
[mailto:[email protected]] On Behalf Of Mike Andrews
Sent: Thursday, March 29, 2007 4:06 PM
To: [email protected]
Subject: Re: [DOTNET-WEB] Template data is not being replaced in a Templated
ASP.NET server control...

Thanks for the response Adam.

Would you be able to provide an example?
I'm not sure what you mean about the DataBind on the scanner control.

Thanks,
Mike


On 3/29/07, Adam Sills <[email protected]> wrote:
>
> You have two problems.
>
> 1. You're not calling DataBind on your scanner control
> 2. WebControl by itself does not loop through its child controls and call
> DataBind on them in its DataBind call. You need to do that yourself or
> inherit from CompositeControl instead.
>
> Adam..
>
> -----Original Message-----
> From: Discussion of building .NET applications targeted for the Web
> [mailto:[email protected]] On Behalf Of Mike Andrews
> Sent: Thursday, March 29, 2007 3:47 PM
> To: [email protected]
> Subject: [DOTNET-WEB] Template data is not being replaced in a Templated
> ASP.NET server control...
>
> Guys,
>
> I'm really lost now.  I cannot, I repeat cannot, get the data in my
> container control that represents the template to work.
> If you notice in my markup, I have this: <%# Container.Text %>.  When the
> template control is created (MyContainer) and I call the Instantiate, this
> value is not replaced with the message "The default container text!" as it
> should have been, but instead is replaced with nothing.
>
> Any help would be most appreciated.  I'm really at my wits end with this
> one.
>
> Thanks,
> Mike
>
> Here's my asp.net markup:
>
> <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs"
> Inherits="_Default" %>
> <%@ Register Assembly="Scanner" Namespace="Scanner" TagPrefix="cc1" %>
> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "
> http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
> <html xmlns="http://www.w3.org/1999/xhtml" >
> <head runat="server">
>    <title>Untitled Page</title>
> </head>
> <body>
>    <form id="form1" runat="server">
>    <div>
>        <cc1:ScannerControl id="ScannerControl1_1" runat="server">
>            <Template>
>                <h1><%# Container.Text %></h1>
>            </Template>
>        </cc1:ScannerControl>
> </div>
>    </form>
> </body>
> </html>
>
>
> Here's the result of the page when it's run:
>
> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "
> http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
> <html xmlns="http://www.w3.org/1999/xhtml" >
> <head><title>
> Untitled Page
> </title></head>
> <body>
>    <form name="form1" method="post" action="Default.aspx" id="form1">
> <div>
> <input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE"
> value="/wEPDwUJNTgwMjcwMjU1ZGTpLl3HHZKhrXybhi5mD/cUEBCBwQ==" />
> </div>
>    <div>
>        <span id="ScannerControl1_1">
>                <h1></h1>
>            </span>
> </div>
>    </form>
> </body>
> </html>
>
>
> Here is a really scaled down version of my code:
>
>
>    [ToolboxData("<{0}:ScannerControl runat=server></{0}:ScannerControl>")]
>    public class ScannerControl : WebControl {
>        private ITemplate _template;
>        [TemplateContainer(typeof(MyContainer))]
>        [PersistenceMode(PersistenceMode.InnerProperty)]
>        public ITemplate Template {
>            get { return _template; }
>            set { _template = value; }
>        }
>
>        protected override void CreateChildControls() {
>            this.Controls.Clear();
>            if (this.Template != null) {
>                MyContainer mc = new MyContainer("The default container
> text!");
>                this.Template.InstantiateIn(mc);
>                this.Controls.Add(mc);
>            }
>            base.CreateChildControls();
>        }
>    }
>
>    public class MyContainer : Control, INamingContainer {
>        public MyContainer(string text) {
>            this.Text = text;
>        }
>
>        private string _text;
>        public string Text {
>            get { return _text; }
>            set { _text = value; }
>        }
>    }
>
> ===================================
> This list is hosted by DevelopMentorR  http://www.develop.com
>
> View archives and manage your subscription(s) at
> http://discuss.develop.com
>
> ===================================
> This list is hosted by DevelopMentor(r)  http://www.develop.com
>
> View archives and manage your subscription(s) at
> http://discuss.develop.com
>

===================================
This list is hosted by DevelopMentorR  http://www.develop.com

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

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

View archives and manage your subscription(s) at http://discuss.develop.com
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.