다음을 통해 공유


DataListCommandEventArgs.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 사용자가 선택한 명령을 결정 하는 속성입니다. 그런 다음 명령에 따라 적절 한 조치를 수행 합니다.

<%@ Page Language="C#" AutoEventWireup="True" %>
<%@ Import Namespace="System.Data" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>DataListCommandEventArgs Example</title>
<script language="c#" runat="server">
 
      ICollection CreateDataSource() 
      {
         
         DataTable dt = new DataTable();
         DataRow dr;
 
         // Create a DataTable.
         dt.Columns.Add(new DataColumn("IntegerValue", typeof(Int32)));
         dt.Columns.Add(new DataColumn("StringValue", typeof(string)));
         dt.Columns.Add(new DataColumn("DateTimeValue", typeof(DateTime)));
 
         // Create sample data.
         for (int i = 1; i <= 9; i++) 
         {
            dr = dt.NewRow();
            dr[0] = i;
            dr[1] = "Item " + i.ToString();
            dr[2] = DateTime.Now.ToShortTimeString();
            dt.Rows.Add(dr);
         }
          
 
         // Return a DataView to the DataTable.
         DataView dv = new DataView(dt);
         return dv;
         
      }
 
      void Page_Load(Object sender, EventArgs e) 
      {
         
         if (!IsPostBack)
            BindList();
 
      }
 
      void BindList() 
      {

         DataList1.DataSource = CreateDataSource();
         DataList1.DataBind();

      }
     
      void DataList_ItemCommand(object sender, DataListCommandEventArgs e) 
      {          
         if (((LinkButton)e.CommandSource).CommandName == "select")
            DataList1.SelectedIndex = e.Item.ItemIndex;
         
         BindList();
      }
 
   </script>
 
</head>
<body>
 
   <form id="form1" runat="server">

      <h3>DataListCommandEventArgs Example</h3>
 
      <asp:DataList id="DataList1"                                                
                    GridLines="Both"                                                       
                    OnItemCommand="DataList_ItemCommand"
                    runat="server">

         <HeaderTemplate>
            Items
         </HeaderTemplate>

         <ItemTemplate>
            <asp:LinkButton id="button1"
                            Text="Show details" 
                            CommandName="select" 
                            runat="server"/>
            <%# DataBinder.Eval(Container.DataItem, "StringValue") %>
         </ItemTemplate>

         <SelectedItemTemplate>
            Item:
            <%# DataBinder.Eval(Container.DataItem, "StringValue") %>
            <br />
            Order Date:
            <%# DataBinder.Eval(Container.DataItem, "DateTimeValue", "{0:d}") %>
            <br />
            Quantity:
            <%# DataBinder.Eval(Container.DataItem, "IntegerValue", "{0:N1}") %>
            <br />
         </SelectedItemTemplate>
 
      </asp:DataList>

   </form>
 
</body>
</html>
<%@ Page Language="VB" AutoEventWireup="True" %>
<%@ Import Namespace="System.Data" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>DataListCommandEventArgs Example</title>
<script language="VB" runat="server">
 
    Function CreateDataSource() As ICollection
        
        Dim dt As New DataTable()
        Dim dr As DataRow
        
        ' Create a DataTable.
        dt.Columns.Add(New DataColumn("IntegerValue", GetType(Int32)))
        dt.Columns.Add(New DataColumn("StringValue", GetType(String)))
        dt.Columns.Add(New DataColumn("DateTimeValue", GetType(DateTime)))
        
        ' Create sample data.
        Dim i As Integer
        For i = 1 To 9
            dr = dt.NewRow()
            dr(0) = i
            dr(1) = "Item " & i.ToString()
            dr(2) = DateTime.Now.ToShortTimeString()
            dt.Rows.Add(dr)
        Next i
        
        
        ' Return a DataView to the DataTable.
        Dim dv As New DataView(dt)
        Return dv
    End Function 'CreateDataSource
     

    Sub Page_Load(sender As Object, e As EventArgs)
        
        If Not IsPostBack Then
            BindList()
        End If 
    End Sub 'Page_Load


    Sub BindList()
        
        DataList1.DataSource = CreateDataSource()
        DataList1.DataBind()
    End Sub 'BindList
     

    Sub DataList_ItemCommand(sender As Object, e As DataListCommandEventArgs)
        If CType(e.CommandSource, LinkButton).CommandName = "select" Then
            DataList1.SelectedIndex = e.Item.ItemIndex
        End If 
        BindList()
    End Sub 'DataList_ItemCommand
     
   </script>
 
</head>
<body>
 
   <form id="form1" runat="server">

      <h3>DataListCommandEventArgs Example</h3>
 
      <asp:DataList id="DataList1"                                                
                    GridLines="Both"                                                       
                    OnItemCommand="DataList_ItemCommand"
                    runat="server">

         <HeaderTemplate>
            Items
         </HeaderTemplate>

         <ItemTemplate>
            <asp:LinkButton id="button1"
                            Text="Show details" 
                            CommandName="select" 
                            runat="server"/>
            <%# DataBinder.Eval(Container.DataItem, "StringValue") %>
         </ItemTemplate>

         <SelectedItemTemplate>
            Item:
            <%# DataBinder.Eval(Container.DataItem, "StringValue") %>
            <br />
            Order Date:
            <%# DataBinder.Eval(Container.DataItem, "DateTimeValue", "{0:d}") %>
            <br />
            Quantity:
            <%# DataBinder.Eval(Container.DataItem, "IntegerValue", "{0:N1}") %>
            <br />
         </SelectedItemTemplate>
 
      </asp:DataList>

   </form>
 
</body>
</html>

설명

사용 된 CommandSource 이벤트를 발생 시킨 명령 소스를 확인 하는 속성입니다. 이 속성은 이벤트를 발생 시킨 명령을 확인 하려면 일반적으로 사용 됩니다. 명령에 따라 적절 한 작업을 수행할 수 있습니다.

적용 대상

추가 정보