ListViewDataItem 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
表示 ListView 控件中的单个数据项。
public ref class ListViewDataItem : System::Web::UI::WebControls::ListViewItem, System::Web::UI::IDataItemContainer
public ref class ListViewDataItem : System::Web::UI::WebControls::ListViewItem
public class ListViewDataItem : System.Web.UI.WebControls.ListViewItem, System.Web.UI.IDataItemContainer
public class ListViewDataItem : System.Web.UI.WebControls.ListViewItem
type ListViewDataItem = class
inherit ListViewItem
interface IDataItemContainer
interface INamingContainer
type ListViewDataItem = class
inherit ListViewItem
Public Class ListViewDataItem
Inherits ListViewItem
Implements IDataItemContainer
Public Class ListViewDataItem
Inherits ListViewItem
- 继承
- 实现
示例
以下示例演示如何使用 ListViewDataItem 对象从控件中的 ListView 项检索字段值。
重要
此示例具有一个接受用户输入的文本框,这是一个潜在的安全威胁。 默认情况下,ASP.NET 网页验证用户输入是否不包含脚本或 HTML 元素。 有关详细信息,请参阅脚本侵入概述。
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
//<Snippet2>
protected void ContactsListView_ItemDataBound(object sender, ListViewItemEventArgs e)
{
//Verify there is an item being edited.
if (ContactsListView.EditIndex >= 0)
{
//Get the item object.
ListViewDataItem dataItem = (ListViewDataItem)e.Item;
// Check for an item in edit mode.
if (dataItem.DisplayIndex == ContactsListView.EditIndex)
{
// Preselect the DropDownList control with the Title value
// for the current item.
// Retrieve the underlying data item. In this example
// the underlying data item is a DataRowView object.
DataRowView rowView = (DataRowView)dataItem.DataItem;
// Retrieve the Title value for the current item.
String title = rowView["Title"].ToString();
// Retrieve the DropDownList control from the current row.
DropDownList list = (DropDownList)dataItem.FindControl("TitlesList");
// Find the ListItem object in the DropDownList control with the
// title value and select the item.
ListItem item = list.Items.FindByText(title);
list.SelectedIndex = list.Items.IndexOf(item);
}
}
}
//</Snippet2>
void ContactsListView_ItemUpdating(Object sender, ListViewUpdateEventArgs e)
{
// Retrieve the row being edited.
ListViewItem item = ContactsListView.Items[ContactsListView.EditIndex];
// Retrieve the DropDownList control from the row.
DropDownList list = (DropDownList)item.FindControl("TitlesList");
// Add the selected value of the DropDownList control to
// the NewValues collection. The NewValues collection is
// passed to the data source control, which then updates the
// data source.
e.NewValues["Title"] = list.SelectedValue;
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>ListView DataItem Example</title>
<link type="text/css" rel="stylesheet" href="StyleSheet.css" />
</head>
<body>
<form id="form1" runat="server">
<h3>ListViewDataItem Example</h3>
<asp:ListView ID="ContactsListView"
DataSourceID="ContactsDataSource"
DataKeyNames="ContactID"
OnItemUpdating="ContactsListView_ItemUpdating"
OnItemDataBound="ContactsListView_ItemDataBound"
runat="server" >
<LayoutTemplate>
<table cellpadding="2" runat="server" id="tblContacts" width="640px">
<tr runat="server" id="itemPlaceholder" />
</table>
<asp:DataPager runat="server" ID="ContactsDataPager" PageSize="12">
<Fields>
<asp:NextPreviousPagerField
ShowFirstPageButton="true" ShowLastPageButton="true"
FirstPageText="|<< " LastPageText=" >>|"
NextPageText=" > " PreviousPageText=" < " />
</Fields>
</asp:DataPager>
</LayoutTemplate>
<ItemTemplate>
<tr class="DataItem" 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>
<asp:Label ID="LastNameLabel" runat="server" Text='<%#Eval("LastName") %>' />
</td>
<td>
<asp:Label ID="TitleLabel" runat="server" Text='<%#Eval("Title", "{0:d}") %>' />
</td>
</tr>
</ItemTemplate>
<EditItemTemplate>
<tr class="EditItem">
<td>
<asp:LinkButton ID="UpdateButton" runat="server"
CommandName="Update" Text="Update" /><br />
<asp:LinkButton ID="CancelButton" runat="server"
CommandName="Cancel" Text="Cancel" />
</td>
<td>
<asp:Label runat="server" ID="FirstNameLabel"
AssociatedControlID="FirstNameTextBox" Text="First Name:"/>
<asp:TextBox ID="FirstNameTextBox" runat="server"
Text='<%#Bind("FirstName") %>' MaxLength="50" />
</td>
<td>
<asp:Label runat="server" ID="LastNameLabel"
AssociatedControlID="LastNameTextBox" Text="Last Name:" />
<asp:TextBox ID="LastNameTextBox" runat="server"
Text='<%#Bind("LastName") %>' MaxLength="50" /><br />
</td>
<td>
<asp:Label runat="server" ID="TitleLabel"
AssociatedControlID="TitlesList" Text="Title:"/>
<asp:DropDownList ID="TitlesList"
DataSourceID="TitlesSqlDataSource"
DataTextField="Title"
runat="server">
</asp:DropDownList>
</td>
</tr>
</EditItemTemplate>
</asp:ListView>
<asp:SqlDataSource ID="TitlesSqlDataSource"
SelectCommand="SELECT Distinct [Title] FROM Person.Contact ORDER BY [Title]"
ConnectionString="<%$ ConnectionStrings:AdventureWorks_DataConnectionString %>"
runat="server">
</asp:SqlDataSource>
<asp:SqlDataSource ID="ContactsDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:AdventureWorks_DataConnectionString %>"
SelectCommand="SELECT [ContactID], [FirstName], [LastName], [Title] FROM Person.Contact"
UpdateCommand="UPDATE Person.Contact
SET FirstName = @FirstName, LastName = @LastName, Title = @Title
WHERE ContactID = @ContactID">
<UpdateParameters>
<asp:Parameter ConvertEmptyStringToNull="true" Name="Title" />
</UpdateParameters>
</asp:SqlDataSource>
</form>
</body>
</html>
<%@ Page Language="VB" %>
<%@ Import Namespace="System.Data" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
'<Snippet2>
Protected Sub ContactsListView_ItemDataBound(ByVal sender As Object, ByVal e As ListViewItemEventArgs)
'Verify there is an item being edited.
If ContactsListView.EditIndex >= 0 Then
'Get the item object.
Dim dataItem As ListViewDataItem = CType(e.Item, ListViewDataItem)
' Check for an item in edit mode.
If dataItem.DisplayIndex = ContactsListView.EditIndex Then
' Preselect the DropDownList control with the Title value
' for the current item.
' Retrieve the underlying data item. In this example
' the underlying data item is a DataRowView object.
Dim rowView As DataRowView = CType(dataItem.DataItem, DataRowView)
' Retrieve the Title value for the current item.
Dim title As String = rowView("Title").ToString()
' Retrieve the DropDownList control from the current row.
Dim list As DropDownList = CType(dataItem.FindControl("TitlesList"), DropDownList)
' Find the ListItem object in the DropDownList control with the
' title value and select the item.
Dim item As ListItem = list.Items.FindByText(title)
list.SelectedIndex = list.Items.IndexOf(item)
End If
End If
End Sub
'</Snippet2>
Sub ContactsListView_ItemUpdating(ByVal sender As Object, ByVal e As ListViewUpdateEventArgs)
' Retrieve the row being edited.
Dim item As ListViewItem = ContactsListView.Items(ContactsListView.EditIndex)
' Retrieve the DropDownList control from the row.
Dim list As DropDownList = CType(item.FindControl("TitlesList"), DropDownList)
' Add the selected value of the DropDownList control to
' the NewValues collection. The NewValues collection is
' passed to the data source control, which then updates the
' data source.
e.NewValues("Title") = list.SelectedValue
End Sub 'ContactsListView_ItemUpdating
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>ListView DataItem Example</title>
<link type="text/css" rel="stylesheet" href="StyleSheet.css" />
</head>
<body>
<form id="form1" runat="server">
<h3>ListViewDataItem Example</h3>
<asp:ListView ID="ContactsListView"
DataSourceID="ContactsDataSource"
DataKeyNames="ContactID"
OnItemUpdating="ContactsListView_ItemUpdating"
OnItemDataBound="ContactsListView_ItemDataBound"
runat="server" >
<LayoutTemplate>
<table cellpadding="2" runat="server" id="tblContacts" width="640px">
<tr runat="server" id="itemPlaceholder" />
</table>
<asp:DataPager runat="server" ID="ContactsDataPager" PageSize="12">
<Fields>
<asp:NextPreviousPagerField
ShowFirstPageButton="true" ShowLastPageButton="true"
FirstPageText="|<< " LastPageText=" >>|"
NextPageText=" > " PreviousPageText=" < " />
</Fields>
</asp:DataPager>
</LayoutTemplate>
<ItemTemplate>
<tr class="DataItem" 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>
<asp:Label ID="LastNameLabel" runat="server" Text='<%#Eval("LastName") %>' />
</td>
<td>
<asp:Label ID="TitleLabel" runat="server" Text='<%#Eval("Title", "{0:d}") %>' />
</td>
</tr>
</ItemTemplate>
<EditItemTemplate>
<tr class="EditItem">
<td>
<asp:LinkButton ID="UpdateButton" runat="server"
CommandName="Update" Text="Update" /><br />
<asp:LinkButton ID="CancelButton" runat="server"
CommandName="Cancel" Text="Cancel" />
</td>
<td>
<asp:Label runat="server" ID="FirstNameLabel"
AssociatedControlID="FirstNameTextBox" Text="First Name:"/>
<asp:TextBox ID="FirstNameTextBox" runat="server"
Text='<%#Bind("FirstName") %>' MaxLength="50" />
</td>
<td>
<asp:Label runat="server" ID="LastNameLabel"
AssociatedControlID="LastNameTextBox" Text="Last Name:" />
<asp:TextBox ID="LastNameTextBox" runat="server"
Text='<%#Bind("LastName") %>' MaxLength="50" /><br />
</td>
<td>
<asp:Label runat="server" ID="TitleLabel"
AssociatedControlID="TitlesList" Text="Title:"/>
<asp:DropDownList ID="TitlesList"
DataSourceID="TitlesSqlDataSource"
DataTextField="Title"
runat="server">
</asp:DropDownList>
</td>
</tr>
</EditItemTemplate>
</asp:ListView>
<asp:SqlDataSource ID="TitlesSqlDataSource"
SelectCommand="SELECT Distinct [Title] FROM Person.Contact ORDER BY [Title]"
ConnectionString="<%$ ConnectionStrings:AdventureWorks_DataConnectionString %>"
runat="server">
</asp:SqlDataSource>
<asp:SqlDataSource ID="ContactsDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:AdventureWorks_DataConnectionString %>"
SelectCommand="SELECT [ContactID], [FirstName], [LastName], [Title] FROM Person.Contact"
UpdateCommand="UPDATE Person.Contact
SET FirstName = @FirstName, LastName = @LastName, Title = @Title
WHERE ContactID = @ContactID">
<UpdateParameters>
<asp:Parameter ConvertEmptyStringToNull="true" Name="Title" />
</UpdateParameters>
</asp:SqlDataSource>
</form>
</body>
</html>
注解
类 ListViewDataItem 表示控件中的 ListView 单个数据项。 数据项通常对应于数据源对象中的记录。 类派生自 ListViewItem 类。
控件中的每个 ListView 项都有由 属性表示的 ItemType 指定项类型。 当项类型为 ListViewItemType.DataItem时, ListViewDataItem 可以使用 类获取有关项的详细信息。
控件 ListView 将数据项存储在集合中 Items 。 若要确定集合中Items对象的索引ListViewDataItem,请使用 DisplayIndex 属性。 若要确定基础数据源中数据项的索引,请使用 DataItemIndex 属性。 这些索引属性之间的区别在于表示 DisplayIndex 数据项在当前页中的位置,而 DataItemIndex 基于页面偏移量。 如果未使用 控件启用分页功能 ListView ,则这两个 DataPager 属性将包含相同的值。
可以使用 属性访问基础数据对象的 DataItem 属性。
属性DataItem仅在 控件事件ListView期间和之后ItemDataBound可用。
有关 ListViewDataItem 类的实例的初始属性值列表,请参见 ListViewDataItem 构造函数。
构造函数
| ListViewDataItem(Int32, Int32) |
初始化 ListViewDataItem 类的新实例。 |
属性
| Adapter |
获取控件的浏览器特定适配器。 (继承自 Control) |
| AppRelativeTemplateSourceDirectory |
获取或设置包含该控件的 Page 或 UserControl 对象的应用程序相对虚拟目录。 (继承自 Control) |
| BindingContainer |
获取包含该控件的数据绑定的控件。 (继承自 Control) |
| ChildControlsCreated |
获取一个值,该值指示是否已创建服务器控件的子控件。 (继承自 Control) |
| ClientID |
获取由 ASP.NET 生成的 HTML 标记的控件 ID。 (继承自 Control) |
| ClientIDMode |
获取或设置用于生成 ClientID 属性值的算法。 (继承自 Control) |
| ClientIDSeparator |
获取一个字符值,该值表示 ClientID 属性中使用的分隔符字符。 (继承自 Control) |
| Context |
为当前 Web 请求获取与服务器控件关联的 HttpContext 对象。 (继承自 Control) |
| Controls |
获取 ControlCollection 对象,该对象表示 UI 层次结构中的指定服务器控件的子控件。 (继承自 Control) |
| DataItem |
获取或设置 ListViewItem 对象绑定到的基础数据对象。 |
| DataItemContainer |
如果命名容器实现 IDataItemContainer,则获取对命名容器的引用。 (继承自 Control) |
| DataItemIndex |
获取基础数据源对象中 DataItem 的索引。 |
| DataKeysContainer |
如果命名容器实现 IDataKeysControl,则获取对命名容器的引用。 (继承自 Control) |
| DesignMode |
获取一个值,该值指示是否正在使用设计图面上的一个控件。 (继承自 Control) |
| DisplayIndex |
获取数据项在 ListView 控件中显示的位置。 |
| EnableTheming |
获取或设置一个值,该值指示主题是否应用于该控件。 (继承自 Control) |
| EnableViewState |
获取或设置一个值,该值指示服务器控件是否向发出请求的客户端保持自己的视图状态以及它所包含的任何子控件的视图状态。 (继承自 Control) |
| Events |
获取控件的事件处理程序委托列表。 此属性为只读。 (继承自 Control) |
| HasChildViewState |
获取一个值,该值指示当前服务器控件的子控件是否具有任何已保存的视图状态设置。 (继承自 Control) |
| ID |
获取或设置分配给服务器控件的编程标识符。 (继承自 Control) |
| IdSeparator |
获取用于分隔控件标识符的字符。 (继承自 Control) |
| IsChildControlStateCleared |
获取一个值,该值指示该控件中包含的控件是否具有控件状态。 (继承自 Control) |
| IsTrackingViewState |
获取一个值,用于指示服务器控件是否会将更改保存到其视图状态中。 (继承自 Control) |
| IsViewStateEnabled |
获取一个值,该值指示是否为该控件启用了视图状态。 (继承自 Control) |
| ItemType |
获取 ListViewItem 对象的项类型。 (继承自 ListViewItem) |
| LoadViewStateByID |
获取一个值,该值指示控件是否通过 ID 而不是索引参与加载其视图状态。 (继承自 Control) |
| NamingContainer |
获取对服务器控件的命名容器的引用,此引用创建唯一的命名空间,以区分具有相同 ID 属性值的服务器控件。 (继承自 Control) |
| Page |
获取对包含服务器控件的 Page 实例的引用。 (继承自 Control) |
| Parent |
获取对页 UI 层次结构中服务器控件的父控件的引用。 (继承自 Control) |
| RenderingCompatibility |
获取一个值,该值指定呈现的 HTML 将与之兼容的 ASP.NET 版本。 (继承自 Control) |
| Site |
获取容器信息,该容器在呈现于设计图面上时承载当前控件。 (继承自 Control) |
| SkinID |
获取或设置要应用于控件的外观。 (继承自 Control) |
| TemplateControl |
获取或设置对包含该控件的模板的引用。 (继承自 Control) |
| TemplateSourceDirectory |
获取包含当前服务器控件的 Page 或 UserControl 的虚拟目录。 (继承自 Control) |
| UniqueID |
获取服务器控件的唯一的、以分层形式限定的标识符。 (继承自 Control) |
| ValidateRequestMode |
获取或设置指示控件是否检查来自浏览器的客户端输入是否具有潜在危险值的值。 (继承自 Control) |
| ViewState |
获取状态信息的字典,这些信息使您可以在同一页的多个请求间保存和还原服务器控件的视图状态。 (继承自 Control) |
| ViewStateIgnoresCase |
获取一个值,该值指示 StateBag 对象是否不区分大小写。 (继承自 Control) |
| ViewStateMode |
获取或设置此控件的视图状态模式。 (继承自 Control) |
| Visible |
获取或设置一个值,该值指示服务器控件是否作为 UI 呈现在页上。 (继承自 Control) |
方法
事件
| DataBinding |
当服务器控件绑定到数据源时发生。 (继承自 Control) |
| Disposed |
当从内存释放服务器控件时发生,这是请求 ASP.NET 页时服务器控件生存期的最后阶段。 (继承自 Control) |
| Init |
当服务器控件初始化时发生;初始化是控件生存期的第一步。 (继承自 Control) |
| Load |
当服务器控件加载到 Page 对象中时发生。 (继承自 Control) |
| PreRender |
在加载 Control 对象之后、呈现之前发生。 (继承自 Control) |
| Unload |
当服务器控件从内存中卸载时发生。 (继承自 Control) |
显式接口实现
| IControlBuilderAccessor.ControlBuilder |
有关此成员的说明,请参见 ControlBuilder。 (继承自 Control) |
| IControlDesignerAccessor.GetDesignModeState() |
有关此成员的说明,请参见 GetDesignModeState()。 (继承自 Control) |
| IControlDesignerAccessor.SetDesignModeState(IDictionary) |
有关此成员的说明,请参见 SetDesignModeState(IDictionary)。 (继承自 Control) |
| IControlDesignerAccessor.SetOwnerControl(Control) |
有关此成员的说明,请参见 SetOwnerControl(Control)。 (继承自 Control) |
| IControlDesignerAccessor.UserData |
有关此成员的说明,请参见 UserData。 (继承自 Control) |
| IDataBindingsAccessor.DataBindings |
有关此成员的说明,请参见 DataBindings。 (继承自 Control) |
| IDataBindingsAccessor.HasDataBindings |
有关此成员的说明,请参见 HasDataBindings。 (继承自 Control) |
| IExpressionsAccessor.Expressions |
有关此成员的说明,请参见 Expressions。 (继承自 Control) |
| IExpressionsAccessor.HasExpressions |
有关此成员的说明,请参见 HasExpressions。 (继承自 Control) |
| IParserAccessor.AddParsedSubObject(Object) |
有关此成员的说明,请参见 AddParsedSubObject(Object)。 (继承自 Control) |
扩展方法
| FindDataSourceControl(Control) |
返回与指定控件的数据控件关联的数据源。 |
| FindFieldTemplate(Control, String) |
返回指定控件的命名容器中指定列的字段模板。 |
| FindMetaTable(Control) |
返回包含数据控件的元表对象。 |
| GetDefaultValues(INamingContainer) |
为指定数据控件获取默认值的集合。 |
| GetMetaTable(INamingContainer) |
为指定数据控件获取表元数据。 |
| SetMetaTable(INamingContainer, MetaTable) |
为指定数据控件设置表元数据。 |
| SetMetaTable(INamingContainer, MetaTable, IDictionary<String,Object>) |
为指定数据控件设置表元数据和默认值映射。 |
| SetMetaTable(INamingContainer, MetaTable, Object) |
为指定数据控件设置表元数据和默认值映射。 |
| TryGetMetaTable(INamingContainer, MetaTable) |
确定表元数据是否可用。 |
| EnableDynamicData(INamingContainer, Type) |
为指定数据控件启用动态数据行为。 |
| EnableDynamicData(INamingContainer, Type, IDictionary<String,Object>) |
为指定数据控件启用动态数据行为。 |
| EnableDynamicData(INamingContainer, Type, Object) |
为指定数据控件启用动态数据行为。 |