다음을 통해 공유


DetailsViewCommandEventArgs.CommandSource 속성

정의

명령 소스를 가져옵니다.

public:
 property System::Object ^ CommandSource { System::Object ^ get(); };
public object CommandSource { get; }
member this.CommandSource : obj
Public ReadOnly Property CommandSource As Object

속성 값

명령 소스를 나타내는 Object 클래스의 인스턴스입니다.

예제

다음 코드 예제를 사용 하는 방법에 설명 합니다 CommandSource 결정 하기 위해 두 개의 속성 DetailsView 발생 하는 컨트롤을 ItemCommand 이벤트입니다.

<%@ 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 ItemDetailsView_ItemCommand(Object sender, 
    DetailsViewCommandEventArgs e)
  {

    // Use the CommandName property to determine which button
    // was clicked. 
    if (e.CommandName == "Add")
    {
      
      // Because this event handler is used for two different
      // DetailsView controls, use the CommandSource property 
      // to get the DetailsView control that raised the event.
      DetailsView view = (DetailsView)e.CommandSource;
      
      // Determine which ListBox control to update based on the
      // ID of the DetailsView control that raised the event.
      ListBox list;
      if (view.ID == "CustomerDetailsView")
      {
        list = CustomerListBox;
      }
      else
      {
        list = EmployeesListBox;
      }

      // Add the selected item to the appropriate ListBox control. 

      // Get the row that contains the value to add. For this
      // example, get the second row (index 1) of the DetailsView 
      // control.
      DetailsViewRow row = view.Rows[1];

      // Get the value from the appropriate cell. For this
      // example, get the value of the second cell (index 1) 
      // of the row.
      String value = row.Cells[1].Text;

      // Create a ListItem object using the value.
      ListItem item = new ListItem(value);

      // Add the ListItem object to the ListBox control, if the 
      // item does not already exist.
      if (!list.Items.Contains(item))
      {
        list.Items.Add(item);
      }

    }

  }
  
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>DetailsViewCommandEventArgs CommandSource Example</title>
</head>
<body>
    <form id="form1" runat="server">
    
      <h3>DetailsViewCommandEventArgs CommandSource Example</h3>
  
      <table border="0">
        <tr valign="top">
          <td>
            <asp:detailsview id="CustomerDetailsView"
              datasourceid="CustomerSource"
              allowpaging="true"
              autogeneraterows="true" 
              onitemcommand="ItemDetailsView_ItemCommand"  
              runat="server">
              <fields>
                <asp:buttonfield buttontype="Link"
                  causesvalidation="false"
                  text="Add to List"
                  commandname="Add"/>
              </fields>
            </asp:detailsview>
          </td>
          <td>
            <asp:detailsview id="EmployeesDetailsView"
              datasourceid="EmployeesSource"
              allowpaging="true"
              autogeneraterows="true" 
              onitemcommand="ItemDetailsView_ItemCommand"  
              runat="server">
              <fields>
                <asp:buttonfield buttontype="Link"
                  causesvalidation="false"
                  text="Add to List"
                  commandname="Add"/>
              </fields>
            </asp:detailsview>
          </td>
        </tr>
        <tr>
          <td>
            Selected Customers:<br/>
            <asp:listbox id="CustomerListBox"
              runat="server"/>
          </td>
          <td>
            Selected Employees:<br/>
            <asp:listbox id="EmployeesListBox"
              runat="server"/>
          </td>
        </tr>
      </table>
      
      <!-- This example uses Microsoft SQL Server and connects  -->
      <!-- to the Northwind sample database. Use an ASP.NET     -->
      <!-- expression to retrieve the connection string value   -->
      <!-- from the Web.config file.                            -->
      <asp:sqldatasource id="CustomerSource"
        selectcommand="Select [CustomerID], [CompanyName], [Address], 
          [City], [PostalCode], [Country] From [Customers]"
        connectionstring=
            "<%$ ConnectionStrings:NorthWindConnectionString%>" 
        runat="server"/>  
        
      <asp:sqldatasource id="EmployeesSource"
        selectcommand="Select [EmployeeID], [FirstName], [LastName], 
          [Title] From [Employees]"
        connectionstring=
            "<%$ ConnectionStrings:NorthWindConnectionString%>" 
        runat="server"/>
  
    </form>
  </body>
</html>
<%@ page language="VB" autoeventwireup="false" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
  
  Sub ItemDetailsView_ItemCommand(ByVal sender As Object, _
    ByVal e As DetailsViewCommandEventArgs) _
    Handles CustomerDetailsView.ItemCommand, _
    EmployeesDetailsView.ItemCommand
   
    ' Use the CommandName property to determine which button
    ' was clicked. 
    If e.CommandName = "Add" Then
      
      ' Because this event handler is used for two different
      ' DetailsView controls, use the CommandSource property 
      ' to get the DetailsView control that raised the event.
      Dim view As DetailsView = CType(e.CommandSource, DetailsView)
      
      ' Determine which ListBox control to update based on the
      ' ID of the DetailsView control that raised the event.
      Dim list As ListBox
      If view.ID = "CustomerDetailsView" Then

        list = CustomerListBox
      
      Else
      
        list = EmployeesListBox
      
      End If

      ' Add the selected item to the appropriate ListBox control. 

      ' Get the row that contains the value to add. For this
      ' example, get the second row (index 1) of the DetailsView 
      ' control.
      Dim row As DetailsViewRow = view.Rows(1)

      ' Get the value from the appropriate cell. For this
      ' example, get the value of the second cell (index 1) 
      ' of the row.
      Dim value As String = row.Cells(1).Text

      ' Create a ListItem object using the value.
      Dim item As New ListItem(value)

      ' Add the ListItem object to the ListBox control, if the 
      ' item does not already exist.
      If Not list.Items.Contains(item) Then
      
        list.Items.Add(item)
        
      End If

    End If

  End Sub
  
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>DetailsViewCommandEventArgs CommandSource Example</title>
</head>
<body>
    <form id="form1" runat="server">
    
      <h3>DetailsViewCommandEventArgs CommandSource Example</h3>
  
      <table border="0">
        <tr valign="top">
          <td>
            <asp:detailsview id="CustomerDetailsView"
              datasourceid="CustomerSource"
              allowpaging="true"
              autogeneraterows="true" 
              onitemcommand="ItemDetailsView_ItemCommand"  
              runat="server">
              <fields>
                <asp:buttonfield buttontype="Link"
                  causesvalidation="false"
                  text="Add to List"
                  commandname="Add"/>
              </fields>
            </asp:detailsview>
          </td>
          <td>
            <asp:detailsview id="EmployeesDetailsView"
              datasourceid="EmployeesSource"
              allowpaging="true"
              autogeneraterows="true" 
              onitemcommand="ItemDetailsView_ItemCommand"  
              runat="server">
              <fields>
                <asp:buttonfield buttontype="Link"
                  causesvalidation="false"
                  text="Add to List"
                  commandname="Add"/>
              </fields>
            </asp:detailsview>
          </td>
        </tr>
        <tr>
          <td>
            Selected Customers:<br/>
            <asp:listbox id="CustomerListBox"
              runat="server"/>
          </td>
          <td>
            Selected Employees:<br/>
            <asp:listbox id="EmployeesListBox"
              runat="server"/>
          </td>
        </tr>
      </table>
      
      <!-- This example uses Microsoft SQL Server and connects  -->
      <!-- to the Northwind sample database. Use an ASP.NET     -->
      <!-- expression to retrieve the connection string value   -->
      <!-- from the Web.config file.                            -->
      <asp:sqldatasource id="CustomerSource"
        selectcommand="Select [CustomerID], [CompanyName], [Address], 
          [City], [PostalCode], [Country] From [Customers]"
        connectionstring=
            "<%$ ConnectionStrings:NorthWindConnectionString%>" 
        runat="server"/>  
        
      <asp:sqldatasource id="EmployeesSource"
        selectcommand="Select [EmployeeID], [FirstName], [LastName], 
          [Title] From [Employees]"
        connectionstring=
            "<%$ ConnectionStrings:NorthWindConnectionString%>" 
        runat="server"/>
  
    </form>
  </body>
</html>

설명

사용 된 CommandSource 명령 소스를 나타내는 개체의 속성에 액세스 하는 속성입니다. 이 경우에 따라 됩니다는 DetailsView 컨트롤 자체와 다른 경우에서는 Button 클릭 한 컨트롤입니다.

적용 대상

추가 정보