Reference server objects within custom templates?

  • Thread starter Thread starter Agendum
  • Start date Start date
A

Agendum

How is it possible to reference a server object within a custom template?
The below example demonstrates the problem. In this example I am simply
trying to print the Button2.ClientID but I get this error:

Compiler Error Message: CS0103: The name 'Button2' does not exist in the
current context

Removing this line allows it to work:

<%= Button2.ClientID %>

In the example you can see it works OK outside the template. Additionally
this very same code works fine in templates from Repeater, Master Pages,
ASP.NET Toolkit ModalPopupExtender, etc. Why does my small custom template
fail to work?

Thanks!


----- test.ascx -----
<%@ Control Language="C#" ClassName="TestControl" AutoEventWireup="true"
Debug="true" %>
<%@ Import Namespace="System.ComponentModel" %>

<script language="C#" runat="server">

private ITemplate _testTemplate = null;

[TemplateContainer(typeof(TestContainer))]
[PersistenceMode(PersistenceMode.InnerProperty)]
public ITemplate TestTemplate
{
get
{
return _testTemplate;
}

set
{
_testTemplate = value;
}
}

void Page_Load()
{
if (_testTemplate != null)
{
TestContainer container = new TestContainer();
_testTemplate.InstantiateIn(container);
testPlaceHolder.Controls.Add(container);
}
}

public class TestContainer : Control, INamingContainer
{
internal TestContainer() { }
}

</script>

<asp:PlaceHolder Id="testPlaceHolder" RunAt="server" />

----- test.aspx -----
<%@ Page Language="C#" %>
<%@ Register TagPrefix="test" TagName="TestControl" Src="test.ascx" %>

<html>
<head></head>
<body>
<form RunAt="server">

<asp:Button Id="Button1" RunAt="server" Text="Button1" />
<%= Button1.ClientID %>
<br/>

<test:TestControl RunAt="server">
<TestTemplate>
<asp:Button Id="Button2" RunAt="server" Text="Button2" />
<%= Button2.ClientID %>
<br/>
</TestTemplate>
</test:TestControl>

</form>
</body>
</html>
 
Agendum said:
How is it possible to reference a server object within a custom template?
The below example demonstrates the problem. In this example I am simply
trying to print the Button2.ClientID but I get this error:

Compiler Error Message: CS0103: The name 'Button2' does not exist in the
current context

Removing this line allows it to work:

<%= Button2.ClientID %>

In the example you can see it works OK outside the template. Additionally
this very same code works fine in templates from Repeater, Master Pages,
ASP.NET Toolkit ModalPopupExtender, etc. Why does my small custom
template
fail to work?

Thanks!

I am not sure exactly, and I don't have time to play completely with the
samples, but items like the ClientID are rendered using bits in the web
control base class. I would grab reflector and search through the compiled
..NET bits and see what it tries to do. You can then compare this to the
compiled ASP.NET version of your control (use a small project to test, as
you can end up with tons of assemblies in ASP.NET when you compile the
project). That is where I would look, if it was me.

My only query here is why is displaying the client ID so important? Is this
just to prove it can be done, to debug, or other?

--
Peace and Grace,
Greg

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

************************************************
| Think outside the box! |
************************************************
 
Printing the ClientID is irrelevant, its not the problem. The error is that
the object itself cannot be found. If the object was found ClientID would be
found. ClientID is just a property on every control so I printed it. Notice
the compiler error is that Button2 cannot be found, not a property on it:
Compiler Error Message: CS0103: The name 'Button2' does not exist in the current context

So the problem is -- why are templated objects not able to be referenced?

Thanks!
 
Hi, I am still struggling with this problem. Does anybody have any
idea what is going on? Thanks!

--- Original Question ---

How is it possible to reference a server object within a custom
template?
The below example demonstrates the problem. In this example I am
simply
trying to print the Button2.ClientID but I get this error:

Compiler Error Message: CS0103: The name 'Button2' does not exist in
the
current context

Removing this line allows it to work:

<%= Button2.ClientID %>

In the example you can see it works OK outside the template.
Additionally
this very same code works fine in templates from Repeater, Master
Pages,
ASP.NET Toolkit ModalPopupExtender, etc. Why does my small custom
template
fail to work?

Thanks!


----- test.ascx -----
<%@ Control Language="C#" ClassName="TestControl"
AutoEventWireup="true"
Debug="true" %>
<%@ Import Namespace="System.ComponentModel" %>

<script language="C#" runat="server">

private ITemplate _testTemplate = null;

[TemplateContainer(typeof(TestContainer))]
[PersistenceMode(PersistenceMode.InnerProperty)]
public ITemplate TestTemplate
{
get
{
return _testTemplate;
}

set
{
_testTemplate = value;
}
}

void Page_Load()
{
if (_testTemplate != null)
{
TestContainer container = new TestContainer();
_testTemplate.InstantiateIn(container);
testPlaceHolder.Controls.Add(container);
}
}

public class TestContainer : Control, INamingContainer
{
internal TestContainer() { }
}

</script>

<asp:PlaceHolder Id="testPlaceHolder" RunAt="server" />

----- test.aspx -----
<%@ Page Language="C#" %>
<%@ Register TagPrefix="test" TagName="TestControl" Src="test.ascx" %>

<html>
<head></head>
<body>
<form RunAt="server">

<asp:Button Id="Button1" RunAt="server" Text="Button1" />
<%= Button1.ClientID %>
<br/>

<test:TestControl RunAt="server">
<TestTemplate>
<asp:Button Id="Button2" RunAt="server" Text="Button2" />
<%= Button2.ClientID %>
<br/>
</TestTemplate>
</test:TestControl>

</form>
</body>
</html>
 
Back
Top