FormView.ItemCommand 이벤트
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
컨트롤 내의 단추를 클릭할 FormView 때 발생합니다.
public:
event System::Web::UI::WebControls::FormViewCommandEventHandler ^ ItemCommand;
public event System.Web.UI.WebControls.FormViewCommandEventHandler ItemCommand;
member this.ItemCommand : System.Web.UI.WebControls.FormViewCommandEventHandler
Public Custom Event ItemCommand As FormViewCommandEventHandler
이벤트 유형
예제
다음 예제에서는 컨트롤에서 ItemCommand 단추 클릭을 처리 하는 이벤트를 사용 하는 방법을 보여 줍니다 FormView . 사용자가 추가 단추를 클릭하면 현재 표시된 항목이 컨트롤에 ListBox 추가됩니다.
<%@ 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 값 | 설명 |
|---|---|
| "취소" | 편집 또는 삽입 작업을 취소하고 컨트롤을 FormView 속성에 지정된 모드로 DefaultMode 반환합니다. 및 ModeChanging 이벤트를 발생합니다ModeChanged. |
| "삭제" | 현재 레코드를 삭제합니다. 및 ItemDeleting 이벤트를 발생합니다ItemDeleted. |
| "편집" | 컨트롤을 FormView 편집 모드로 전환합니다. 및 ModeChanging 이벤트를 발생합니다ModeChanged. |
| "삽입" | 데이터 원본에 현재 레코드를 삽입합니다. 및 ItemInserting 이벤트를 발생합니다ItemInserted. |
| "새로 만들기" | 컨트롤을 FormView 삽입 모드로 전환합니다. 및 ModeChanging 이벤트를 발생합니다ModeChanged. |
| "페이지" | 페이징 작업을 수행합니다.
CommandArgument 단추의 속성을 "First", "Last", "Next", "Prev" 또는 페이지 번호로 설정하여 수행할 페이징 작업의 유형을 지정합니다. 및 PageIndexChanging 이벤트를 발생합니다PageIndexChanged. |
| "업데이트" | 데이터 원본의 현재 레코드를 업데이트합니다. 및 ItemUpdating 이벤트를 발생합니다ItemUpdated. |
이전 표에 ItemCommand 나열된 단추를 클릭할 때 이벤트가 발생하지만 테이블에 나열된 이벤트를 작업에 사용하는 것이 좋습니다.
FormViewCommandEventArgs 개체가 이벤트 처리 메서드에 전달되므로 클릭한 단추의 명령 이름과 명령 인수를 확인할 수 있습니다. 명령 이름 및 명령 인수를 확인하려면 각각 및 CommandArgument 속성을 사용합니다CommandName. 속성을 사용하여 이벤트를 발생시킨 컨트롤에 액세스할 수도 있습니다 CommandSource .
이벤트를 처리하는 방법에 대한 자세한 내용은 이벤트 처리 및 발생을 참조하세요.