ListView.SelectedIndexChanging Evento
Definição
Importante
Algumas informações se referem a produtos de pré-lançamento que podem ser substancialmente modificados antes do lançamento. A Microsoft não oferece garantias, expressas ou implícitas, das informações aqui fornecidas.
Ocorre quando o botão Seleção de um item é clicado, mas antes do controle ListView manipular a operação de seleção.
public:
event EventHandler<System::Web::UI::WebControls::ListViewSelectEventArgs ^> ^ SelectedIndexChanging;
public event EventHandler<System.Web.UI.WebControls.ListViewSelectEventArgs> SelectedIndexChanging;
member this.SelectedIndexChanging : EventHandler<System.Web.UI.WebControls.ListViewSelectEventArgs>
Public Custom Event SelectedIndexChanging As EventHandler(Of ListViewSelectEventArgs)
Tipo de evento
Exemplos
O exemplo a seguir mostra como criar um manipulador de eventos para o SelectedIndexChanging evento.
<%@ 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>
Comentários
O SelectedIndexChanging evento é acionado quando o botão Selecionar de um item é clicado, mas antes que o ListView controle manipule a operação de seleção. (Um botão Selecionar é um controle de botão cuja CommandName propriedade está definida como "Select".) Isso permite que você execute uma rotina personalizada sempre que esse evento ocorrer, como cancelar a operação de seleção.
Observação
Esse evento não é gerado quando você define programaticamente a SelectedIndex propriedade.
Um ListViewSelectEventArgs objeto é passado para o manipulador de eventos, o que permite determinar o índice do item que foi selecionado pelo usuário. Ele também permite que você indique que a operação de seleção deve ser cancelada. Para cancelar a operação de seleção, defina a Cancel propriedade do ListViewSelectEventArgs objeto como true
.
Para obter mais informações sobre como lidar com eventos, consulte Manipulando e levantando eventos.