ListViewSelectEventArgs 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
提供 SelectedIndexChanging 事件的資料。
public ref class ListViewSelectEventArgs : System::ComponentModel::CancelEventArgs
public class ListViewSelectEventArgs : System.ComponentModel.CancelEventArgs
type ListViewSelectEventArgs = class
inherit CancelEventArgs
Public Class ListViewSelectEventArgs
Inherits CancelEventArgs
- 繼承
範例
下列範例示範如何使用 ListViewSelectEventArgs 傳遞至 SelectedIndexChanging 事件的物件,在選取的專案已停止時取消選取作業。
<%@ 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">
void Page_Load()
{
Message.Text = String.Empty;
}
//<Snippet2>
void ProductsListView_SelectedIndexChanging(Object sender, ListViewSelectEventArgs e)
{
ListViewItem item = (ListViewItem)ProductsListView.Items[e.NewSelectedIndex];
Label l = (Label)item.FindControl("DiscontinuedDateLabel");
if (String.IsNullOrEmpty(l.Text))
{
return;
}
DateTime discontinued = DateTime.Parse(l.Text);
if (discontinued < DateTime.Now)
{
Message.Text = "You cannot select a discontinued item.";
e.Cancel = true;
}
}
//</Snippet2>
protected void ProductsListView_PagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e)
{
// Clear selection.
ProductsListView.SelectedIndex = -1;
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>ListView.SelectedIndexChanging Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>ListView.SelectedIndexChanging Example</h3>
<asp:Label ID="Message"
ForeColor="Red"
runat="server"/>
<br/>
<asp:ListView ID="ProductsListView"
DataSourceID="ProductsDataSource"
DataKeyNames="ProductID"
OnSelectedIndexChanging="ProductsListView_SelectedIndexChanging"
OnPagePropertiesChanging="ProductsListView_PagePropertiesChanging"
runat="server" >
<LayoutTemplate>
<table cellpadding="2" runat="server" id="tblProducts" width="640px">
<tr runat="server" id="itemPlaceholder" />
</table>
<asp:DataPager runat="server" ID="ProductsDataPager" PageSize="12">
<Fields>
<asp:NextPreviousPagerField
ShowFirstPageButton="true" ShowLastPageButton="true"
FirstPageText="|<< " LastPageText=" >>|"
NextPageText=" > " PreviousPageText=" < " />
</Fields>
</asp:DataPager>
</LayoutTemplate>
<ItemTemplate>
<tr runat="server">
<td valign="top">
<asp:LinkButton ID="SelectButton" runat="server" Text="..." CommandName="Select" />
</td>
<td valign="top">
<asp:Label ID="NameLabel" runat="server" Text='<%#Eval("Name") %>' />
</td>
<td valign="top">
<asp:Label ID="ProductNumberLabel" runat="server" Text='<%#Eval("ProductNumber") %>' />
</td>
<td>
<asp:Label ID="DiscontinuedDateLabel" runat="server" Text='<%#Eval("DiscontinuedDate", "{0:d}") %>' />
</td>
</tr>
</ItemTemplate>
<SelectedItemTemplate>
<tr runat="server" style="background-color:#ADD8E6">
<td> </td>
<td valign="top">
<asp:Label ID="NameLabel" runat="server" Text='<%#Eval("Name") %>' />
</td>
<td valign="top">
<asp:Label ID="ProductNumberLabel" runat="server" Text='<%#Eval("ProductNumber") %>' />
</td>
<td>
<asp:Label ID="DiscontinuedDateLabel" runat="server" Text='<%#Eval("DiscontinuedDate", "{0:d}") %>' />
</td>
</tr>
</SelectedItemTemplate>
</asp:ListView>
<asp:SqlDataSource ID="ProductsDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:AdventureWorks_DataConnectionString %>"
SelectCommand="SELECT [ProductID], [Name], [ProductNumber], [DiscontinuedDate]
FROM Production.Product"
UpdateCommand="UPDATE Production.Product
SET Name = @Name, ProductNumber = @ProductNumber, DiscontinuedDate = @DiscontinuedDate
WHERE ProductID = @ProductID">
</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">
Sub Page_Load()
Message.Text = String.Empty
End Sub
'<Snippet2>
Sub ProductsListView_SelectedIndexChanging(ByVal sender As Object, ByVal e As ListViewSelectEventArgs)
Dim item As ListViewItem = CType(ProductsListView.Items(e.NewSelectedIndex), ListViewItem)
Dim l As Label = CType(item.FindControl("DiscontinuedDateLabel"), Label)
If String.IsNullOrEmpty(l.Text) Then
Return
End If
Dim discontinued As DateTime = DateTime.Parse(l.Text)
If discontinued < DateTime.Now Then
Message.Text = "You cannot select a discontinued item."
e.Cancel = True
End If
End Sub
'</Snippet2>
Protected Sub ProductsListView_PagePropertiesChanging(ByVal sender As Object, _
ByVal e As PagePropertiesChangingEventArgs)
' Clears the selection.
ProductsListView.SelectedIndex = -1
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>ListView.SelectedIndexChanging Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>ListView.SelectedIndexChanging Example</h3>
<asp:Label ID="Message"
ForeColor="Red"
runat="server"/>
<br/>
<asp:ListView ID="ProductsListView"
DataSourceID="ProductsDataSource"
DataKeyNames="ProductID"
OnSelectedIndexChanging="ProductsListView_SelectedIndexChanging"
OnPagePropertiesChanging="ProductsListView_PagePropertiesChanging"
runat="server">
<LayoutTemplate>
<table cellpadding="2" runat="server" id="tblProducts" width="640px">
<tr runat="server" id="itemPlaceholder" />
</table>
<asp:DataPager runat="server" ID="ProductsDataPager" PageSize="12">
<Fields>
<asp:NextPreviousPagerField
ShowFirstPageButton="true" ShowLastPageButton="true"
FirstPageText="|<< " LastPageText=" >>|"
NextPageText=" > " PreviousPageText=" < " />
</Fields>
</asp:DataPager>
</LayoutTemplate>
<ItemTemplate>
<tr runat="server">
<td valign="top">
<asp:LinkButton ID="SelectButton" runat="server" Text="..." CommandName="Select" />
</td>
<td valign="top">
<asp:Label ID="NameLabel" runat="server" Text='<%#Eval("Name") %>' />
</td>
<td valign="top">
<asp:Label ID="ProductNumberLabel" runat="server" Text='<%#Eval("ProductNumber") %>' />
</td>
<td>
<asp:Label ID="DiscontinuedDateLabel" runat="server" Text='<%#Eval("DiscontinuedDate", "{0:d}") %>' />
</td>
</tr>
</ItemTemplate>
<SelectedItemTemplate>
<tr runat="server" style="background-color:#ADD8E6">
<td> </td>
<td valign="top">
<asp:Label ID="NameLabel" runat="server" Text='<%#Eval("Name") %>' />
</td>
<td valign="top">
<asp:Label ID="ProductNumberLabel" runat="server" Text='<%#Eval("ProductNumber") %>' />
</td>
<td>
<asp:Label ID="DiscontinuedDateLabel" runat="server" Text='<%#Eval("DiscontinuedDate", "{0:d}") %>' />
</td>
</tr>
</SelectedItemTemplate>
</asp:ListView>
<asp:SqlDataSource ID="ProductsDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:AdventureWorks_DataConnectionString %>"
SelectCommand="SELECT [ProductID], [Name], [ProductNumber], [DiscontinuedDate]
FROM Production.Product"
UpdateCommand="UPDATE Production.Product
SET Name = @Name, ProductNumber = @ProductNumber, DiscontinuedDate = @DiscontinuedDate
WHERE ProductID = @ProductID">
</asp:SqlDataSource>
</form>
</body>
</html>
備註
當單擊 [選取] 按鈕,但在控件處理選取作業之前ListView,控件ListView就會引發 SelectedIndexChanging 事件。 (選取按鈕是 CommandName
屬性設定為 「Select」.) 這可讓您提供事件處理方法,以在發生此事件時執行自定義例程,例如取消選取作業。
ListViewSelectEventArgs物件會傳遞至事件處理方法,可讓您判斷使用者所選取專案的索引。 它也可讓您取消選取作業。 若要取消選取工作,請將 Cancel 物件的 屬性 ListViewSelectEventArgs 設定為 true
。
如需 ListViewSelectEventArgs 類別之執行個體的初始屬性值清單,請參閱 ListViewSelectEventArgs 建構函式。
建構函式
ListViewSelectEventArgs(Int32) |
初始化 ListViewSelectEventArgs 類別的新執行個體。 |
屬性
Cancel |
取得或設定值,這個值表示是否應該取消事件。 (繼承來源 CancelEventArgs) |
NewSelectedIndex |
取得或設定要在 ListView 控制項中選取之新項目的索引。 |
方法
Equals(Object) |
判斷指定的物件是否等於目前的物件。 (繼承來源 Object) |
GetHashCode() |
做為預設雜湊函式。 (繼承來源 Object) |
GetType() |
取得目前執行個體的 Type。 (繼承來源 Object) |
MemberwiseClone() |
建立目前 Object 的淺層複製。 (繼承來源 Object) |
ToString() |
傳回代表目前物件的字串。 (繼承來源 Object) |