System.Web.UI.WebControls N ...


.NET Framework Class Library
ListViewItem Class

Represents an individual item in a ListView control.

Namespace:  System.Web.UI.WebControls
Assembly:  System.Web.Extensions (in System.Web.Extensions.dll)
Syntax

Visual Basic (Declaration)
<AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level := AspNetHostingPermissionLevel.Minimal)> _
<AspNetHostingPermissionAttribute(SecurityAction.InheritanceDemand, Level := AspNetHostingPermissionLevel.Minimal)> _
Public Class ListViewItem _
    Inherits Control _
    Implements INamingContainer
Visual Basic (Usage)
Dim instance As ListViewItem
C#
[AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
[AspNetHostingPermissionAttribute(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
public class ListViewItem : Control, 
    INamingContainer
Visual C++
[AspNetHostingPermissionAttribute(SecurityAction::LinkDemand, Level = AspNetHostingPermissionLevel::Minimal)]
[AspNetHostingPermissionAttribute(SecurityAction::InheritanceDemand, Level = AspNetHostingPermissionLevel::Minimal)]
public ref class ListViewItem : public Control, 
    INamingContainer
JScript
public class ListViewItem extends Control implements INamingContainer
ASP.NET
<asp:ListViewItem />
Remarks

The ListViewItem class is used to represent an individual item in a ListView control. An individual item usually represents a record in the underlying data source of a ListView control.

Each item in the ListView control has a designated item type, as listed in the following table.

Item type

Description

DataItem

A data item in the ListView control.

InsertItem

An insert item in the ListView control.

EmptyItem

An empty item in the ListView control. The empty item is displayed when the ListView control does not have any records to display, or when a group in the ListView control does not have any more records to display.

To determine the type of a ListViewItem object, use the ItemType property.

If an item contains other controls, you can retrieve a control by using the Controls collection of the item. If the control's ID property is set, you can also use the FindControl method to find the control.

For a list of initial property values for an instance of ListViewItem, see the ListViewItem constructor.

Examples

The following example shows how to use the ListViewItem class to access the properties of the item that is being created in the ListView control.

Security noteSecurity 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.

Visual Basic
<%@ Page language="VB" %>

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

<script runat="server">

  Protected Sub ContactsListView_ItemCreated(ByVal sender As Object, ByVal e As ListViewItemEventArgs)

    ' Retrieve the current item.
    Dim item As ListViewItem = e.Item

    ' Verify if the item is a data item.
    If item.ItemType = ListViewItemType.DataItem Then

      ' Get the EmailAddressLabel Label control in the item.      
      Dim EmailAddressLabel As Label = CType(item.FindControl("EmailAddressLabel"), Label)

      ' Display the e-mail address in italics.
      EmailAddressLabel.Font.Italic = True

    End If

  End Sub

</script>

<html  >
  <head id="Head1" runat="server">
    <title>ListViewItem Example</title>
    <style type="text/css">
      body { text-align: center; }
      .bgcolor { background-color: #CAEEFF; }
    </style>
  </head>
  <body style="font: 10pt Trebuchet MS">
    <form id="form1" runat="server">

      <h3>ListViewItem Example</h3>

      <asp:ListView ID="ContactsListView" 
        DataSourceID="ContactsDataSource"
        InsertItemPosition="LastItem"
        OnItemCreated="ContactsListView_ItemCreated"
        runat="server">
        <LayoutTemplate>
          <table cellpadding="2" width="680px" border="0">
            <tr class="bgcolor" runat="server">
              <th runat="server">First Name</th>
              <th runat="server">Last Name</th>
              <th runat="server">E-mail Address</th>
            </tr>
            <tr id="itemPlaceholder" runat="server"></tr>
          </table>
          <asp:DataPager runat="server" ID="PeopleDataPager">
            <Fields>
              <asp:NumericPagerField ButtonCount="10" /> 
            </Fields>
          </asp:DataPager>
        </LayoutTemplate>
        <ItemTemplate>
          <tr runat="server"> 
            <td>
              <asp:Label ID="FirstNameLabel" runat="server" Text='<%#Eval("FirstName") %>' />
            </td>
            <td>
              <asp:Label ID="LastNameLabel" runat="server" Text='<%#Eval("LastName") %>' />
            </td>
            <td>
              <asp:Label ID="EmailAddressLabel" runat="server" Text='<%#Eval("EmailAddress") %>' />
            </td>
          </tr>
        </ItemTemplate>
        <InsertItemTemplate>
          <tr class="bgcolor">
            <td>
              <asp:TextBox ID="FirstNameTextBox" runat="server" 
                Text='<%#Bind("FirstName") %>' MaxLength="50" />
            </td>
            <td>
              <asp:TextBox ID="LastNameTextBox" runat="server" 
                Text='<%#Bind("LastName") %>' MaxLength="50" /> <br />
            </td>
            <td>
              <asp:TextBox ID="EmailAddressTextBox" runat="server" 
                Text='<%#Bind("EmailAddress") %>' MaxLength="50" /> <br />
            </td>
          </tr>
          <tr class="bgcolor" runat="server">
            <td colspan="3">
              <asp:Button ID="InsertButton" runat="server" CommandName="Insert" Text="Insert" />
              <asp:Button ID="CancelInsertButton" runat="server" CommandName="Cancel" Text="Cancel" />
            </td>
          </tr>
        </InsertItemTemplate>
      </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 FirstName, LastName, EmailAddress FROM Person.Contact"
        InsertCommand="INSERT INTO Person.Contact
         ([FirstName], [LastName], [EmailAddress], [PasswordHash], [PasswordSalt]) 
         Values(@FirstName, @LastName, @EmailAddress, '', '')">
      </asp:SqlDataSource>

    </form>
  </body>
</html>
C#
<%@ Page language="C#" %>

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

<script runat="server">

  protected void ContactsListView_ItemCreated(object sender, ListViewItemEventArgs e)
  {
    // Retrieve the current item.
    ListViewItem item = e.Item;

    // Verify if the item is a data item.
    if (item.ItemType == ListViewItemType.DataItem)
    {
      // Get the EmailAddressLabel Label control in the item.
      Label EmailAddressLabel = (Label)item.FindControl("EmailAddressLabel");

      // Display the e-mail address in italics.
      EmailAddressLabel.Font.Italic = true;
    }
  }

</script>

<html  >
  <head id="Head1" runat="server">
    <title>ListViewItem Example</title>
    <style type="text/css">
      body { text-align: center; }
      .bgcolor { background-color: #CAEEFF; }
    </style>
  </head>
  <body style="font: 10pt Trebuchet MS">
    <form id="form1" runat="server">

      <h3>ListViewItem Example</h3>

      <asp:ListView ID="ContactsListView" 
        DataSourceID="ContactsDataSource"
        InsertItemPosition="LastItem"
        OnItemCreated="ContactsListView_ItemCreated"
        runat="server">
        <LayoutTemplate>
          <table cellpadding="2" width="680px" border="0">
            <tr class="bgcolor" runat="server">
              <th runat="server">First Name</th>
              <th runat="server">Last Name</th>
              <th runat="server">E-mail Address</th>
            </tr>
            <tr id="itemPlaceholder" runat="server"></tr>
          </table>
          <asp:DataPager runat="server" ID="PeopleDataPager">
            <Fields>
              <asp:NumericPagerField ButtonCount="10" /> 
            </Fields>
          </asp:DataPager>
        </LayoutTemplate>
        <ItemTemplate>
          <tr runat="server"> 
            <td>
              <asp:Label ID="FirstNameLabel" runat="server" Text='<%#Eval("FirstName") %>' />
            </td>
            <td>
              <asp:Label ID="LastNameLabel" runat="server" Text='<%#Eval("LastName") %>' />
            </td>
            <td>
              <asp:Label ID="EmailAddressLabel" runat="server" Text='<%#Eval("EmailAddress") %>' />
            </td>
          </tr>
        </ItemTemplate>
        <InsertItemTemplate>
          <tr class="bgcolor">
            <td>
              <asp:TextBox ID="FirstNameTextBox" runat="server" 
                Text='<%#Bind("FirstName") %>' MaxLength="50" />
            </td>
            <td>
              <asp:TextBox ID="LastNameTextBox" runat="server" 
                Text='<%#Bind("LastName") %>' MaxLength="50" /> <br />
            </td>
            <td>
              <asp:TextBox ID="EmailAddressTextBox" runat="server" 
                Text='<%#Bind("EmailAddress") %>' MaxLength="50" /> <br />
            </td>
          </tr>
          <tr class="bgcolor" runat="server">
            <td colspan="3">
              <asp:Button ID="InsertButton" runat="server" CommandName="Insert" Text="Insert" />
              <asp:Button ID="CancelInsertButton" runat="server" CommandName="Cancel" Text="Cancel" />
            </td>
          </tr>
        </InsertItemTemplate>
      </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 FirstName, LastName, EmailAddress FROM Person.Contact"
        InsertCommand="INSERT INTO Person.Contact
         ([FirstName], [LastName], [EmailAddress], [PasswordHash], [PasswordSalt]) 
         Values(@FirstName, @LastName, @EmailAddress, '', '')">
      </asp:SqlDataSource>

    </form>
  </body>
</html>
.NET Framework Security

Inheritance Hierarchy

System..::.Object
  System.Web.UI..::.Control
    System.Web.UI.WebControls..::.ListViewItem
      System.Web.UI.WebControls..::.ListViewDataItem
Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Platforms

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Version Information

.NET Framework

Supported in: 3.5
See Also

Reference

Other Resources

Tags :


Page view tracker