ListView.SelectedIndexChanged 이벤트
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
항목의 선택 단추를 클릭할 때 ListView 컨트롤에서 선택 작업을 처리한 후 발생합니다.
public:
event EventHandler ^ SelectedIndexChanged;
public event EventHandler SelectedIndexChanged;
member this.SelectedIndexChanged : EventHandler
Public Custom Event SelectedIndexChanged As EventHandler
이벤트 유형
예제
다음 예제에서는 이벤트에 대 한 이벤트 처리기를 만드는 방법을 보여 입니다 SelectedIndexChanged .
<%@ 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">
//<Snippet3>
void ContactsListView_SelectedIndexChanged(Object sender, EventArgs e)
{
if (ContactsListView.SelectedIndex >= 0)
ViewState["SelectedKey"] = ContactsListView.SelectedValue;
else
ViewState["SelectedKey"] = null;
}
void ContactsListView_DataBound(Object sender, EventArgs e)
{
for (int i = 0; i < ContactsListView.Items.Count; i++)
{
// Ignore values that cannot be cast as integer.
try
{
if ((int)ContactsListView.DataKeys[i].Value == (int)ViewState["SelectedKey"])
ContactsListView.SelectedIndex = i;
}
catch { }
}
}
//</Snippet3>
void ContactsListView_PagePropertiesChanged(Object sender, EventArgs e)
{
ContactsListView.SelectedIndex = -1;
}
void ContactsListView_Sorting(Object sender, ListViewSortEventArgs e)
{
ContactsListView.SelectedIndex = -1;
}
void ClearButton_Click(Object sender, EventArgs e)
{
ViewState["SelectedKey"] = null;
ContactsListView.SelectedIndex = -1;
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>ListView SelectedIndex Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>ListView SelectedIndex Example</h3>
<asp:ListView ID="ContactsListView"
DataSourceID="ContactsDataSource"
DataKeyNames="ContactID"
OnDataBound="ContactsListView_DataBound"
OnSelectedIndexChanged="ContactsListView_SelectedIndexChanged"
OnPagePropertiesChanged="ContactsListView_PagePropertiesChanged"
OnSorting="ContactsListView_Sorting"
runat="server">
<LayoutTemplate>
<asp:Button runat="server" ID="SortButton"
CommandArgument="LastName" CommandName="Sort" Text="Sort" />
<asp:Button runat="server" ID="ClearButton"
OnClick="ClearButton_Click" Text="Clear Selection" /><br />
<table cellpadding="2" border="1" runat="server" id="tblContacts" width="640px">
<tr runat="server" id="itemPlaceholder" />
</table>
<asp:DataPager runat="server" ID="PeopleDataPager" PageSize="20">
<Fields>
<asp:NextPreviousPagerField
ShowFirstPageButton="true" ShowLastPageButton="true"
FirstPageText="|<< " LastPageText=" >>|"
NextPageText=" > " PreviousPageText=" < " />
</Fields>
</asp:DataPager>
</LayoutTemplate>
<ItemTemplate>
<tr runat="server">
<td valign="top">
<asp:Label ID="FirstNameLabel" runat="server" Text='<%#Eval("FirstName") %>' />
<asp:Label ID="LastNameLabel" runat="server" Text='<%#Eval("LastName") %>' />
</td>
<td>
<asp:Label ID="EmailLabel" runat="server" Text='<%#Eval("EmailAddress") %>' />
</td>
<td>
<asp:LinkButton ID="SelectButton" runat="server" CommandName="Select" Text="Select" />
</td>
</tr>
</ItemTemplate>
<SelectedItemTemplate>
<tr runat="server" style="background-color:#B0C4DE">
<td valign="top">
<asp:Label ID="FirstNameLabel" runat="server" Text='<%#Eval("FirstName") %>' />
<asp:Label ID="LastNameLabel" runat="server" Text='<%#Eval("LastName") %>' />
</td>
<td>
<asp:Label ID="EmailLabel" runat="server" Text='<%#Eval("EmailAddress") %>' />
</td>
<td> </td>
</tr>
</SelectedItemTemplate>
</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], [EmailAddress]
FROM Person.Contact">
</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">
'<Snippet3>
Sub ContactsListView_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
If ContactsListView.SelectedIndex >= 0 Then
ViewState("SelectedKey") = ContactsListView.SelectedValue
Else
ViewState("SelectedKey") = Nothing
End If
End Sub
Sub ContactsListView_DataBound(ByVal sender As Object, ByVal e As EventArgs)
For i As Integer = 0 To ContactsListView.Items.Count - 1
' Ignore values that cannot be cast as integer.
Try
If Convert.ToInt32(ContactsListView.DataKeys(i).Value) = Convert.ToInt32(ViewState("SelectedKey")) Then _
ContactsListView.SelectedIndex = i
Catch
End Try
Next
End Sub
'</Snippet3>
Sub ContactsListView_PagePropertiesChanged(ByVal sender As Object, ByVal e As EventArgs)
ContactsListView.SelectedIndex = -1
End Sub
Sub ContactsListView_Sorting(ByVal sender As Object, ByVal e As ListViewSortEventArgs)
ContactsListView.SelectedIndex = -1
End Sub
Sub ClearButton_Click(ByVal sender As Object, ByVal e As EventArgs)
ViewState("SelectedKey") = Nothing
ContactsListView.SelectedIndex = -1
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>ListView SelectedIndex Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>ListView SelectedIndex Example</h3>
<asp:ListView ID="ContactsListView"
DataSourceID="ContactsDataSource"
DataKeyNames="ContactID"
OnDataBound="ContactsListView_DataBound"
OnSelectedIndexChanged="ContactsListView_SelectedIndexChanged"
OnPagePropertiesChanged="ContactsListView_PagePropertiesChanged"
OnSorting="ContactsListView_Sorting"
runat="server">
<LayoutTemplate>
<asp:Button runat="server" ID="SortButton"
CommandArgument="LastName" CommandName="Sort" Text="Sort" />
<asp:Button runat="server" ID="ClearButton"
OnClick="ClearButton_Click" Text="Clear Selection" /><br />
<table cellpadding="2" border="1" runat="server" id="tblContacts" width="640px">
<tr runat="server" id="itemPlaceholder" />
</table>
<asp:DataPager runat="server" ID="PeopleDataPager" PageSize="20">
<Fields>
<asp:NextPreviousPagerField
ShowFirstPageButton="true" ShowLastPageButton="true"
FirstPageText="|<< " LastPageText=" >>|"
NextPageText=" > " PreviousPageText=" < " />
</Fields>
</asp:DataPager>
</LayoutTemplate>
<ItemTemplate>
<tr runat="server">
<td valign="top">
<asp:Label ID="FirstNameLabel" runat="server" Text='<%#Eval("FirstName") %>' />
<asp:Label ID="LastNameLabel" runat="server" Text='<%#Eval("LastName") %>' />
</td>
<td>
<asp:Label ID="EmailLabel" runat="server" Text='<%#Eval("EmailAddress") %>' />
</td>
<td>
<asp:LinkButton ID="SelectButton" runat="server" CommandName="Select" Text="Select" />
</td>
</tr>
</ItemTemplate>
<SelectedItemTemplate>
<tr runat="server" style="background-color:#B0C4DE">
<td valign="top">
<asp:Label ID="FirstNameLabel" runat="server" Text='<%#Eval("FirstName") %>' />
<asp:Label ID="LastNameLabel" runat="server" Text='<%#Eval("LastName") %>' />
</td>
<td>
<asp:Label ID="EmailLabel" runat="server" Text='<%#Eval("EmailAddress") %>' />
</td>
<td> </td>
</tr>
</SelectedItemTemplate>
</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], [EmailAddress]
FROM Person.Contact">
</asp:SqlDataSource>
</form>
</body>
</html>
설명
이벤트는 SelectedIndexChanged 컨트롤이 선택 작업을 처리한 후 ListView 항목의 선택 단추를 클릭할 때 발생합니다. (선택 단추는 속성이 CommandName "선택"으로 설정된 단추 컨트롤입니다.) 이렇게 하면 현재 선택한 항목에 대한 정보를 사용하여 상태 레이블을 업데이트하는 등 이 이벤트가 발생할 때마다 사용자 지정 루틴을 수행할 수 있습니다.
이벤트를 처리 하는 방법에 대 한 자세한 내용은 참조 하세요. 이벤트 처리 및 발생합니다.
적용 대상
추가 정보
GitHub에서 Microsoft와 공동 작업
이 콘텐츠의 원본은 GitHub에서 찾을 수 있으며, 여기서 문제와 끌어오기 요청을 만들고 검토할 수도 있습니다. 자세한 내용은 참여자 가이드를 참조하세요.
.NET