Discussion:
Template data is not being replaced in a Templated ASP.NET server control...
Mike Andrews
2007-03-29 20:47:00 UTC
Permalink
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 DevelopMentor® http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com
Adam Sills
2007-03-29 21:01:41 UTC
Permalink
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:DOTNET-***@DISCUSS.DEVELOP.COM] On Behalf Of Mike Andrews
Sent: Thursday, March 29, 2007 3:47 PM
To: DOTNET-***@DISCUSS.DEVELOP.COM
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� http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com
Mike Andrews
2007-03-29 21:06:00 UTC
Permalink
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
Post by Adam Sills
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
Sent: Thursday, March 29, 2007 3:47 PM
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
Inherits="_Default" %>
<!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>
<!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>
[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 DevelopMentor® http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com
Adam Sills
2007-03-29 21:33:29 UTC
Permalink
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:DOTNET-***@DISCUSS.DEVELOP.COM] On Behalf Of Mike Andrews
Sent: Thursday, March 29, 2007 4:06 PM
To: DOTNET-***@DISCUSS.DEVELOP.COM
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
Post by Adam Sills
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
Sent: Thursday, March 29, 2007 3:47 PM
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
Inherits="_Default" %>
<!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>
<!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>
[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
Adam Sills
2007-03-29 21:35:51 UTC
Permalink
Your code modified. Note all I've done is change what's in the <%# %> block
and make ScannerControl inherit from CompositeControl.

.aspx:

<cc1:ScannerControl id="ScannerControl1_1" runat="server">
<Template>
<h1><%# Container.GetType().Name %></h1>
</Template>
</cc1:ScannerControl>

Scanner control:

[ToolboxData( "<{0}:ScannerControl runat=server></{0}:ScannerControl>" )]
public class ScannerControl : CompositeControl {
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.Controls.Add( mc );
this.Template.InstantiateIn( mc );
}
base.CreateChildControls();
}
}

-----Original Message-----
From: Discussion of building .NET applications targeted for the Web
[mailto:DOTNET-***@DISCUSS.DEVELOP.COM] On Behalf Of Mike Andrews
Sent: Thursday, March 29, 2007 4:06 PM
To: DOTNET-***@DISCUSS.DEVELOP.COM
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
Post by Adam Sills
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
Sent: Thursday, March 29, 2007 3:47 PM
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
Inherits="_Default" %>
<!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>
<!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>
[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
Adam Sills
2007-03-30 21:25:06 UTC
Permalink
I wouldn't recommend doing that, even though it works. I would personally
recommend anytime you use databinding syntax you force the page owner to
call your DataBind method (or just Page.DataBind).

If you are insistent on doing automatic data binding, you can override
OnInit or OnLoad (provided they're not doing anything else) and then call
DataBind from there.

Adam..

-----Original Message-----
From: Discussion of building .NET applications targeted for the Web
[mailto:DOTNET-***@DISCUSS.DEVELOP.COM] On Behalf Of Mike Andrews
Sent: Friday, March 30, 2007 3:25 PM
To: DOTNET-***@DISCUSS.DEVELOP.COM
Subject: Re: [DOTNET-WEB] Template data is not being replaced in a Templated
ASP.NET server control...

Adam,

I've figured something else out now.
Due to the complex nature of this particular control I've had to call
DataBind() within the control itself.
If I call DataBind() in the Render() method, then I don't need to call in on
the page.
Also, I implemented the IPostBackEventHandler method for the control and
called DataBind() in it as well.
The control does work now with the complex behavior and works as expected.

Thanks again for your help,
Mike
Did you forget to put runat="server" on the control?
-----Original Message-----
From: Discussion of building .NET applications targeted for the Web
Sent: Friday, March 30, 2007 1:26 PM
Subject: Re: [DOTNET-WEB] Template data is not being replaced in a Templated
ASP.NET server control...
I have yet another question regarding this...
I'm almost working now...
However, when I put an asp control inside of the template, the markup is
sending the asp:xxx control out to the page instead of the rendered html.
Any suggestions?
Thanks,
Mike
Yes.
But like I also said, I had to add a call to DataBind() in my codebehind
page.
Here's all the code, works great.
Default.aspx.cs"
Inherits="WebApplication1._Default" %>
TagPrefix="cc1" %>
<html xmlns="http://www.w3.org/1999/xhtml" > <head id="Head1"
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>
namespace WebApplication1 {
public partial class _Default : System.Web.UI.Page {
protected void Page_Load( object sender, EventArgs e ) {
this.ScannerControl1_1.DataBind();
}
}
}
namespace WebApplication1 {
public class MyContainer : Control, INamingContainer {
public MyContainer( string text ) {
this.Text = text;
}
private string _text;
public string Text {
get { return _text; }
set { _text = value; }
}
}
}
namespace WebApplication1 {
[ToolboxData( "<{0}:ScannerControl
runat=server></{0}:ScannerControl>" )]
public class ScannerControl : CompositeControl {
private ITemplate _template;
[TemplateContainer( typeof( MyContainer ) )]
[PersistenceMode( PersistenceMode.InnerProperty )]
public ITemplate Template {
get { return _template; }
set { _template = value; }
}
public override void DataBind() {
base.DataBind();
}
protected override void CreateChildControls() {
this.Controls.Clear();
if( this.Template != null ) {
MyContainer mc = new MyContainer( "The
default container text!" );
this.Controls.Add( mc );
this.Template.InstantiateIn( mc );
}
base.CreateChildControls();
}
}
}
<!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 id="Head1"><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="/wEPDwULLTE4NzY2NjU3OTEPZBYCAgMPZBYCAgEPZBYCZg9kFgJmDxUBG1RoZSBkZWZhd
Wx0IGNvbnRhaW5lciB0ZXh0IWRkSSdc9mE3KlCrYB94vl7I3Cok4f8=" />
</div>
<div>
<span id="ScannerControl1_1">
<h1>The default container text!</h1>
</span>
</div>
</form>
</body>
</html>
-----Original Message-----
From: Discussion of building .NET applications targeted for the Web
Sent: Friday, March 30, 2007 9:45 AM
Subject: Re: [DOTNET-WEB] Template data is not being replaced in a Templated
ASP.NET server control...
Adam,
Thank you for your replies.
However, I cannot get any of your examples to work. I still the get
same
effect as before.
One of the things I specifically tried was the <%# Container.GetType
().Name
%> and it worked as before.
It's replaced with nothing instead of the name of the object.
Were you able to get them to work in your environment?
Thanks,
Mike
Post by Adam Sills
Your code modified. Note all I've done is change what's in the <%# %> block
and make ScannerControl inherit from CompositeControl.
<cc1:ScannerControl id="ScannerControl1_1" runat="server">
<Template>
<h1><%# Container.GetType().Name %></h1>
</Template>
</cc1:ScannerControl>
[ToolboxData( "<{0}:ScannerControl runat=server></{0}:ScannerControl>"
)]
Post by Adam Sills
public class ScannerControl : CompositeControl {
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.Controls.Add( mc );
this.Template.InstantiateIn( mc );
}
base.CreateChildControls();
}
}
-----Original Message-----
From: Discussion of building .NET applications targeted for the Web
Sent: Thursday, March 29, 2007 4:06 PM
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
Post by Adam Sills
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
Post by Adam Sills
DataBind on them in its DataBind call. You need to do that yourself
or
Post by Adam Sills
Post by Adam Sills
inherit from CompositeControl instead.
Adam..
-----Original Message-----
From: Discussion of building .NET applications targeted for the Web
Sent: Thursday, March 29, 2007 3:47 PM
Subject: [DOTNET-WEB] Template data is not being replaced in a
Templated
Post by Adam Sills
Post by Adam Sills
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
Post by Adam Sills
template control is created (MyContainer) and I call the
Instantiate,
Post by Adam Sills
this
Post by Adam Sills
value is not replaced with the message "The default container text!"
as
Post by Adam Sills
it
Post by Adam Sills
should have been, but instead is replaced with nothing.
Any help would be most appreciated. I'm really at my wits end with
this
Post by Adam Sills
Post by Adam Sills
one.
Thanks,
Mike
Default.aspx.cs
Post by Adam Sills
"
Post by Adam Sills
Inherits="_Default" %>
%>
Post by Adam Sills
Post by Adam Sills
<!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>
<!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">
Post by Adam Sills
Post by Adam Sills
<div>
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE"
value="/wEPDwUJNTgwMjcwMjU1ZGTpLl3HHZKhrXybhi5mD/cUEBCBwQ==" />
</div>
<div>
<span id="ScannerControl1_1">
<h1></h1>
</span>
</div>
</form>
</body>
</html>
[ToolboxData("<{0}:ScannerControl
runat=server></{0}:ScannerControl>")]
Post by Adam Sills
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
Post by Adam Sills
Post by Adam Sills
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(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(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(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
Mike Andrews
2007-04-04 20:57:21 UTC
Permalink
Adam,

I decided to go ahead and call the DataBind() method in the Render method.
This particular control doesn't provide any functionality at all for binding
to a datasource of any kind. It's a wrapper for an ActiveX control and for
several modes of viewing; I render the control completely myself based on
properties. I'm also building lots of dynamic javascript and use the
__doPostBack method for using the IPostBackEventHandler interface. The
templates are to let the UI programmer to customize the interface but keep
the previously built Javascript methods working.

Thanks,
Mike
Post by Adam Sills
I wouldn't recommend doing that, even though it works. I would personally
recommend anytime you use databinding syntax you force the page owner to
call your DataBind method (or just Page.DataBind).
If you are insistent on doing automatic data binding, you can override
OnInit or OnLoad (provided they're not doing anything else) and then call
DataBind from there.
Adam..
-----Original Message-----
From: Discussion of building .NET applications targeted for the Web
Sent: Friday, March 30, 2007 3:25 PM
Subject: Re: [DOTNET-WEB] Template data is not being replaced in a Templated
ASP.NET server control...
Adam,
I've figured something else out now.
Due to the complex nature of this particular control I've had to call
DataBind() within the control itself.
If I call DataBind() in the Render() method, then I don't need to call in on
the page.
Also, I implemented the IPostBackEventHandler method for the control and
called DataBind() in it as well.
The control does work now with the complex behavior and works as expected.
Thanks again for your help,
Mike
Did you forget to put runat="server" on the control?
-----Original Message-----
From: Discussion of building .NET applications targeted for the Web
Sent: Friday, March 30, 2007 1:26 PM
Subject: Re: [DOTNET-WEB] Template data is not being replaced in a Templated
ASP.NET server control...
I have yet another question regarding this...
I'm almost working now...
However, when I put an asp control inside of the template, the markup is
sending the asp:xxx control out to the page instead of the rendered
html.
Any suggestions?
Thanks,
Mike
Yes.
But like I also said, I had to add a call to DataBind() in my
codebehind
page.
Here's all the code, works great.
Default.aspx.cs"
Inherits="WebApplication1._Default" %>
TagPrefix="cc1" %>
<html xmlns="http://www.w3.org/1999/xhtml" > <head id="Head1"
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>
namespace WebApplication1 {
public partial class _Default : System.Web.UI.Page {
protected void Page_Load( object sender, EventArgs e )
{
this.ScannerControl1_1.DataBind();
}
}
}
namespace WebApplication1 {
public class MyContainer : Control, INamingContainer {
public MyContainer( string text ) {
this.Text = text;
}
private string _text;
public string Text {
get { return _text; }
set { _text = value; }
}
}
}
namespace WebApplication1 {
[ToolboxData( "<{0}:ScannerControl
runat=server></{0}:ScannerControl>" )]
public class ScannerControl : CompositeControl {
private ITemplate _template;
[TemplateContainer( typeof( MyContainer ) )]
[PersistenceMode( PersistenceMode.InnerProperty )]
public ITemplate Template {
get { return _template; }
set { _template = value; }
}
public override void DataBind() {
base.DataBind();
}
protected override void CreateChildControls() {
this.Controls.Clear();
if( this.Template != null ) {
MyContainer mc = new MyContainer( "The
default container text!" );
this.Controls.Add( mc );
this.Template.InstantiateIn( mc );
}
base.CreateChildControls();
}
}
}
<!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 id="Head1"><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="/wEPDwULLTE4NzY2NjU3OTEPZBYCAgMPZBYCAgEPZBYCZg9kFgJmDxUBG1RoZSBkZWZhd
Wx0IGNvbnRhaW5lciB0ZXh0IWRkSSdc9mE3KlCrYB94vl7I3Cok4f8=" />
</div>
<div>
<span id="ScannerControl1_1">
<h1>The default container text!</h1>
</span>
</div>
</form>
</body>
</html>
-----Original Message-----
From: Discussion of building .NET applications targeted for the Web
Sent: Friday, March 30, 2007 9:45 AM
Subject: Re: [DOTNET-WEB] Template data is not being replaced in a Templated
ASP.NET server control...
Adam,
Thank you for your replies.
However, I cannot get any of your examples to work. I still the get
same
effect as before.
One of the things I specifically tried was the <%# Container.GetType
().Name
%> and it worked as before.
It's replaced with nothing instead of the name of the object.
Were you able to get them to work in your environment?
Thanks,
Mike
Post by Adam Sills
Your code modified. Note all I've done is change what's in the <%#
%>
Post by Adam Sills
block
and make ScannerControl inherit from CompositeControl.
<cc1:ScannerControl id="ScannerControl1_1" runat="server">
<Template>
<h1><%# Container.GetType().Name %></h1>
</Template>
</cc1:ScannerControl>
[ToolboxData( "<{0}:ScannerControl
runat=server></{0}:ScannerControl>"
)]
Post by Adam Sills
public class ScannerControl : CompositeControl {
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
Post by Adam Sills
container text!" );
this.Controls.Add( mc );
this.Template.InstantiateIn( mc );
}
base.CreateChildControls();
}
}
-----Original Message-----
From: Discussion of building .NET applications targeted for the Web
Sent: Thursday, March 29, 2007 4:06 PM
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.
Post by Adam Sills
Thanks,
Mike
Post by Adam Sills
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
Post by Adam Sills
call
Post by Adam Sills
DataBind on them in its DataBind call. You need to do that
yourself
or
Post by Adam Sills
Post by Adam Sills
inherit from CompositeControl instead.
Adam..
-----Original Message-----
From: Discussion of building .NET applications targeted for the
Web
Post by Adam Sills
Post by Adam Sills
Sent: Thursday, March 29, 2007 3:47 PM
Subject: [DOTNET-WEB] Template data is not being replaced in a
Templated
Post by Adam Sills
Post by Adam Sills
ASP.NET server control...
Guys,
I'm really lost now. I cannot, I repeat cannot, get the data in
my
Post by Adam Sills
Post by Adam Sills
container control that represents the template to work.
If you notice in my markup, I have this: <%# Container.Text%
. When
Post by Adam Sills
the
Post by Adam Sills
template control is created (MyContainer) and I call the
Instantiate,
Post by Adam Sills
this
Post by Adam Sills
value is not replaced with the message "The default container
text!"
as
Post by Adam Sills
it
Post by Adam Sills
should have been, but instead is replaced with nothing.
Any help would be most appreciated. I'm really at my wits end
with
this
Post by Adam Sills
Post by Adam Sills
one.
Thanks,
Mike
Default.aspx.cs
Post by Adam Sills
"
Post by Adam Sills
Inherits="_Default" %>
TagPrefix="cc1"
%>
Post by Adam Sills
Post by Adam Sills
<!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>
<!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">
Post by Adam Sills
Post by Adam Sills
<div>
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE"
value="/wEPDwUJNTgwMjcwMjU1ZGTpLl3HHZKhrXybhi5mD/cUEBCBwQ==" />
</div>
<div>
<span id="ScannerControl1_1">
<h1></h1>
</span>
</div>
</form>
</body>
</html>
[ToolboxData("<{0}:ScannerControl
runat=server></{0}:ScannerControl>")]
Post by Adam Sills
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
Post by Adam Sills
Post by Adam Sills
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(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(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(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(r) 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
Mike Andrews
2007-03-30 14:44:58 UTC
Permalink
Adam,

Thank you for your replies.

However, I cannot get any of your examples to work. I still the get same
effect as before.
One of the things I specifically tried was the <%# Container.GetType().Name
%> and it worked as before.
It's replaced with nothing instead of the name of the object.

Were you able to get them to work in your environment?

Thanks,
Mike
Post by Adam Sills
Your code modified. Note all I've done is change what's in the <%# %> block
and make ScannerControl inherit from CompositeControl.
<cc1:ScannerControl id="ScannerControl1_1" runat="server">
<Template>
<h1><%# Container.GetType().Name %></h1>
</Template>
</cc1:ScannerControl>
[ToolboxData( "<{0}:ScannerControl runat=server></{0}:ScannerControl>" )]
public class ScannerControl : CompositeControl {
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.Controls.Add( mc );
this.Template.InstantiateIn( mc );
}
base.CreateChildControls();
}
}
-----Original Message-----
From: Discussion of building .NET applications targeted for the Web
Sent: Thursday, March 29, 2007 4:06 PM
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
Post by Adam Sills
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
Post by Adam Sills
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
Sent: Thursday, March 29, 2007 3:47 PM
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
Post by Adam Sills
template control is created (MyContainer) and I call the Instantiate,
this
Post by Adam Sills
value is not replaced with the message "The default container text!" as
it
Post by Adam Sills
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
"
Post by Adam Sills
Inherits="_Default" %>
<!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>
<!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>
[ToolboxData("<{0}:ScannerControl
runat=server></{0}:ScannerControl>")]
Post by Adam Sills
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(r) 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
Adam Sills
2007-03-30 15:04:34 UTC
Permalink
Yes.

But like I also said, I had to add a call to DataBind() in my codebehind
page.

Here's all the code, works great.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs"
Inherits="WebApplication1._Default" %>
<%@ Register Assembly="WebApplication1" Namespace="WebApplication1"
TagPrefix="cc1" %>
<html xmlns="http://www.w3.org/1999/xhtml" > <head id="Head1"
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>

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

namespace WebApplication1 {
public class MyContainer : Control, INamingContainer {
public MyContainer( string text ) {
this.Text = text;
}

private string _text;
public string Text {
get { return _text; }
set { _text = value; }
}
}

}

namespace WebApplication1 {
[ToolboxData( "<{0}:ScannerControl
runat=server></{0}:ScannerControl>" )]
public class ScannerControl : CompositeControl {
private ITemplate _template;

[TemplateContainer( typeof( MyContainer ) )]
[PersistenceMode( PersistenceMode.InnerProperty )]
public ITemplate Template {
get { return _template; }
set { _template = value; }
}

public override void DataBind() {
base.DataBind();
}

protected override void CreateChildControls() {
this.Controls.Clear();
if( this.Template != null ) {
MyContainer mc = new MyContainer( "The
default container text!" );
this.Controls.Add( mc );
this.Template.InstantiateIn( mc );
}
base.CreateChildControls();
}
}
}

And with this, here's the output:

<!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 id="Head1"><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="/wEPDwULLTE4NzY2NjU3OTEPZBYCAgMPZBYCAgEPZBYCZg9kFgJmDxUBG1RoZSBkZWZhd
Wx0IGNvbnRhaW5lciB0ZXh0IWRkSSdc9mE3KlCrYB94vl7I3Cok4f8=" />
</div>

<div>
<span id="ScannerControl1_1">
<h1>The default container text!</h1>
</span>
</div>
</form>
</body>
</html>

-----Original Message-----
From: Discussion of building .NET applications targeted for the Web
[mailto:DOTNET-***@DISCUSS.DEVELOP.COM] On Behalf Of Mike Andrews
Sent: Friday, March 30, 2007 9:45 AM
To: DOTNET-***@DISCUSS.DEVELOP.COM
Subject: Re: [DOTNET-WEB] Template data is not being replaced in a Templated
ASP.NET server control...

Adam,

Thank you for your replies.

However, I cannot get any of your examples to work. I still the get same
effect as before.
One of the things I specifically tried was the <%# Container.GetType().Name
%> and it worked as before.
It's replaced with nothing instead of the name of the object.

Were you able to get them to work in your environment?

Thanks,
Mike
Post by Adam Sills
Your code modified. Note all I've done is change what's in the <%# %> block
and make ScannerControl inherit from CompositeControl.
<cc1:ScannerControl id="ScannerControl1_1" runat="server">
<Template>
<h1><%# Container.GetType().Name %></h1>
</Template>
</cc1:ScannerControl>
[ToolboxData( "<{0}:ScannerControl runat=server></{0}:ScannerControl>" )]
public class ScannerControl : CompositeControl {
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.Controls.Add( mc );
this.Template.InstantiateIn( mc );
}
base.CreateChildControls();
}
}
-----Original Message-----
From: Discussion of building .NET applications targeted for the Web
Sent: Thursday, March 29, 2007 4:06 PM
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
Post by Adam Sills
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
Post by Adam Sills
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
Sent: Thursday, March 29, 2007 3:47 PM
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
Post by Adam Sills
template control is created (MyContainer) and I call the Instantiate,
this
Post by Adam Sills
value is not replaced with the message "The default container text!" as
it
Post by Adam Sills
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
"
Post by Adam Sills
Inherits="_Default" %>
<!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>
<!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>
[ToolboxData("<{0}:ScannerControl
runat=server></{0}:ScannerControl>")]
Post by Adam Sills
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(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
Mike Andrews
2007-03-30 15:32:35 UTC
Permalink
Adam,

Thank you. I was able to get this to work after I placed a call to the
DataBind() method in the Page_Load event.
I appreciate your patience. I got my custom control to work as expected
now.

Thanks,
Mike
Yes.
But like I also said, I had to add a call to DataBind() in my codebehind
page.
Here's all the code, works great.
Inherits="WebApplication1._Default" %>
TagPrefix="cc1" %>
<html xmlns="http://www.w3.org/1999/xhtml" > <head id="Head1"
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>
namespace WebApplication1 {
public partial class _Default : System.Web.UI.Page {
protected void Page_Load( object sender, EventArgs e ) {
this.ScannerControl1_1.DataBind();
}
}
}
namespace WebApplication1 {
public class MyContainer : Control, INamingContainer {
public MyContainer( string text ) {
this.Text = text;
}
private string _text;
public string Text {
get { return _text; }
set { _text = value; }
}
}
}
namespace WebApplication1 {
[ToolboxData( "<{0}:ScannerControl
runat=server></{0}:ScannerControl>" )]
public class ScannerControl : CompositeControl {
private ITemplate _template;
[TemplateContainer( typeof( MyContainer ) )]
[PersistenceMode( PersistenceMode.InnerProperty )]
public ITemplate Template {
get { return _template; }
set { _template = value; }
}
public override void DataBind() {
base.DataBind();
}
protected override void CreateChildControls() {
this.Controls.Clear();
if( this.Template != null ) {
MyContainer mc = new MyContainer( "The
default container text!" );
this.Controls.Add( mc );
this.Template.InstantiateIn( mc );
}
base.CreateChildControls();
}
}
}
<!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 id="Head1"><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="/wEPDwULLTE4NzY2NjU3OTEPZBYCAgMPZBYCAgEPZBYCZg9kFgJmDxUBG1RoZSBkZWZhd
Wx0IGNvbnRhaW5lciB0ZXh0IWRkSSdc9mE3KlCrYB94vl7I3Cok4f8=" />
</div>
<div>
<span id="ScannerControl1_1">
<h1>The default container text!</h1>
</span>
</div>
</form>
</body>
</html>
-----Original Message-----
From: Discussion of building .NET applications targeted for the Web
Sent: Friday, March 30, 2007 9:45 AM
Subject: Re: [DOTNET-WEB] Template data is not being replaced in a Templated
ASP.NET server control...
Adam,
Thank you for your replies.
However, I cannot get any of your examples to work. I still the get same
effect as before.
One of the things I specifically tried was the <%# Container.GetType
().Name
%> and it worked as before.
It's replaced with nothing instead of the name of the object.
Were you able to get them to work in your environment?
Thanks,
Mike
Post by Adam Sills
Your code modified. Note all I've done is change what's in the <%# %> block
and make ScannerControl inherit from CompositeControl.
<cc1:ScannerControl id="ScannerControl1_1" runat="server">
<Template>
<h1><%# Container.GetType().Name %></h1>
</Template>
</cc1:ScannerControl>
[ToolboxData( "<{0}:ScannerControl runat=server></{0}:ScannerControl>"
)]
Post by Adam Sills
public class ScannerControl : CompositeControl {
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.Controls.Add( mc );
this.Template.InstantiateIn( mc );
}
base.CreateChildControls();
}
}
-----Original Message-----
From: Discussion of building .NET applications targeted for the Web
Sent: Thursday, March 29, 2007 4:06 PM
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
Post by Adam Sills
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
Post by Adam Sills
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
Sent: Thursday, March 29, 2007 3:47 PM
Subject: [DOTNET-WEB] Template data is not being replaced in a
Templated
Post by Adam Sills
Post by Adam Sills
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
Post by Adam Sills
template control is created (MyContainer) and I call the Instantiate,
this
Post by Adam Sills
value is not replaced with the message "The default container text!"
as
Post by Adam Sills
it
Post by Adam Sills
should have been, but instead is replaced with nothing.
Any help would be most appreciated. I'm really at my wits end with
this
Post by Adam Sills
Post by Adam Sills
one.
Thanks,
Mike
Default.aspx.cs
Post by Adam Sills
"
Post by Adam Sills
Inherits="_Default" %>
<!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>
<!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>
[ToolboxData("<{0}:ScannerControl
runat=server></{0}:ScannerControl>")]
Post by Adam Sills
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(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(r) 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
Mike Andrews
2007-03-30 18:26:26 UTC
Permalink
I have yet another question regarding this...

I'm almost working now...
However, when I put an asp control inside of the template, the markup is
sending the asp:xxx control out to the page instead of the rendered html.
Any suggestions?

Thanks,
Mike
Yes.
But like I also said, I had to add a call to DataBind() in my codebehind
page.
Here's all the code, works great.
Inherits="WebApplication1._Default" %>
TagPrefix="cc1" %>
<html xmlns="http://www.w3.org/1999/xhtml" > <head id="Head1"
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>
namespace WebApplication1 {
public partial class _Default : System.Web.UI.Page {
protected void Page_Load( object sender, EventArgs e ) {
this.ScannerControl1_1.DataBind();
}
}
}
namespace WebApplication1 {
public class MyContainer : Control, INamingContainer {
public MyContainer( string text ) {
this.Text = text;
}
private string _text;
public string Text {
get { return _text; }
set { _text = value; }
}
}
}
namespace WebApplication1 {
[ToolboxData( "<{0}:ScannerControl
runat=server></{0}:ScannerControl>" )]
public class ScannerControl : CompositeControl {
private ITemplate _template;
[TemplateContainer( typeof( MyContainer ) )]
[PersistenceMode( PersistenceMode.InnerProperty )]
public ITemplate Template {
get { return _template; }
set { _template = value; }
}
public override void DataBind() {
base.DataBind();
}
protected override void CreateChildControls() {
this.Controls.Clear();
if( this.Template != null ) {
MyContainer mc = new MyContainer( "The
default container text!" );
this.Controls.Add( mc );
this.Template.InstantiateIn( mc );
}
base.CreateChildControls();
}
}
}
<!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 id="Head1"><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="/wEPDwULLTE4NzY2NjU3OTEPZBYCAgMPZBYCAgEPZBYCZg9kFgJmDxUBG1RoZSBkZWZhd
Wx0IGNvbnRhaW5lciB0ZXh0IWRkSSdc9mE3KlCrYB94vl7I3Cok4f8=" />
</div>
<div>
<span id="ScannerControl1_1">
<h1>The default container text!</h1>
</span>
</div>
</form>
</body>
</html>
-----Original Message-----
From: Discussion of building .NET applications targeted for the Web
Sent: Friday, March 30, 2007 9:45 AM
Subject: Re: [DOTNET-WEB] Template data is not being replaced in a Templated
ASP.NET server control...
Adam,
Thank you for your replies.
However, I cannot get any of your examples to work. I still the get same
effect as before.
One of the things I specifically tried was the <%# Container.GetType
().Name
%> and it worked as before.
It's replaced with nothing instead of the name of the object.
Were you able to get them to work in your environment?
Thanks,
Mike
Post by Adam Sills
Your code modified. Note all I've done is change what's in the <%# %> block
and make ScannerControl inherit from CompositeControl.
<cc1:ScannerControl id="ScannerControl1_1" runat="server">
<Template>
<h1><%# Container.GetType().Name %></h1>
</Template>
</cc1:ScannerControl>
[ToolboxData( "<{0}:ScannerControl runat=server></{0}:ScannerControl>"
)]
Post by Adam Sills
public class ScannerControl : CompositeControl {
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.Controls.Add( mc );
this.Template.InstantiateIn( mc );
}
base.CreateChildControls();
}
}
-----Original Message-----
From: Discussion of building .NET applications targeted for the Web
Sent: Thursday, March 29, 2007 4:06 PM
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
Post by Adam Sills
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
Post by Adam Sills
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
Sent: Thursday, March 29, 2007 3:47 PM
Subject: [DOTNET-WEB] Template data is not being replaced in a
Templated
Post by Adam Sills
Post by Adam Sills
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
Post by Adam Sills
template control is created (MyContainer) and I call the Instantiate,
this
Post by Adam Sills
value is not replaced with the message "The default container text!"
as
Post by Adam Sills
it
Post by Adam Sills
should have been, but instead is replaced with nothing.
Any help would be most appreciated. I'm really at my wits end with
this
Post by Adam Sills
Post by Adam Sills
one.
Thanks,
Mike
Default.aspx.cs
Post by Adam Sills
"
Post by Adam Sills
Inherits="_Default" %>
<!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>
<!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>
[ToolboxData("<{0}:ScannerControl
runat=server></{0}:ScannerControl>")]
Post by Adam Sills
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(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(r) 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
Adam Sills
2007-03-30 18:29:26 UTC
Permalink
Did you forget to put runat="server" on the control?

-----Original Message-----
From: Discussion of building .NET applications targeted for the Web
[mailto:DOTNET-***@DISCUSS.DEVELOP.COM] On Behalf Of Mike Andrews
Sent: Friday, March 30, 2007 1:26 PM
To: DOTNET-***@DISCUSS.DEVELOP.COM
Subject: Re: [DOTNET-WEB] Template data is not being replaced in a Templated
ASP.NET server control...

I have yet another question regarding this...

I'm almost working now...
However, when I put an asp control inside of the template, the markup is
sending the asp:xxx control out to the page instead of the rendered html.
Any suggestions?

Thanks,
Mike
Yes.
But like I also said, I had to add a call to DataBind() in my codebehind
page.
Here's all the code, works great.
Inherits="WebApplication1._Default" %>
TagPrefix="cc1" %>
<html xmlns="http://www.w3.org/1999/xhtml" > <head id="Head1"
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>
namespace WebApplication1 {
public partial class _Default : System.Web.UI.Page {
protected void Page_Load( object sender, EventArgs e ) {
this.ScannerControl1_1.DataBind();
}
}
}
namespace WebApplication1 {
public class MyContainer : Control, INamingContainer {
public MyContainer( string text ) {
this.Text = text;
}
private string _text;
public string Text {
get { return _text; }
set { _text = value; }
}
}
}
namespace WebApplication1 {
[ToolboxData( "<{0}:ScannerControl
runat=server></{0}:ScannerControl>" )]
public class ScannerControl : CompositeControl {
private ITemplate _template;
[TemplateContainer( typeof( MyContainer ) )]
[PersistenceMode( PersistenceMode.InnerProperty )]
public ITemplate Template {
get { return _template; }
set { _template = value; }
}
public override void DataBind() {
base.DataBind();
}
protected override void CreateChildControls() {
this.Controls.Clear();
if( this.Template != null ) {
MyContainer mc = new MyContainer( "The
default container text!" );
this.Controls.Add( mc );
this.Template.InstantiateIn( mc );
}
base.CreateChildControls();
}
}
}
<!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 id="Head1"><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="/wEPDwULLTE4NzY2NjU3OTEPZBYCAgMPZBYCAgEPZBYCZg9kFgJmDxUBG1RoZSBkZWZhd
Wx0IGNvbnRhaW5lciB0ZXh0IWRkSSdc9mE3KlCrYB94vl7I3Cok4f8=" />
</div>
<div>
<span id="ScannerControl1_1">
<h1>The default container text!</h1>
</span>
</div>
</form>
</body>
</html>
-----Original Message-----
From: Discussion of building .NET applications targeted for the Web
Sent: Friday, March 30, 2007 9:45 AM
Subject: Re: [DOTNET-WEB] Template data is not being replaced in a Templated
ASP.NET server control...
Adam,
Thank you for your replies.
However, I cannot get any of your examples to work. I still the get same
effect as before.
One of the things I specifically tried was the <%# Container.GetType
().Name
%> and it worked as before.
It's replaced with nothing instead of the name of the object.
Were you able to get them to work in your environment?
Thanks,
Mike
Post by Adam Sills
Your code modified. Note all I've done is change what's in the <%# %> block
and make ScannerControl inherit from CompositeControl.
<cc1:ScannerControl id="ScannerControl1_1" runat="server">
<Template>
<h1><%# Container.GetType().Name %></h1>
</Template>
</cc1:ScannerControl>
[ToolboxData( "<{0}:ScannerControl runat=server></{0}:ScannerControl>"
)]
Post by Adam Sills
public class ScannerControl : CompositeControl {
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.Controls.Add( mc );
this.Template.InstantiateIn( mc );
}
base.CreateChildControls();
}
}
-----Original Message-----
From: Discussion of building .NET applications targeted for the Web
Sent: Thursday, March 29, 2007 4:06 PM
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
Post by Adam Sills
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
Post by Adam Sills
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
Sent: Thursday, March 29, 2007 3:47 PM
Subject: [DOTNET-WEB] Template data is not being replaced in a
Templated
Post by Adam Sills
Post by Adam Sills
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
Post by Adam Sills
template control is created (MyContainer) and I call the Instantiate,
this
Post by Adam Sills
value is not replaced with the message "The default container text!"
as
Post by Adam Sills
it
Post by Adam Sills
should have been, but instead is replaced with nothing.
Any help would be most appreciated. I'm really at my wits end with
this
Post by Adam Sills
Post by Adam Sills
one.
Thanks,
Mike
Default.aspx.cs
Post by Adam Sills
"
Post by Adam Sills
Inherits="_Default" %>
<!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>
<!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>
[ToolboxData("<{0}:ScannerControl
runat=server></{0}:ScannerControl>")]
Post by Adam Sills
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(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(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
Mike Andrews
2007-03-30 18:40:16 UTC
Permalink
Yes! LOL.

I just noticed that after I had already sent the message. Thank you!
Did you forget to put runat="server" on the control?
-----Original Message-----
From: Discussion of building .NET applications targeted for the Web
Sent: Friday, March 30, 2007 1:26 PM
Subject: Re: [DOTNET-WEB] Template data is not being replaced in a Templated
ASP.NET server control...
I have yet another question regarding this...
I'm almost working now...
However, when I put an asp control inside of the template, the markup is
sending the asp:xxx control out to the page instead of the rendered html.
Any suggestions?
Thanks,
Mike
Yes.
But like I also said, I had to add a call to DataBind() in my codebehind
page.
Here's all the code, works great.
Default.aspx.cs"
Inherits="WebApplication1._Default" %>
TagPrefix="cc1" %>
<html xmlns="http://www.w3.org/1999/xhtml" > <head id="Head1"
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>
namespace WebApplication1 {
public partial class _Default : System.Web.UI.Page {
protected void Page_Load( object sender, EventArgs e ) {
this.ScannerControl1_1.DataBind();
}
}
}
namespace WebApplication1 {
public class MyContainer : Control, INamingContainer {
public MyContainer( string text ) {
this.Text = text;
}
private string _text;
public string Text {
get { return _text; }
set { _text = value; }
}
}
}
namespace WebApplication1 {
[ToolboxData( "<{0}:ScannerControl
runat=server></{0}:ScannerControl>" )]
public class ScannerControl : CompositeControl {
private ITemplate _template;
[TemplateContainer( typeof( MyContainer ) )]
[PersistenceMode( PersistenceMode.InnerProperty )]
public ITemplate Template {
get { return _template; }
set { _template = value; }
}
public override void DataBind() {
base.DataBind();
}
protected override void CreateChildControls() {
this.Controls.Clear();
if( this.Template != null ) {
MyContainer mc = new MyContainer( "The
default container text!" );
this.Controls.Add( mc );
this.Template.InstantiateIn( mc );
}
base.CreateChildControls();
}
}
}
<!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 id="Head1"><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="/wEPDwULLTE4NzY2NjU3OTEPZBYCAgMPZBYCAgEPZBYCZg9kFgJmDxUBG1RoZSBkZWZhd
Wx0IGNvbnRhaW5lciB0ZXh0IWRkSSdc9mE3KlCrYB94vl7I3Cok4f8=" />
</div>
<div>
<span id="ScannerControl1_1">
<h1>The default container text!</h1>
</span>
</div>
</form>
</body>
</html>
-----Original Message-----
From: Discussion of building .NET applications targeted for the Web
Sent: Friday, March 30, 2007 9:45 AM
Subject: Re: [DOTNET-WEB] Template data is not being replaced in a Templated
ASP.NET server control...
Adam,
Thank you for your replies.
However, I cannot get any of your examples to work. I still the get
same
effect as before.
One of the things I specifically tried was the <%# Container.GetType
().Name
%> and it worked as before.
It's replaced with nothing instead of the name of the object.
Were you able to get them to work in your environment?
Thanks,
Mike
Post by Adam Sills
Your code modified. Note all I've done is change what's in the <%# %> block
and make ScannerControl inherit from CompositeControl.
<cc1:ScannerControl id="ScannerControl1_1" runat="server">
<Template>
<h1><%# Container.GetType().Name %></h1>
</Template>
</cc1:ScannerControl>
[ToolboxData( "<{0}:ScannerControl runat=server></{0}:ScannerControl>"
)]
Post by Adam Sills
public class ScannerControl : CompositeControl {
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.Controls.Add( mc );
this.Template.InstantiateIn( mc );
}
base.CreateChildControls();
}
}
-----Original Message-----
From: Discussion of building .NET applications targeted for the Web
Sent: Thursday, March 29, 2007 4:06 PM
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
Post by Adam Sills
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
Post by Adam Sills
DataBind on them in its DataBind call. You need to do that yourself
or
Post by Adam Sills
Post by Adam Sills
inherit from CompositeControl instead.
Adam..
-----Original Message-----
From: Discussion of building .NET applications targeted for the Web
Sent: Thursday, March 29, 2007 3:47 PM
Subject: [DOTNET-WEB] Template data is not being replaced in a
Templated
Post by Adam Sills
Post by Adam Sills
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
Post by Adam Sills
template control is created (MyContainer) and I call the
Instantiate,
Post by Adam Sills
this
Post by Adam Sills
value is not replaced with the message "The default container text!"
as
Post by Adam Sills
it
Post by Adam Sills
should have been, but instead is replaced with nothing.
Any help would be most appreciated. I'm really at my wits end with
this
Post by Adam Sills
Post by Adam Sills
one.
Thanks,
Mike
Default.aspx.cs
Post by Adam Sills
"
Post by Adam Sills
Inherits="_Default" %>
%>
Post by Adam Sills
Post by Adam Sills
<!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>
<!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">
Post by Adam Sills
Post by Adam Sills
<div>
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE"
value="/wEPDwUJNTgwMjcwMjU1ZGTpLl3HHZKhrXybhi5mD/cUEBCBwQ==" />
</div>
<div>
<span id="ScannerControl1_1">
<h1></h1>
</span>
</div>
</form>
</body>
</html>
[ToolboxData("<{0}:ScannerControl
runat=server></{0}:ScannerControl>")]
Post by Adam Sills
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
Post by Adam Sills
Post by Adam Sills
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(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(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(r) 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
Mike Andrews
2007-03-30 20:24:40 UTC
Permalink
Adam,

I've figured something else out now.
Due to the complex nature of this particular control I've had to call
DataBind() within the control itself.
If I call DataBind() in the Render() method, then I don't need to call in on
the page.
Also, I implemented the IPostBackEventHandler method for the control and
called DataBind() in it as well.
The control does work now with the complex behavior and works as expected.

Thanks again for your help,
Mike
Did you forget to put runat="server" on the control?
-----Original Message-----
From: Discussion of building .NET applications targeted for the Web
Sent: Friday, March 30, 2007 1:26 PM
Subject: Re: [DOTNET-WEB] Template data is not being replaced in a Templated
ASP.NET server control...
I have yet another question regarding this...
I'm almost working now...
However, when I put an asp control inside of the template, the markup is
sending the asp:xxx control out to the page instead of the rendered html.
Any suggestions?
Thanks,
Mike
Yes.
But like I also said, I had to add a call to DataBind() in my codebehind
page.
Here's all the code, works great.
Default.aspx.cs"
Inherits="WebApplication1._Default" %>
TagPrefix="cc1" %>
<html xmlns="http://www.w3.org/1999/xhtml" > <head id="Head1"
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>
namespace WebApplication1 {
public partial class _Default : System.Web.UI.Page {
protected void Page_Load( object sender, EventArgs e ) {
this.ScannerControl1_1.DataBind();
}
}
}
namespace WebApplication1 {
public class MyContainer : Control, INamingContainer {
public MyContainer( string text ) {
this.Text = text;
}
private string _text;
public string Text {
get { return _text; }
set { _text = value; }
}
}
}
namespace WebApplication1 {
[ToolboxData( "<{0}:ScannerControl
runat=server></{0}:ScannerControl>" )]
public class ScannerControl : CompositeControl {
private ITemplate _template;
[TemplateContainer( typeof( MyContainer ) )]
[PersistenceMode( PersistenceMode.InnerProperty )]
public ITemplate Template {
get { return _template; }
set { _template = value; }
}
public override void DataBind() {
base.DataBind();
}
protected override void CreateChildControls() {
this.Controls.Clear();
if( this.Template != null ) {
MyContainer mc = new MyContainer( "The
default container text!" );
this.Controls.Add( mc );
this.Template.InstantiateIn( mc );
}
base.CreateChildControls();
}
}
}
<!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 id="Head1"><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="/wEPDwULLTE4NzY2NjU3OTEPZBYCAgMPZBYCAgEPZBYCZg9kFgJmDxUBG1RoZSBkZWZhd
Wx0IGNvbnRhaW5lciB0ZXh0IWRkSSdc9mE3KlCrYB94vl7I3Cok4f8=" />
</div>
<div>
<span id="ScannerControl1_1">
<h1>The default container text!</h1>
</span>
</div>
</form>
</body>
</html>
-----Original Message-----
From: Discussion of building .NET applications targeted for the Web
Sent: Friday, March 30, 2007 9:45 AM
Subject: Re: [DOTNET-WEB] Template data is not being replaced in a Templated
ASP.NET server control...
Adam,
Thank you for your replies.
However, I cannot get any of your examples to work. I still the get
same
effect as before.
One of the things I specifically tried was the <%# Container.GetType
().Name
%> and it worked as before.
It's replaced with nothing instead of the name of the object.
Were you able to get them to work in your environment?
Thanks,
Mike
Post by Adam Sills
Your code modified. Note all I've done is change what's in the <%# %> block
and make ScannerControl inherit from CompositeControl.
<cc1:ScannerControl id="ScannerControl1_1" runat="server">
<Template>
<h1><%# Container.GetType().Name %></h1>
</Template>
</cc1:ScannerControl>
[ToolboxData( "<{0}:ScannerControl runat=server></{0}:ScannerControl>"
)]
Post by Adam Sills
public class ScannerControl : CompositeControl {
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.Controls.Add( mc );
this.Template.InstantiateIn( mc );
}
base.CreateChildControls();
}
}
-----Original Message-----
From: Discussion of building .NET applications targeted for the Web
Sent: Thursday, March 29, 2007 4:06 PM
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
Post by Adam Sills
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
Post by Adam Sills
DataBind on them in its DataBind call. You need to do that yourself
or
Post by Adam Sills
Post by Adam Sills
inherit from CompositeControl instead.
Adam..
-----Original Message-----
From: Discussion of building .NET applications targeted for the Web
Sent: Thursday, March 29, 2007 3:47 PM
Subject: [DOTNET-WEB] Template data is not being replaced in a
Templated
Post by Adam Sills
Post by Adam Sills
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
Post by Adam Sills
template control is created (MyContainer) and I call the
Instantiate,
Post by Adam Sills
this
Post by Adam Sills
value is not replaced with the message "The default container text!"
as
Post by Adam Sills
it
Post by Adam Sills
should have been, but instead is replaced with nothing.
Any help would be most appreciated. I'm really at my wits end with
this
Post by Adam Sills
Post by Adam Sills
one.
Thanks,
Mike
Default.aspx.cs
Post by Adam Sills
"
Post by Adam Sills
Inherits="_Default" %>
%>
Post by Adam Sills
Post by Adam Sills
<!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>
<!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">
Post by Adam Sills
Post by Adam Sills
<div>
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE"
value="/wEPDwUJNTgwMjcwMjU1ZGTpLl3HHZKhrXybhi5mD/cUEBCBwQ==" />
</div>
<div>
<span id="ScannerControl1_1">
<h1></h1>
</span>
</div>
</form>
</body>
</html>
[ToolboxData("<{0}:ScannerControl
runat=server></{0}:ScannerControl>")]
Post by Adam Sills
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
Post by Adam Sills
Post by Adam Sills
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(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(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(r) 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
Adam Sills
2007-03-29 21:41:18 UTC
Permalink
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:***@gmail.com]
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:DOTNET-***@DISCUSS.DEVELOP.COM] On Behalf Of Mike Andrews
Sent: Thursday, March 29, 2007 4:06 PM
To: DOTNET-***@DISCUSS.DEVELOP.COM
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
Post by Adam Sills
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
Sent: Thursday, March 29, 2007 3:47 PM
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
Inherits="_Default" %>
<!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>
<!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>
[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
Adam Sills
2007-03-29 21:03:00 UTC
Permalink
Of course #1 is probably being done, you just didn't give us your
default.aspx.cs contents :)

Adam..

-----Original Message-----
From: Adam Sills [mailto:***@gmail.com]
Sent: Thursday, March 29, 2007 4:02 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...

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:DOTNET-***@DISCUSS.DEVELOP.COM] On Behalf Of Mike Andrews
Sent: Thursday, March 29, 2007 3:47 PM
To: DOTNET-***@DISCUSS.DEVELOP.COM
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� http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com
Continue reading on narkive:
Loading...