GridView加上刪除提示訊息

摘要:GridView加上刪除提示訊息

一般GridView的刪除鈕可以在Columns加上ButtonFieldCommandField欄位來完成,但是這二種方式的刪除鈕都沒有屬性可以加入刪除的提示訊息,以下的範例將說明如何在GridView加上刪除提示訊息。

GridViewColumns中加入一個ButtonField然後在GridViewRowDataBound事件中,找到按鈕並加入刪除的訊息。

 

*.aspx 程式碼

01         <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="EmployeeID"
02             DataSourceID="SqlDataSource1" EmptyDataText="沒有資料錄可顯示。" AllowSorting="True">
03             <Columns>
04                 <asp:ButtonField ButtonType="Button" CommandName="Delete" Text="刪除" />
05                 <asp:BoundField DataField="EmployeeID" HeaderText="EmployeeID" ReadOnly="True" SortExpression="EmployeeID" />
06                 <asp:BoundField DataField="LastName" HeaderText="LastName" SortExpression="LastName" />
07                 <asp:BoundField DataField="FirstName" HeaderText="FirstName" SortExpression="FirstName" />
08                 <asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
09                 <asp:BoundField DataField="TitleOfCourtesy" HeaderText="TitleOfCourtesy" SortExpression="TitleOfCourtesy" />
10                 <asp:BoundField DataField="BirthDate" HeaderText="BirthDate" SortExpression="BirthDate" />
11                 <asp:CommandField ShowEditButton="True" />
12             </Columns>
13         </asp:GridView>

 

*.aspx.vb 程式碼

01     Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
02         Dim oDataRow As Data.DataRowView
03         Dim sShowField As String
04         Dim sMessage As String
05         If e.Row.RowIndex <> -1 Then
06             oDataRow = CType(e.Row.DataItem, Data.DataRowView)
07             sShowField = oDataRow.Item("LastName").ToString()
08             sMessage = String.Format("您確定要刪除 [{0}] 嗎?", sShowField)
09             CType(e.Row.Cells(0).Controls(0), Button).OnClientClick = "if (confirm('" & sMessage & "')==false) {return false;}"
10         End If
11     End Sub

ASP.NET 魔法學院