GridViewDeleteEventHandler 代理人
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
表示處理 RowDeleting 控制項之 GridView 事件的方法。
public delegate void GridViewDeleteEventHandler(System::Object ^ sender, GridViewDeleteEventArgs ^ e);
public delegate void GridViewDeleteEventHandler(object sender, GridViewDeleteEventArgs e);
type GridViewDeleteEventHandler = delegate of obj * GridViewDeleteEventArgs -> unit
Public Delegate Sub GridViewDeleteEventHandler(sender As Object, e As GridViewDeleteEventArgs)
參數
- sender
- Object
事件的來源。
包含事件資料的 GridViewDeleteEventArgs 物件。
範例
下列範例示範如何以程式設計方式將委派新增 GridViewDeleteEventHandler 至 RowDeleting 控制項的事件 GridView 。
<%@ 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 Page_Load(Object sender, EventArgs e)
{
// Create a new GridView control.
GridView customersGridView = new GridView();
// Set the GridView control's properties.
customersGridView.ID = "CustomersGridView";
customersGridView.DataSourceID = "CustomersSqlDataSource";
customersGridView.AutoGenerateColumns = true;
customersGridView.AutoGenerateDeleteButton = true;
customersGridView.DataKeyNames = new String[] {"CustomerID"};
// Programmatically register the event-handling methods.
customersGridView.RowDeleting += new GridViewDeleteEventHandler(this.CustomersGridView_RowDeleting);
// Add the GridView object to the Controls collection
// of the PlaceHolder control.
GridViewPlaceHolder.Controls.Add(customersGridView);
}
void CustomersGridView_RowDeleting(Object sender, GridViewDeleteEventArgs e)
{
// User the sender parameter to retrieve the GridView control
// that raised the event.
GridView customersGridView = (GridView)sender;
// Cancel the delete operation if the user attempts to remove
// the last record from the GridView control.
if (customersGridView.Rows.Count <= 1)
{
e.Cancel = true;
Message.Text = "You must keep at least one record.";
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>GridViewDeleteEventHandler Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>GridViewDeleteEventHandler Example</h3>
<asp:label id="Message"
forecolor="Red"
runat="server"/>
<br/>
<asp:placeholder id="GridViewPlaceHolder"
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="CustomersSqlDataSource"
selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]"
deletecommand="Delete from Customers where CustomerID = @CustomerID"
connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>"
runat="server">
</asp:sqldatasource>
</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 Page_Load(ByVal sender As Object, ByVal e As EventArgs)
' Create a new GridView control.
Dim customersGridView As New GridView()
' Set the GridView control's properties.
customersGridView.ID = "CustomersGridView"
customersGridView.DataSourceID = "CustomersSqlDataSource"
customersGridView.AutoGenerateColumns = True
customersGridView.AutoGenerateDeleteButton = True
Dim keyArray() As String = {"CustomerID"}
customersGridView.DataKeyNames = keyArray
' Programmatically register the event-handling methods.
AddHandler customersGridView.RowDeleting, AddressOf CustomersGridView_RowDeleting
' Add the GridView object to the Controls collection
' of the PlaceHolder control.
GridViewPlaceHolder.Controls.Add(customersGridView)
End Sub
Sub CustomersGridView_RowDeleting(ByVal sender As Object, ByVal e As GridViewDeleteEventArgs)
' User the sender parameter to retrieve the GridView control
' that raised the event.
Dim customersGridView As GridView = CType(sender, GridView)
' Cancel the delete operation if the user attempts to remove
' the last record from the GridView control.
If customersGridView.Rows.Count <= 1 Then
e.Cancel = True
Message.Text = "You must keep at least one record."
End If
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>GridViewDeleteEventHandler Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>GridViewDeleteEventHandler Example</h3>
<asp:label id="Message"
forecolor="Red"
runat="server"/>
<br/>
<asp:placeholder id="GridViewPlaceHolder"
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="CustomersSqlDataSource"
selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]"
deletecommand="Delete from Customers where CustomerID = @CustomerID"
connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>"
runat="server">
</asp:sqldatasource>
</form>
</body>
</html>
下列範例示範如何以宣告方式將委派新增 GridViewDeleteEventHandler 至 RowDeleting 控制項的事件 GridView 。
<%@ 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 CustomersGridView_RowDeleting
(Object sender, GridViewDeleteEventArgs e)
{
TableCell cell = CustomersGridView.Rows[e.RowIndex].Cells[2];
if (cell.Text == "Beaver")
{
e.Cancel = true;
Message.Text = "You cannot delete customer Beaver.";
}
else
{
Message.Text = "";
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>GridView RowDeleting Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>
GridView RowDeleting Example
</h3>
<asp:Label ID="Message" ForeColor="Red" runat="server" />
<br />
<asp:GridView ID="CustomersGridView" runat="server"
DataSourceID="CustomersSqlDataSource"
AutoGenerateColumns="False"
AutoGenerateDeleteButton="True"
OnRowDeleting="CustomersGridView_RowDeleting"
DataKeyNames="CustomerID,AddressID">
<Columns>
<asp:BoundField DataField="FirstName"
HeaderText="FirstName" SortExpression="FirstName" />
<asp:BoundField DataField="LastName" HeaderText="LastName"
SortExpression="LastName" />
<asp:BoundField DataField="City" HeaderText="City"
SortExpression="City" />
<asp:BoundField DataField="StateProvince" HeaderText="State"
SortExpression="StateProvince" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="CustomersSqlDataSource" runat="server"
SelectCommand="SELECT SalesLT.CustomerAddress.CustomerID,
SalesLT.CustomerAddress.AddressID,
SalesLT.Customer.FirstName,
SalesLT.Customer.LastName,
SalesLT.Address.City,
SalesLT.Address.StateProvince
FROM SalesLT.Customer
INNER JOIN SalesLT.CustomerAddress
ON SalesLT.Customer.CustomerID =
SalesLT.CustomerAddress.CustomerID
INNER JOIN SalesLT.Address ON SalesLT.CustomerAddress.AddressID =
SalesLT.Address.AddressID"
DeleteCommand="Delete from SalesLT.CustomerAddress where CustomerID =
@CustomerID and AddressID = @AddressID"
ConnectionString="<%$ ConnectionStrings:AdventureWorksLTConnectionString %>">
<DeleteParameters>
<asp:Parameter Name="AddressID" />
<asp:Parameter Name="CustomerID" />
</DeleteParameters>
</asp:SqlDataSource>
</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">
Private Sub CustomersGridView_RowDeleting _
(ByVal sender As [Object], _
ByVal e As GridViewDeleteEventArgs)
Dim cell As TableCell
cell = CustomersGridView.Rows(e.RowIndex).Cells(2)
If cell.Text = "Beaver" Then
e.Cancel = True
Message.Text = "You cannot delete customer Beaver."
Else
Message.Text = ""
End If
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>GridView RowDeleting Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>
GridView RowDeleting Example
</h3>
<asp:Label ID="Message" ForeColor="Red" runat="server" />
<br />
<asp:GridView ID="CustomersGridView" runat="server"
DataSourceID="CustomersSqlDataSource"
AutoGenerateColumns="False"
AutoGenerateDeleteButton="True"
OnRowDeleting="CustomersGridView_RowDeleting"
DataKeyNames="CustomerID,AddressID">
<Columns>
<asp:BoundField DataField="FirstName"
HeaderText="FirstName" SortExpression="FirstName" />
<asp:BoundField DataField="LastName" HeaderText="LastName"
SortExpression="LastName" />
<asp:BoundField DataField="City" HeaderText="City"
SortExpression="City" />
<asp:BoundField DataField="StateProvince" HeaderText="State"
SortExpression="StateProvince" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="CustomersSqlDataSource" runat="server"
SelectCommand="SELECT SalesLT.CustomerAddress.CustomerID,
SalesLT.CustomerAddress.AddressID,
SalesLT.Customer.FirstName,
SalesLT.Customer.LastName,
SalesLT.Address.City,
SalesLT.Address.StateProvince
FROM SalesLT.Customer
INNER JOIN SalesLT.CustomerAddress
ON SalesLT.Customer.CustomerID =
SalesLT.CustomerAddress.CustomerID
INNER JOIN SalesLT.Address ON SalesLT.CustomerAddress.AddressID =
SalesLT.Address.AddressID"
DeleteCommand="Delete from SalesLT.CustomerAddress where CustomerID =
@CustomerID and AddressID = @AddressID"
ConnectionString="<%$ ConnectionStrings:AdventureWorksLTConnectionString %>">
<DeleteParameters>
<asp:Parameter Name="AddressID" />
<asp:Parameter Name="CustomerID" />
</DeleteParameters>
</asp:SqlDataSource>
</form>
</body>
</html>
備註
GridView當按一下 [刪除] 按鈕 (CommandName
按鈕的屬性設為 「Delete」) 時,控制項就會引發 RowDeleting 事件,但在控制項刪除記錄之前 GridView 。 這可讓您提供執行自訂常式的事件處理方法,例如每當發生此事件時取消刪除作業。
建立 GridViewDeleteEventHandler 委派時,必須識別處理事件的方法。 若要使事件與您的事件處理常式產生關聯,請將委派的執行個體 (Instance) 加入至事件。 除非您移除委派,否則每當事件發生時就會呼叫事件處理常式。 如需事件處理常式委派的詳細資訊,請參閱 處理和引發事件。
擴充方法
GetMethodInfo(Delegate) |
取得表示特定委派所代表之方法的物件。 |