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