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 对象。
示例
下面的示例演示如何以编程方式向 控件的 GridView 事件添加GridViewDeleteEventHandler委托RowDeleting。
<%@ 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>
以下示例演示如何以声明方式向 控件的 GridView 事件添加GridViewDeleteEventHandler委托RowDeleting。
<%@ 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
时,控件将引发 RowDeleting 事件) ,但在控件删除记录之前GridView。 这允许你提供一个事件处理方法,该方法执行自定义例程,例如,每当发生此事件时取消删除操作。
创建 GridViewDeleteEventHandler 委托时,需要标识将处理该事件的方法。 若要将事件与事件处理程序关联,请将该委托的一个实例添加到事件中。 除非移除了该委托,否则每当发生该事件时就会调用事件处理程序。 有关事件处理程序委托的详细信息,请参阅 处理和引发事件。
扩展方法
GetMethodInfo(Delegate) |
获取指示指定委托表示的方法的对象。 |