다음을 통해 공유


ListView.SelectedIndexChanging 이벤트

정의

항목의 선택 단추를 클릭할 때 ListView 컨트롤에서 선택 작업을 처리하기 전에 발생합니다.

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) 

이벤트 유형

예제

다음 예제에서는 이벤트에 대 한 이벤트 처리기를 만드는 방법을 보여 있습니다 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="|&lt;&lt; " LastPageText=" &gt;&gt;|"
                NextPageText=" &gt; " PreviousPageText=" &lt; " />
            </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>&nbsp;</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="|&lt;&lt; " LastPageText=" &gt;&gt;|"
                NextPageText=" &gt; " PreviousPageText=" &lt; " />
            </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>&nbsp;</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>

설명

이벤트는 SelectedIndexChanging 항목의 선택 단추를 클릭할 때 발생하지만 컨트롤이 ListView 선택 작업을 처리하기 전에 발생합니다. (선택 단추는 속성이 CommandName "선택"으로 설정된 단추 컨트롤입니다.) 이렇게 하면 이 이벤트가 발생할 때마다 선택 작업을 취소하는 등 사용자 지정 루틴을 수행할 수 있습니다.

참고

이 이벤트는 프로그래밍 방식으로 속성을 설정할 SelectedIndex 때 발생하지 않습니다.

ListViewSelectEventArgs 개체가 이벤트 처리기에 전달되므로 사용자가 선택한 항목의 인덱스를 확인할 수 있습니다. 선택 작업을 취소해야 함을 나타낼 수도 있습니다. 선택 작업을 취소 하려면 합니다 Cancel 의 속성을 ListViewSelectEventArgs 개체를 true입니다.

이벤트를 처리 하는 방법에 대 한 자세한 내용은 참조 하세요. 이벤트 처리 및 발생합니다.

적용 대상

추가 정보