在DataGrid里添加确认删除的对话框
http://lucky_elove.www1.dotnetplayground.com/
在删除数据前进行确认是一个非常友好的做法。在本文里,我们就讨论利用TemplateColumn和Button服务端控件实现这种功能的方法。
查看例子
DeleteIt.aspx
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="DeleteIt.aspx.vb" Inherits="aspxWeb.DeleteIt"%>
WebForm1
后代码:DeleteIt.aspx.vb
Imports System.Web
Imports System.Collections
Imports System
Imports System.Data
Imports System.Data.OleDb
Imports System.Web.UI.WebControls
Public Class DeleteIt
Inherits System.Web.UI.Page
Protected WithEvents Label1 As System.Web.UI.WebControls.Label
Protected WithEvents myDataGrid As System.Web.UI.WebControls.DataGrid
#Region " Web 窗体设计器生成的代码 "
'该调用是 Web 窗体设计器所必需的。
Private Sub InitializeComponent()
End Sub
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: 此方法调用是 Web 窗体设计器所必需的
'不要使用代码编辑器修改它。
InitializeComponent()
End Sub
#End Region
Sub Page_Load(ByVal Sender As Object, ByVal e As EventArgs) Handles MyBase.Load
myDataGrid.HeaderStyle.Font.Bold = True
myDataGrid.HeaderStyle.HorizontalAlign = HorizontalAlign.Center
myDataGrid.Columns(0).HeaderText = "操作"
myDataGrid.Columns(1).HeaderText = "标题"
myDataGrid.Columns(2).HeaderText = "发布日期"
If Not IsPostBack Then
Dim strCn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("Test.mdb")
Dim strSql As String
strSql = "SELECT Top 15 objectGuid,Title,CreateDate FROM Document Order By CreateDate Desc"
Dim cn As New OleDbConnection(strCn)
cn.Open()
Dim cmd As New OleDbCommand(strSql, cn)
myDataGrid.DataSource = cmd.ExecuteReader(CommandBehavior.CloseConnection)
myDataGrid.DataBind()
cmd.Dispose()
cmd = Nothing
cn.Close()
cn.Dispose()
cn = Nothing
End If
End Sub
Sub DataGrid_ItemCreated(ByVal Sender As Object, ByVal e As DataGridItemEventArgs)_
Handles myDataGrid.ItemCreated
Select Case e.Item.ItemType
Case ListItemType.Item, ListItemType.AlternatingItem, ListItemType.EditItem
Dim myDeleteButton As Button
myDeleteButton = e.Item.FindControl("btnDelete")
myDeleteButton.Text = "删除此行"
myDeleteButton.Attributes.Add("onclick", "return confirm('您真的要删除第 "_
+ e.Item.ItemIndex.ToString + " 行吗?');")
End Select
End Sub
Private Sub myDataGrid_ItemCommand(ByVal source As Object, _
ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs)_
Handles myDataGrid.ItemCommand
Response.Write("您要删除的是:" + e.Item.Cells(1).Text + "")
e.Item.BackColor = System.Drawing.Color.Ivory
End Sub
End Class
本文地址:http://com.8s8s.com/it/it45606.htm