FormViewCommandEventArgs 클래스

정의

ItemCommand 이벤트에 대한 데이터를 제공합니다.

public ref class FormViewCommandEventArgs : System::Web::UI::WebControls::CommandEventArgs
public class FormViewCommandEventArgs : System.Web.UI.WebControls.CommandEventArgs
type FormViewCommandEventArgs = class
    inherit CommandEventArgs
Public Class FormViewCommandEventArgs
Inherits CommandEventArgs
상속
FormViewCommandEventArgs

예제

다음 예제에 사용 하는 방법을 보여 줍니다.를 FormViewCommandEventArgs 개체에 대 한 이벤트 처리 메서드에 전달 합니다 ItemCommand 이벤트는 단추 내에 FormView 컨트롤 사용자가 클릭 되었습니다.

중요

이 예제에는 사용자 입력을 허용하는 텍스트 상자가 있으므로 보안상 위험할 수 있습니다. 기본적으로 ASP.NET 웹 페이지는 사용자 입력 내용에 스크립트 또는 HTML 요소가 포함되어 있지 않은지 확인합니다. 자세한 내용은 Script Exploits Overview를 참조하세요.


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

    // The ItemCommand event is raised when any button within
    // the FormView control is clicked. Use the CommandName property 
    // to determine which button was clicked. 
    if (e.CommandName == "Add")
    {

      // Add the product to the ListBox control. 
      
      // Use the Row property to retrieve the data row.
      FormViewRow row = ProductFormView.Row;
      
      // Retrieve the ProductNameLabel control from
      // the data row.
      Label productNameLabel = (Label)row.FindControl("ProductNameLabel");

      // Retrieve the QuantityTextBox control from
      // the data row.
      TextBox quantityTextBox = (TextBox)row.FindControl("QuantityTextBox");

      if (productNameLabel != null && quantityTextBox != null)
      {    
        // Get the product name from the ProductNameLabel control.
        string name = productNameLabel.Text;
        
        // Get the quantity from the QuantityTextBox control.
        string quantity = quantityTextBox.Text;

        // Create the text to display in the ListBox control.
        string description = name + " - " + quantity + " Qty";

        // Create a ListItem object using the description and
        // product name.
        ListItem item = new ListItem(description, name);

        // Add the ListItem object to the ListBox.
        ProductListBox.Items.Add(item);

        // Use the CommandSource property to retrieve
        // the Add button. Disable the button after
        // the user adds the currently displayed employee
        // name to the ListBox control.
        Button addButton = (Button)e.CommandSource;
        addButton.Enabled = false;
      }

    }

  }

  void ProductFormView_DataBound(Object sender, EventArgs e)
  {
    
    // To prevent the user from adding duplicate items, 
    // disable the Add button if the item being bound to the 
    // FormView control is already in the ListBox control.
    
    // Use the Row property to retrieve the data row.
    FormViewRow row = ProductFormView.Row;

    // Retrieve the Add button from the data row.
    Button addButton = (Button)row.FindControl("AddButton");

    // Retrieve the ProductNameLabel control from
    // data row.
    Label productNameLabel = (Label)row.FindControl("ProductNameLabel");

    if (addButton != null && productNameLabel != null)
    {
      // Get the product name from the ProductNameLabel 
      // control.
      string name = productNameLabel.Text;

      // Use the FindByValue method to determine whether
      // the ListBox control already contains an entry for
      // the item.
      ListItem item = ProductListBox.Items.FindByValue(name);

      // Disable the Add button if the ListBox control
      // already contains the item.
      if (item != null)
      {
        addButton.Enabled = false;
      }
      else
      {
        addButton.Enabled = true;
      }
    }

  }
    
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>FormViewCommandEventArgs Example</title>
</head>
<body>
    <form id="form1" runat="server">
        
      <h3>FormViewCommandEventArgs Example</h3>
                       
      <asp:formview id="ProductFormView"
        datasourceid="ProductSource"
        allowpaging="true"
        datakeynames="ProductID"
        onitemcommand="ProductFormView_ItemCommand"
        ondatabound="ProductFormView_DataBound"  
        runat="server">
        
        <itemtemplate>
        
          <table>
            <tr>
              <td style="width:400px">
                <b>Description:</b>
                <asp:label id="ProductNameLabel"
                  text='<%# Eval("ProductName") %>'
                  runat='server'/>
                <br/>      
                <b>Price:</b>
                <asp:label id="PriceLabel"
                  text='<%# Eval("UnitPrice", "{0:c}") %>'
                  runat='server'/>
                <br/>  
                <asp:textbox id="QuantityTextBox"
                  width="50px"
                  maxlength="3" 
                  runat="server"/> Qty                   
              </td>
            </tr>
            <tr>
              <td>
                <asp:requiredfieldvalidator ID="QuantityRequiredValidator"
                  controltovalidate="QuantityTextBox"
                  text="Please enter a quantity."
                  display="Static"
                  runat="server"/>
                <br/>
                <asp:CompareValidator id="QuantityCompareValidator"
                  controltovalidate="QuantityTextBox"
                  text="Please enter an integer value."
                  display="Static"
                  type="Integer"
                  operator="DataTypeCheck"  
                  runat="server"/>    
              </td>
            </tr>
            <tr>
              <td colspan="2">
                <asp:button id="AddButton"
                  text="Add"
                  commandname="Add"
                  runat="server"/>
              </td>
            </tr>
          </table>
        
        </itemtemplate>
                  
      </asp:formview>
      
      <br/><br/><hr/>
      
      Items:<br/>
      <asp:listbox id="ProductListBox"
        runat="server"/>
          
      <!-- 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="ProductSource"
        selectcommand="Select [ProductID], [ProductName], [UnitPrice] From [Products]"
        connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>" 
        runat="server"/>
            
    </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 ProductFormView_ItemCommand(ByVal sender As Object, ByVal e As FormViewCommandEventArgs)

    ' The ItemCommand event is raised when any button within
    ' the FormView control is clicked. Use the CommandName property 
    ' to determine which button was clicked. 
    If e.CommandName = "Add" Then

      ' Add the product to the ListBox control. 
      
      ' Use the Row property to retrieve the data row.
      Dim row As FormViewRow = ProductFormView.Row
      
      ' Retrieve the ProductNameLabel control from
      ' the data row.
      Dim productNameLabel As Label = CType(row.FindControl("ProductNameLabel"), Label)

      ' Retrieve the QuantityTextBox control from
      ' the data row.
      Dim quantityTextBox As TextBox = CType(row.FindControl("QuantityTextBox"), TextBox)

      If productNameLabel IsNot Nothing And quantityTextBox IsNot Nothing Then
          
        ' Get the product name from the ProductNameLabel control.
        Dim name As String = productNameLabel.Text
        
        ' Get the quantity from the QuantityTextBox control.
        Dim quantity As String = quantityTextBox.Text

        ' Create the text to display in the ListBox control.
        Dim description As String = name & " - " & quantity & " Qty"

        ' Create a ListItem object using the description and
        ' product name.
        Dim item As new ListItem(description, name)

        ' Add the ListItem object to the ListBox.
        ProductListBox.Items.Add(item)

        ' Use the CommandSource property to retrieve
        ' the Add button. Disable the button after
        ' the user adds the currently displayed employee
        ' name to the ListBox control.
        Dim addButton As Button = CType(e.CommandSource, Button)
        addButton.Enabled = False
        
      End If

    End If

  End Sub

  Sub ProductFormView_DataBound(ByVal sender As Object, ByVal e As EventArgs)
    
    ' To prevent the user from adding duplicate items, 
    ' disable the Add button if the item being bound to the 
    ' FormView control is already in the ListBox control.
    
    ' Use the Row property to retrieve the data row.
    Dim row As FormViewRow = ProductFormView.Row

    ' Retrieve the Add button from the data row.
    Dim addButton As Button = CType(row.FindControl("AddButton"), Button)

    ' Retrieve the ProductNameLabel control from
    ' data row.
    Dim productNameLabel As Label = CType(row.FindControl("ProductNameLabel"), Label)

    If addButton IsNot Nothing And productNameLabel IsNot Nothing Then
    
      ' Get the product name from the ProductNameLabel 
      ' control.
      Dim name As String = productNameLabel.Text

      ' Use the FindByValue method to determine whether
      ' the ListBox control already contains an entry for
      ' the item.
      Dim item As ListItem = ProductListBox.Items.FindByValue(name)

      ' Disable the Add button if the ListBox control
      ' already contains the item.
      If item IsNot Nothing Then
      
        addButton.Enabled = False
      
      Else
      
        addButton.Enabled = True
        
      End If
      
    End If

  End Sub
    
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>FormViewCommandEventArgs Example</title>
</head>
<body>
    <form id="form1" runat="server">
        
      <h3>FormViewCommandEventArgs Example</h3>
                       
      <asp:formview id="ProductFormView"
        datasourceid="ProductSource"
        allowpaging="true"
        datakeynames="ProductID"
        onitemcommand="ProductFormView_ItemCommand"
        ondatabound="ProductFormView_DataBound"  
        runat="server">
        
        <itemtemplate>
        
          <table>
            <tr>
              <td style="width:400px">
                <b>Description:</b>
                <asp:label id="ProductNameLabel"
                  text='<%# Eval("ProductName") %>'
                  runat='server'/>
                <br/>      
                <b>Price:</b>
                <asp:label id="PriceLabel"
                  text='<%# Eval("UnitPrice", "{0:c}") %>'
                  runat='server'/>
                <br/>  
                <asp:textbox id="QuantityTextBox"
                  width="50px"
                  maxlength="3" 
                  runat="server"/> Qty                   
              </td>
            </tr>
            <tr>
              <td>
                <asp:requiredfieldvalidator ID="QuantityRequiredValidator"
                  controltovalidate="QuantityTextBox"
                  text="Please enter a quantity."
                  display="Static"
                  runat="server"/>
                <br/>
                <asp:CompareValidator id="QuantityCompareValidator"
                  controltovalidate="QuantityTextBox"
                  text="Please enter an integer value."
                  display="Static"
                  type="Integer"
                  operator="DataTypeCheck"  
                  runat="server"/>    
              </td>
            </tr>
            <tr>
              <td colspan="2">
                <asp:button id="AddButton"
                  text="Add"
                  commandname="Add"
                  runat="server"/>
              </td>
            </tr>
          </table>
        
        </itemtemplate>
                  
      </asp:formview>
      
      <br/><br/><hr/>
      
      Items:<br/>
      <asp:listbox id="ProductListBox"
        runat="server"/>
          
      <!-- 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="ProductSource"
        selectcommand="Select [ProductID], [ProductName], [UnitPrice] From [Products]"
        connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>" 
        runat="server"/>
            
    </form>
  </body>
</html>

설명

합니다 ItemCommand 이벤트는 단추를는 FormView 컨트롤을 클릭 합니다. 이 옵션을 사용 하면이 이벤트가 발생할 때마다 사용자 지정 루틴을 수행 하는 이벤트 처리 메서드를 제공할 수 있습니다.

내에서 단추를 FormView 컨트롤 컨트롤의 기본 기능 중 일부를 호출할 수도 있습니다. 이러한 작업 중 하나를 수행 하려면 설정의 CommandName 다음 표의 값 중 하나에 단추의 속성입니다.

CommandName 값 Description
"취소" 편집 또는 삽입 작업을 취소 하 고 반환 합니다 FormView 컨트롤을 지정 된 모드로 DefaultMode 속성입니다. 발생 합니다 ModeChangedModeChanging 이벤트입니다.
"Delete" 현재 레코드를 삭제합니다. 발생 합니다 ItemDeletedItemDeleting 이벤트입니다.
"Edit" 배치는 FormView 편집 모드에서 제어 합니다. 발생 합니다 ModeChangedModeChanging 이벤트입니다.
"Insert" 데이터 소스에 현재 레코드를 삽입합니다. 발생 합니다 ItemInsertedItemInserting 이벤트입니다.
"New" 배치는 FormView 삽입 모드에서 제어 합니다. 발생 합니다 ModeChangedModeChanging 이벤트입니다.
"Page" 페이징 작업을 수행합니다. 설정 된 CommandArgument , 속성을 단추 "First", "Last"를 "다음", "prev" 라는 페이징 작업의 형식을 지정 하기 위해 페이지를 수행 하 합니다. 발생 합니다 PageIndexChangedPageIndexChanging 이벤트입니다.
"업데이트" 데이터 소스의 현재 레코드를 업데이트합니다. 발생 합니다 ItemUpdatedItemUpdating 이벤트입니다.

하지만 ItemCommand 이전 표에 나열 된 단추가 클릭 될 때 이벤트가 발생, 작업에 대 한 테이블에 나열 된 이벤트를 사용 하는 것이 좋습니다.

FormViewCommandEventArgs 개체 명령 이름과 클릭 한 단추의 명령 인수를 확인할 수 있는 이벤트 처리 메서드에 전달 됩니다. 명령 이름과 명령 인수를 확인 하려면 사용 합니다 CommandNameCommandArgument 속성을 각각. 사용 하 여 이벤트를 발생 시킨 단추 컨트롤에 액세스할 수도 있습니다는 CommandSource 속성입니다.

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

FormViewCommandEventArgs 클래스의 인스턴스에 대한 초기 속성 값 목록은 FormViewCommandEventArgs 생성자를 참조하십시오.

생성자

FormViewCommandEventArgs(Object, CommandEventArgs)

FormViewCommandEventArgs 클래스의 새 인스턴스를 초기화합니다.

속성

CommandArgument

명령에 대한 인수를 가져옵니다.

(다음에서 상속됨 CommandEventArgs)
CommandName

명령의 이름을 가져옵니다.

(다음에서 상속됨 CommandEventArgs)
CommandSource

이벤트를 발생시킨 컨트롤을 가져옵니다.

Handled

제어가 이벤트를 처리했는지 여부를 나타내는 값을 가져오거나 설정합니다.

메서드

Equals(Object)

지정된 개체가 현재 개체와 같은지 확인합니다.

(다음에서 상속됨 Object)
GetHashCode()

기본 해시 함수로 작동합니다.

(다음에서 상속됨 Object)
GetType()

현재 인스턴스의 Type을 가져옵니다.

(다음에서 상속됨 Object)
MemberwiseClone()

현재 Object의 단순 복사본을 만듭니다.

(다음에서 상속됨 Object)
ToString()

현재 개체를 나타내는 문자열을 반환합니다.

(다음에서 상속됨 Object)

적용 대상

추가 정보