Condividi tramite


Esempio di controllo basato su modello

Di seguito viene illustrato un esempio di controllo basato su modello, TemplatedFirstControl, che comprende una proprietà FirstTemplate, di tipo ITemplate, e due proprietà, Text e DateTime, che contengono i dati per il controllo. Il controllo TemplatedFirstControl converte l'esempio illustrato nella sezione Sviluppo di un controllo server ASP.NET semplice in un controllo basato su modello per consentire allo sviluppatore di pagine di personalizzarne la presentazione.

Per generare l'esempio, vedere le istruzioni in Esempi di controlli server.

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

    

Il seguente controllo rappresenta il contenitore per il modello.

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

Utilizzo del controllo basato su modello in una pagina

Nella pagina ASP.NET di esempio illustrata di seguito viene utilizzato il controllo basato su modello definito nell'esempio precedente. Il controllo viene utilizzato con due diversi modelli e successivamente senza alcun modello.

<%@ 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>

Vedere anche

Sviluppo di un controllo basato su modello con associazione a dati