2 out of 2 rated this helpful - Rate this topic

ListView.LayoutTemplate Property

Gets or sets the custom content for the root container in a ListView control.

Namespace:  System.Web.UI.WebControls
Assembly:  System.Web.Extensions (in System.Web.Extensions.dll)
[BrowsableAttribute(false)]
[PersistenceModeAttribute(PersistenceMode.InnerProperty)]
[TemplateContainerAttribute(typeof(ListView))]
public virtual ITemplate LayoutTemplate { get; set; }
<asp:ListView>
	<LayoutTemplate>ITemplate</LayoutTemplate>
</asp:ListView>

Property Value

Type: System.Web.UI.ITemplate
An object that contains the custom content for the root container in a ListView control. The default is null, which indicates that this property is not set.

Use the LayoutTemplate property to define a custom user interface (UI) for the root container of the ListView control.

To specify the layout template, add a LayoutTemplate element inside the ListView control. You can then add the contents of the template to the LayoutTemplate element.

The LayoutTemplate content must include a placeholder control such as a table row (tr) element for the items that are defined by the ItemTemplate template or for groups that are defined by the GroupTemplate template. The placeholder control must have the runat attribute set to "server" and the ID attribute set to the value of the ItemPlaceholderID or the GroupPlaceholderID property, depending on whether the ListView control is using groups.

The LayoutTemplate template is not required by the ListView control. You can use the ListView control without a LayoutTemplate and also without a placeholder server control with a known ID.

The following example shows how to define a custom template for the root container in the ListView control.

Security note Security Note

This example has a text box that accepts user input, which is a potential security threat. By default, ASP.NET Web pages validate that user input does not include script or HTML elements. For more information, see Script Exploits Overview.


<%@ Page language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head id="Head1" runat="server">
    <title>ListView Templates Example</title>
  </head>
  <body>
    <form id="form1" runat="server">

      <h3>ListView Templates Example</h3>

      <asp:ListView ID="ContactsListView" 
        DataSourceID="ContactsDataSource" 
        DataKeyNames="ContactID"
        runat="server">
        <LayoutTemplate>
          <table cellpadding="2" width="640px" border="1" runat="server" id="tblProducts">
            <tr runat="server">
              <th runat="server">Action</th>
              <th runat="server">First Name</th>
              <th runat="server">Last Name</th>
            </tr>
            <tr runat="server" id="itemPlaceholder" />
          </table>
          <asp:DataPager runat="server" ID="ContactsDataPager" PageSize="12">
            <Fields>
              <asp:NextPreviousPagerField ShowFirstPageButton="true" ShowLastPageButton="true"
                FirstPageText="|&lt;&lt; " LastPageText=" &gt;&gt;|"
                NextPageText=" &gt; " PreviousPageText=" &lt; " />
            </Fields>
          </asp:DataPager>
        </LayoutTemplate>
        <ItemTemplate>
          <tr runat="server">
            <td>
              <asp:LinkButton ID="EditButton" runat="Server" Text="Edit" CommandName="Edit" />
            </td>
            <td>
              <asp:Label ID="FirstNameLabel" runat="Server" Text='<%#Eval("FirstName") %>' />
            </td>
            <td valign="top">
              <asp:Label ID="LastNameLabel" runat="Server" Text='<%#Eval("LastName") %>' />
            </td>
          </tr>
        </ItemTemplate>
        <EditItemTemplate>
          <tr style="background-color: #ADD8E6">
            <td>
              <asp:LinkButton ID="UpdateButton" runat="server" CommandName="Update" Text="Update" />&nbsp;
              <asp:LinkButton ID="CancelButton" runat="server" CommandName="Cancel" Text="Cancel" />
            </td>
            <td>
              <asp:TextBox ID="FirstNameTextBox" runat="server" Text='<%#Bind("FirstName") %>' 
                MaxLength="50" /><br />
            </td>
            <td>
              <asp:TextBox ID="LastNameTextBox" runat="server" Text='<%#Bind("LastName") %>' 
                MaxLength="50" /><br />
            </td>
          </tr>
        </EditItemTemplate>
      </asp:ListView>

      <!-- This example uses Microsoft SQL Server and connects      -->
      <!-- to the AdventureWorks sample database. Use an ASP.NET    -->
      <!-- expression to retrieve the connection string value       -->
      <!-- from the Web.config file.                                -->
      <asp:SqlDataSource ID="ContactsDataSource" runat="server" 
        ConnectionString="<%$ ConnectionStrings:AdventureWorks_DataConnectionString %>"
        SelectCommand="SELECT [ContactID], [FirstName], [LastName] FROM Person.Contact"
        UpdateCommand="UPDATE Person.Contact
                       SET FirstName = @FirstName, LastName = @LastName
                       WHERE ContactID = @ContactID">
      </asp:SqlDataSource>

    </form>
  </body>
</html>


The following example shows how to use the ListView control without defining a LayoutTemplate template in the control. A server control with a known ID is also not specified.

<asp:ListView ID="ListView1" runat="server">
  <ItemTemplate>
    <% Eval("LastName")%>
  </ItemTemplate>
</asp:ListView>

.NET Framework

Supported in: 4, 3.5

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
admin only (codeBehind) /Security
Public Class _default1
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            For Each usr As MembershipUser In Membership.GetAllUsers
                lstUsers.Items.Add(usr.UserName)
            Next
        End If
    End Sub



    Private Sub lstUsers_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles lstUsers.SelectedIndexChanged
        lstRoles.Items.Clear()
        For Each role As String In Roles.GetRolesForUser(lstUsers.SelectedItem.ToString)
            lstRoles.Items.Add(role)
        Next
    End Sub
End Class
Authenticatie > Forms, Authorization, ... /Security
<?xml version="1.0"?>

<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->

<configuration>
    <configSections>
    </configSections>
    <connectionStrings>
        <add name="wt3sec" connectionString="Data Source=(local);Initial Catalog=SecurityLaboDB;User ID=zefzefzfzef;Password=zefdzedzefzef"
            providerName="System.Data.SqlClient" />
    </connectionStrings>
    <system.web>
        <compilation debug="true" strict="false" explicit="true" targetFramework="4.0" />

/// 1

        <authentication mode="Forms">
          <forms loginUrl="login.aspx" />
        </authentication>


//// 2

      <authorization>
        <deny users="?"/>
      </authorization>
    
//// 3

 <membership defaultProvider="SqlMembershipProvider">
        <providers>
          <add name="SqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" connectionStringName="wt3sec"/>
        </providers>
      </membership>

//// 4
    
 <roleManager enabled="true" defaultProvider="SqlRoleProvider">
        <providers>
          <add name="SqlRoleProvider" connectionStringName="wt3sec" type="System.Web.Security.SqlRoleProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
        </providers>
       </roleManager>
    </system.web>

///////////////////////////

</configuration>
default.aspx /StateManagement (codeBehind)
Imports System.Data.SqlClient
Imports System.Data.Common
Imports StateManagement.BL

Partial Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Me.IsPostBack Then
            '1. TreeView (menu opbouwen)
            PopulateTreeview(TreeView1.Nodes)
        End If

        '2. Preferred Theme instellen op basis van een cookie
        'SetThemeByCookie()

    End Sub

    Private Sub PopulateTreeview(ByVal treeviewNodes As TreeNodeCollection)
        For Each category In Product.GetCategories()
            Dim treenode As New TreeNode
            treenode.Text = Server.HtmlEncode(category)
            treenode.Value = category
            treeviewNodes.Add(treenode)
            Me.PopulateSubLevel(treenode)
        Next
    End Sub

    Private Sub PopulateSubLevel(ByVal treenode As TreeNode)
        For Each prod As Product In Product.GetProducts(treenode.Text)
            Dim tn As New TreeNode
            tn.Text = Server.HtmlEncode(prod.Name)
            tn.Value = prod.ProductID
            treenode.ChildNodes.Add(tn)
        Next
    End Sub

    Protected Sub TreeView1_SelectedNodeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TreeView1.SelectedNodeChanged
        viewProduct.ProductID = CType(sender, TreeView).SelectedValue
    End Sub


    Protected Sub Page_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
        Dim cookie As HttpCookie = Me.Request.Cookies("Theme")
        If Not cookie Is Nothing Then
            Dim Theme As String = cookie.Value
            ddlTheme.Items.FindByValue(Theme).Selected = True
            Page.Theme = Theme
        Else
            Page.Theme = "ThemeDark"
            ddlTheme.Items.FindByValue("ThemeDark").Selected = True
        End If
    End Sub


    Protected Sub ddlTheme_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlTheme.SelectedIndexChanged
        Dim cookie As New HttpCookie("Theme", ddlTheme.SelectedValue)
        cookie.Expires = Date.Now.AddMinutes(5.0)
        Response.Cookies.Add(cookie)
        Response.Redirect(Request.Url.ToString)

    End Sub
End Class