ListView.ItemDeleting 事件
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
當要求刪除作業時,但是在 ListView 控制項刪除項目之前發生。
public:
event EventHandler<System::Web::UI::WebControls::ListViewDeleteEventArgs ^> ^ ItemDeleting;
public event EventHandler<System.Web.UI.WebControls.ListViewDeleteEventArgs> ItemDeleting;
member this.ItemDeleting : EventHandler<System.Web.UI.WebControls.ListViewDeleteEventArgs>
Public Custom Event ItemDeleting As EventHandler(Of ListViewDeleteEventArgs)
事件類型
範例
下列範例示範如何宣告方式新增事件的事件處理程式 ItemDeleting 。
<%@ 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">
protected void CategoriesListView_OnItemDeleting(object sender, ListViewDeleteEventArgs e)
{
if (SubCategoriesGridView.Rows.Count > 0)
{
MessageLabel.Text = "You cannot delete a category that has sub-categories.";
e.Cancel = true;
}
}
protected void Page_Load()
{
MessageLabel.Text = String.Empty;
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Subcategories List</title>
</head>
<body>
<form id="form1" runat="server">
<b>Categories</b>
<br />
<asp:Label ForeColor="Red" runat="server" ID="MessageLabel" /><br />
<asp:ListView runat="server"
ID="CategoriesListView"
OnItemDeleting="CategoriesListView_OnItemDeleting"
DataSourceID="CategoriesDataSource"
DataKeyNames="ProductCategoryID">
<LayoutTemplate>
<table runat="server" id="tblCategories"
cellspacing="0" cellpadding="1" width="440px" border="1">
<tr id="itemPlaceholder" runat="server"></tr>
</table>
</LayoutTemplate>
<ItemTemplate>
<tr runat="server">
<td>
<asp:Label runat="server" ID="NameLabel" Text='<%#Eval("Name") %>' />
</td>
<td style="width:40px">
<asp:LinkButton runat="server" ID="SelectCategoryButton"
Text="Select" CommandName="Select" />
</td>
</tr>
</ItemTemplate>
<SelectedItemTemplate>
<tr runat="server" style="background-color:#90EE90">
<td>
<asp:Label runat="server" ID="NameLabel" Text='<%#Eval("Name") %>' />
</td>
<td style="width:40px">
<asp:LinkButton runat="server" ID="SelectCategoryButton"
Text="Delete" CommandName="Delete" />
</td>
</tr>
</SelectedItemTemplate>
</asp:ListView>
<br />
<b>Subcategories</b>
<asp:GridView runat="server" ID="SubCategoriesGridView" Width="300px"
DataSourceID="SubCategoriesDataSource" DataKeyNames="ProductSubcategoryID"
AutoGenerateColumns="True" />
<!-- This example uses Microsoft SQL Server and connects -->
<!-- to the AdventureWorks sample database. Use an ASP.NET -->
<!-- expression to retrieve the connection string value -->
<!-- from the Web.config file. -->
<asp:SqlDataSource ID="CategoriesDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:AdventureWorks_DataConnectionString %>"
SelectCommand="SELECT [ProductCategoryID], [Name]
FROM Production.ProductCategory">
</asp:SqlDataSource>
<asp:SqlDataSource ID="SubCategoriesDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:AdventureWorks_DataConnectionString %>"
SelectCommand="SELECT [ProductSubcategoryID], [Name]
FROM Production.ProductSubcategory
WHERE ProductCategoryID = @ProductCategoryID
ORDER BY [Name]">
<SelectParameters>
<asp:ControlParameter Name="ProductCategoryID" DefaultValue="0"
ControlID="CategoriesListView" PropertyName="SelectedValue" />
</SelectParameters>
</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">
Protected Sub CategoriesListView_OnItemDeleting(sender As Object, e As ListViewDeleteEventArgs)
If SubCategoriesGridView.Rows.Count > 0 Then
MessageLabel.Text = "You cannot delete a category that has sub-categories."
e.Cancel = True
End If
End Sub
Protected Sub Page_Load()
MessageLabel.Text = String.Empty
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Subcategories List</title>
</head>
<body>
<form id="form1" runat="server">
<b>Categories</b>
<br />
<asp:Label ForeColor="Red" runat="server" ID="MessageLabel" /><br />
<asp:ListView runat="server"
ID="CategoriesListView"
OnItemDeleting="CategoriesListView_OnItemDeleting"
DataSourceID="CategoriesDataSource"
DataKeyNames="ProductCategoryID">
<LayoutTemplate>
<table runat="server" id="tblCategories"
cellspacing="0" cellpadding="1" width="440px" border="1">
<tr id="itemPlaceholder" runat="server"></tr>
</table>
</LayoutTemplate>
<ItemTemplate>
<tr runat="server">
<td>
<asp:Label runat="server" ID="NameLabel" Text='<%#Eval("Name") %>' />
</td>
<td style="width:40px">
<asp:LinkButton runat="server" ID="SelectCategoryButton"
Text="Select" CommandName="Select" />
</td>
</tr>
</ItemTemplate>
<SelectedItemTemplate>
<tr runat="server" style="background-color:#90EE90">
<td>
<asp:Label runat="server" ID="NameLabel" Text='<%#Eval("Name") %>' />
</td>
<td style="width:40px">
<asp:LinkButton runat="server" ID="SelectCategoryButton"
Text="Delete" CommandName="Delete" />
</td>
</tr>
</SelectedItemTemplate>
</asp:ListView>
<br />
<b>Subcategories</b>
<asp:GridView runat="server" ID="SubCategoriesGridView" Width="300px"
DataSourceID="SubCategoriesDataSource" DataKeyNames="ProductSubcategoryID"
AutoGenerateColumns="True" />
<!-- This example uses Microsoft SQL Server and connects -->
<!-- to the AdventureWorks sample database. Use an ASP.NET -->
<!-- expression to retrieve the connection string value -->
<!-- from the Web.config file. -->
<asp:SqlDataSource ID="CategoriesDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:AdventureWorks_DataConnectionString %>"
SelectCommand="SELECT [ProductCategoryID], [Name]
FROM Production.ProductCategory">
</asp:SqlDataSource>
<asp:SqlDataSource ID="SubCategoriesDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:AdventureWorks_DataConnectionString %>"
SelectCommand="SELECT [ProductSubcategoryID], [Name]
FROM Production.ProductSubcategory
WHERE ProductCategoryID = @ProductCategoryID
ORDER BY [Name]">
<SelectParameters>
<asp:ControlParameter Name="ProductCategoryID" DefaultValue="0"
ControlID="CategoriesListView" PropertyName="SelectedValue" />
</SelectParameters>
</asp:SqlDataSource>
</form>
</body>
</html>
備註
ItemDeleting按兩下專案的 [刪除] 按鈕或DeleteItem呼叫 方法,但在控件刪除專案之前ListView,就會引發事件。 (Delete 按鈕是一個按鈕控制件,其 CommandName 屬性設定為 “Delete”。) 這可讓您在發生此事件時執行自定義例程,例如取消刪除作業。
ListViewDeleteEventArgs物件會傳遞至事件處理程式,可讓您判斷目前專案的索引。 它也可讓您指出應該取消刪除作業。 若要取消刪除工作,請將 Cancel 物件的屬性 ListViewDeleteEventArgs 設定為 true
。 您也可以在值傳遞至數據源之前,先使用 Keys 和 Values 集合。
如需如何處理事件的詳細資訊,請參閱 處理和引發事件。