Nota:
El acceso a esta página requiere autorización. Puede intentar iniciar sesión o cambiar directorios.
El acceso a esta página requiere autorización. Puede intentar cambiar los directorios.
En el siguiente ejemplo de control basado en plantilla, TemplatedFirstControl, tiene una propiedad FirstTemplate, del tipo ITemplate, y dos propiedades, Text y DateTime, que contienen los datos para el control. TemplatedFirstControl convierte el ejemplo que aparece en Desarrollar un control de servidor ASP.NET sencillo en un control basado en plantilla permitiendo así a un programador de páginas personalizar su presentación.
Para generar el ejemplo, vea las instrucciones en Ejemplos de control de servidor.
using System;
using System.Web;
using System.Web.UI;
namespace CustomControls
{
[
ParseChildren(true)
]
public class TemplatedFirstControl : Control, INamingContainer
{
private ITemplate firstTemplate;
private String text = null;
private Control myTemplateContainer;
protected override void OnDataBinding(EventArgs e) {
EnsureChildControls();
base.OnDataBinding(e);
}
[TemplateContainer(typeof(FirstTemplateContainer))]
public ITemplate FirstTemplate
{
get
{
return firstTemplate;
}
set
{
firstTemplate = value;
}
}
public String Text
{
get
{
return text;
}
set
{
text = value;
}
}
public String DateTime
{
get
{
return System.DateTime.Now.ToLongTimeString();
}
}
public Control MyTemplateContainer
{
get
{
return myTemplateContainer;
}
}
protected override void CreateChildControls ()
{
if (FirstTemplate != null)
{
myTemplateContainer = new FirstTemplateContainer(this);
FirstTemplate.InstantiateIn(myTemplateContainer);
Controls.Add(myTemplateContainer);
}
else
{
Controls.Add(new LiteralControl(Text + " " + DateTime));
}
}
}
}
[Visual Basic]
Option Explicit
Option Strict
Imports System
Imports System.Web
Imports System.Web.UI
Namespace CustomControls
<ParseChildren(True)> _
Public Class TemplatedFirstControl
Inherits Control
Implements INamingContainer
Private _firstTemplate As ITemplate
Private _text As String = Nothing
Private _myTemplateContainer As Control
Protected Overrides Sub OnDataBinding(e As EventArgs)
EnsureChildControls()
MyBase.OnDataBinding(e)
End Sub
<TemplateContainer(GetType(FirstTemplateContainer))> _
Public Property FirstTemplate() As ITemplate
Get
Return _firstTemplate
End Get
Set
_firstTemplate = value
End Set
End Property
Public Property Text() As String
Get
Return _text
End Get
Set
_text = value
End Set
End Property
Public ReadOnly Property DateTime() As String
Get
Return System.DateTime.Now.ToLongTimeString()
End Get
End Property
Public ReadOnly Property MyTemplateContainer() As Control
Get
Return _myTemplateContainer
End Get
End Property
Protected Overrides Sub CreateChildControls()
If Not (FirstTemplate Is Nothing) Then
_myTemplateContainer = New FirstTemplateContainer(Me)
FirstTemplate.InstantiateIn(_myTemplateContainer)
Controls.Add(_myTemplateContainer)
Else
Controls.Add(New LiteralControl(Text + " " + DateTime))
End If
End Sub
End Class
End Namespace
El siguiente control es el contenedor de la plantilla.
using System;
using System.Web;
using System.Web.UI;
namespace CustomControls
{
public class FirstTemplateContainer : Control, INamingContainer
{
private TemplatedFirstControl parent;
public FirstTemplateContainer(TemplatedFirstControl parent)
{
this.parent = parent;
}
public String Text
{
get
{
return parent.Text;
}
}
public String DateTime
{
get
{
return parent.DateTime;
}
}
}
}
[Visual Basic]
Option Explicit
Option Strict
Imports System
Imports System.Web
Imports System.Web.UI
Namespace CustomControls
Public Class FirstTemplateContainer
Inherits Control
Implements INamingContainer
Private _parent As TemplatedFirstControl
Public Sub New(parent As TemplatedFirstControl)
Me._parent = parent
End Sub
Public ReadOnly Property Text() As [String]
Get
Return _parent.Text
End Get
End Property
Public ReadOnly Property DateTime() As [String]
Get
Return _parent.DateTime
End Get
End Property
End Class
End Namespace
Utilizar el control basado en plantilla en una página
En la siguiente página ASP.NET se utiliza el control basado en plantilla que se definió en el ejemplo anterior. Primero se utiliza el control con dos plantillas distintas y luego sin plantilla.
<%@ Register TagPrefix="Custom" Namespace="CustomControls" Assembly = "CustomControls" %>
<script runat=server language="VB">
Sub Page_Load()
First.DataBind()
Second.DataBind()
End Sub
</script>
<html>
<body>
<form runat=server>
Here is a custom templated server control:<br><br>
<Custom:TemplatedFirstControl id = "First" Text= "The time on the server is " runat=server>
<FirstTemplate>
<h3><font face="Verdana" color = "red"><%# Container.Text %> <%# Container.DateTime %>
</font></h3>
</FirstTemplate>
</Custom:TemplatedFirstControl>
Here is the templated server control with a different template:<br><br>
<Custom:TemplatedFirstControl id = "Second" Text= "The time on the server is " runat=server>
<FirstTemplate>
<h1><font face="Arial" color = "Purple"><%# Container.Text %> <%# Container.DateTime %>
</font></h1>
</FirstTemplate>
</Custom:TemplatedFirstControl>
Here is the templated server control without a template:<br><br>
<Custom:TemplatedFirstControl id = "Third" Text= "The time on the server is " runat=server/>
</form>
</body>
</html>
Vea también
Desarrollar un control enlazado a datos a partir de plantillas