ListViewCommandEventArgs.Item Property
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Gets the data item for which the command was issued.
public:
property System::Web::UI::WebControls::ListViewItem ^ Item { System::Web::UI::WebControls::ListViewItem ^ get(); };
public System.Web.UI.WebControls.ListViewItem Item { get; }
member this.Item : System.Web.UI.WebControls.ListViewItem
Public ReadOnly Property Item As ListViewItem
Property Value
The data item from the ListView control for which the command was issued.
Examples
The following example shows how to use the ListViewCommandEventArgs object that is passed to the event handler.
<%@ 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 EmployeesListView_OnItemCommand(object sender, ListViewCommandEventArgs e)
{
if (String.Equals(e.CommandName, "AddToList"))
{
// Verify that the employee ID is not already in the list. If not, add the
// employee to the list.
ListViewDataItem dataItem = (ListViewDataItem)e.Item;
string employeeID =
EmployeesListView.DataKeys[dataItem.DisplayIndex].Value.ToString();
if (SelectedEmployeesListBox.Items.FindByValue(employeeID) == null)
{
ListItem item = new ListItem(e.CommandArgument.ToString(), employeeID);
SelectedEmployeesListBox.Items.Add(item);
}
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Employee List</title>
</head>
<body>
<form id="form1" runat="server">
<asp:ListView runat="server"
ID="EmployeesListView"
OnItemCommand="EmployeesListView_OnItemCommand"
DataSourceID="EmployeesDataSource"
DataKeyNames="EmployeeID">
<LayoutTemplate>
<table runat="server" id="tblEmployees"
cellspacing="0" cellpadding="1" width="440px" border="1">
<tr id="itemPlaceholder" runat="server"></tr>
</table>
<asp:DataPager ID="EmployeesDataPager" runat="server" PageSize="10">
<Fields>
<asp:NumericPagerField />
</Fields>
</asp:DataPager>
</LayoutTemplate>
<ItemTemplate>
<tr runat="server">
<td>
<asp:Label runat="server" ID="NameLabel"
Text='<%#Eval("LastName") + ", " + Eval("FirstName") %>' />
</td>
<td style="width:80px">
<asp:LinkButton runat="server"
ID="SelectEmployeeButton"
Text="Add To List"
CommandName="AddToList"
CommandArgument='<%#Eval("LastName") + ", " + Eval("FirstName") %>' />
</td>
</tr>
</ItemTemplate>
</asp:ListView>
<br /><br />
<b>Selected Employees:</b><br />
<asp:ListBox runat="server" ID="SelectedEmployeesListBox" Rows="10" Width="300px" />
<!-- 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="EmployeesDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:AdventureWorks_DataConnectionString %>"
SelectCommand="SELECT [EmployeeID], [FirstName], [LastName]
FROM HumanResources.vEmployee
ORDER BY [LastName], [FirstName], [EmployeeID]">
</asp:SqlDataSource>
</form>
</body>
</html>
<%@ 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 EmployeesListView_OnItemCommand(ByVal sender As Object, _
ByVal e As ListViewCommandEventArgs)
If String.Equals(e.CommandName, "AddToList") Then
' Verify that the employee ID is not already in the list. If not, add the
' employee to the list.
Dim dataItem As ListViewDataItem = CType(e.Item, ListViewDataItem)
Dim employeeID As String = _
EmployeesListView.DataKeys(dataItem.DisplayIndex).Value.ToString()
If SelectedEmployeesListBox.Items.FindByValue(employeeID) Is Nothing Then
Dim item As ListItem = _
New ListItem(e.CommandArgument.ToString(), employeeID)
SelectedEmployeesListBox.Items.Add(item)
End If
End If
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head2" runat="server">
<title>Employee List</title>
</head>
<body>
<form id="form1" runat="server">
<asp:ListView runat="server"
ID="EmployeesListView"
OnItemCommand="EmployeesListView_OnItemCommand"
DataSourceID="EmployeesDataSource"
DataKeyNames="EmployeeID">
<LayoutTemplate>
<table runat="server" id="tblEmployees"
cellspacing="0" cellpadding="1" width="440px" border="1">
<tr id="itemPlaceholder" runat="server"></tr>
</table>
<asp:DataPager ID="EmployeesDataPager" runat="server" PageSize="10">
<Fields>
<asp:NumericPagerField />
</Fields>
</asp:DataPager>
</LayoutTemplate>
<ItemTemplate>
<tr runat="server">
<td>
<asp:Label runat="server" ID="NameLabel"
Text='<%#Eval("LastName") & ", " & Eval("FirstName") %>' />
</td>
<td style="width:80px">
<asp:LinkButton runat="server"
ID="SelectEmployeeButton"
Text="Add To List"
CommandName="AddToList"
CommandArgument='<%#Eval("LastName") & ", " & Eval("FirstName") %>' />
</td>
</tr>
</ItemTemplate>
</asp:ListView>
<br /><br />
<b>Selected Employees:</b><br />
<asp:ListBox runat="server" ID="SelectedEmployeesListBox" Rows="10" Width="300px" />
<!-- 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="EmployeesDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:AdventureWorks_DataConnectionString %>"
SelectCommand="SELECT [EmployeeID], [FirstName], [LastName]
FROM HumanResources.vEmployee
ORDER BY [LastName], [FirstName], [EmployeeID]">
</asp:SqlDataSource>
</form>
</body>
</html>
Applies to
See also
GitHub에서 Microsoft와 공동 작업
이 콘텐츠의 원본은 GitHub에서 찾을 수 있으며, 여기서 문제와 끌어오기 요청을 만들고 검토할 수도 있습니다. 자세한 내용은 참여자 가이드를 참조하세요.
.NET
피드백
https://aka.ms/ContentUserFeedback
출시 예정: 2024년 내내 콘텐츠에 대한 피드백 메커니즘으로 GitHub 문제를 단계적으로 폐지하고 이를 새로운 피드백 시스템으로 바꿀 예정입니다. 자세한 내용은 다음을 참조하세요.다음에 대한 사용자 의견 제출 및 보기