ListViewSelectEventArgs.NewSelectedIndex 屬性

定義

取得或設定要在 ListView 控制項中選取之新項目的索引。

public:
 property int NewSelectedIndex { int get(); void set(int value); };
public int NewSelectedIndex { get; set; }
member this.NewSelectedIndex : int with get, set
Public Property NewSelectedIndex As Integer

屬性值

要在 ListView 控制項中選取之新項目的索引。

範例

下列範例示範如何使用 NewSelectedIndex 物件的 屬性 ListViewSelectEventArgs 來存取使用者選取的專案。

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;
  }
}
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

備註

事件發生 ListView.SelectedIndexChanging 在控制項執行選取作業之前 ListView 。 因此,您無法使用 ListView.SelectedIndex 控制項的 屬性來判斷使用者所選取之新專案的索引。 屬性 ListView.SelectedIndex 包含先前選取專案的索引。 若要判斷使用者所選取之新專案的索引,請使用 NewSelectedIndex 屬性。 您也可以使用這個屬性,將選取的專案索引設定為另一個值,以程式設計方式覆寫選取的專案索引。

適用於

另請參閱