Reference server objects within custom templates?

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>
 
G

Gregory A. Beamer

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! |
************************************************
 
A

Agendum

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!
 
A

Agendum

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>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top