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>
備註
按一下控制項內的 FormView 按鈕時,就會 ItemCommand 引發 事件。 這可讓您提供事件處理方法,每當發生此事件時,都會執行自訂常式。
控制項內的 FormView 按鈕也可以叫用控制項的一些內建功能。 若要執行其中一項作業,請將 CommandName
按鈕的 屬性設定為下表中的其中一個值。
CommandName 值 | 描述 |
---|---|
「取消」 | 取消編輯或插入作業,並將控制項傳回 FormView 屬性所 DefaultMode 指定的模式。 ModeChanged引發 和 ModeChanging 事件。 |
"Delete" | 刪除目前記錄。 ItemDeleted引發 和 ItemDeleting 事件。 |
「編輯」 | 將 FormView 控制項置於編輯模式。 ModeChanged引發 和 ModeChanging 事件。 |
「Insert」 | 將目前的資料錄插入資料來源。 ItemInserted引發 和 ItemInserting 事件。 |
「新增」 | 將 FormView 控制項置於插入模式中。 ModeChanged引發 和 ModeChanging 事件。 |
「Page」 | 執行分頁作業。 將 CommandArgument 按鈕的 屬性設定為 「First」、「Last」、「Next」、「Prev」 或頁碼,以指定要執行的分頁作業類型。
PageIndexChanged引發 和 PageIndexChanging 事件。 |
「Update」 | 更新資料來源中目前的資料錄。 ItemUpdated引發 和 ItemUpdating 事件。 |
ItemCommand雖然按一下上表所列的按鈕時會引發事件,但建議您使用資料表中所列的事件來進行作業。
FormViewCommandEventArgs物件會傳遞至事件處理方法,可讓您判斷按下按鈕的命令名稱和命令引數。 若要判斷命令名稱和命令引數,請分別使用 CommandName 和 CommandArgument 屬性。 您也可以使用 CommandSource 屬性來存取引發事件的控制項。
如需如何處理事件的詳細資訊,請參閱 處理和引發事件。