This documentation is archived and is not being maintained.

Creating Templates Dynamically in a DataList Web Server Control

Visual Studio .NET 2003

Templates do not have to be assigned at design time. In some situations, you might be able to lay out the template at design time, but changes you know you will make during run time are so extensive that loading a new template at run time simplifies the programming. In other cases, you may have several possible templates, but want to change the template at run-time.

One strategy is to create a template file with the basic template layout in it — a template definition file. To load a template at run time, use the Page.LoadTemplate method. This method reads a template definition from a file and creates an ITemplate Interface object. You can then assign this object to any of the templates in a DataList.

To create a template definition file

  1. On the Project menu, click Add New Item.
  2. In the Add New Item dialog box, select Text File. Name the file with the ".ascx" extension.
  3. Add template definition statements to the template file and save it.

    The following listing creates a template that displays the string "Name:" followed by the "LastName" field of the data source that is bound to the DataList.

    <!-- Visual Basic -->
    <%@ Language = "VB" %>
    Name: <b> <%# DataBinder.Eval(Container, "DataItem.LastName") %> </b>
    <!-- Visual C# -->
    <%@ Language = "C#" %>
    Name: <b> <%# DataBinder.Eval(Container, "DataItem.LastName") %> </b>

To create templates dynamically

  • Add code to your Web Forms page to load the template using the Page.LoadTemplate method.

    The following code uses the Page_Load event handler to load a template created as shown above and named NewTemplate.ascx.

    ' Visual Basic 
    Private Function CreateDataSource() As DataTable
       Dim dt As DataTable = New DataTable()
       dt.Columns.Add("LastName", Type.GetType("System.String"))
       dt.Rows.Add(New Object() {"Smith"})
       dt.Rows.Add(New Object() {"Jones"})
       dt.Rows.Add(New Object() {"Clark"})
       Return dt
    End Function
    Protected Sub Page_Init(ByVal Sender As System.Object, _
    ByVal e As System.EventArgs) Handles MyBase.Init
       If (Not Page.IsPostBack) Then
          DataList1.AlternatingItemTemplate = _
       End If
    End Sub
    Private Sub Page_Load(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles MyBase.Load
       'Put user code to initialize the page here
       If (Not Page.IsPostBack) Then
          DataList1.DataSource = CreateDataSource()
       End If
    End Sub
    // C#
    private DataTable CreateDataSource()
       DataTable dt = new DataTable();
       dt.Columns.Add("LastName", Type.GetType("System.String"));
       dt.Rows.Add(new object[] {"Smith"});
       dt.Rows.Add(new object[] {"Jones"});
       dt.Rows.Add(new object[] {"Clark"});
       return dt;
    private void Page_Init(object sender, EventArgs e)
       DataList1.AlternatingItemTemplate = 
    private void Page_Load(object sender, System.EventArgs e)
       // Put user code to initialize the page here
       if (!Page.IsPostBack)
          DataList1.DataSource = CreateDataSource();

See Also

TemplateControl.LoadTemplate Method | Web Server Controls Templates | DataList Web Server Control | Customizing DataList Items at Run Time