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