请先看下面这篇文章:
今天我试了上面文章讲的方法,感觉实现起来好象也不是特别好,这样说可能要被人
死.看了同学买的.net书,里面介绍了一个RowDataBound事件,在MSDN中我也摘录了这个事件的使用方法,具体请看下面这篇文章:
关键的代码:
void
GridView1_RowDataBound(
object
sender, GridViewRowEventArgs e)
...
{
if (e.Row.RowType == DataControlRowType.DataRow)
...{
((LinkButton)(e.Row.Cells[2].Controls[0])).Attributes.Add("onclick", "return confirm(‘确定要删除吗?‘)");
}
}
里面需要用到查找控件添加属性的方法,可以查看其它文章,完整的实现代码如下;
<%
@ Page Language
=
"
C#
"
AutoEventWireup
=
"
true
"
%>

<!
DOCTYPE html PUBLIC
"
-//W3C//DTD XHTML 1.0 Transitional//EN
"
"
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd
"
>

<
html xmlns
=
"
http://www.w3.org/1999/xhtml
"
>
<
head runat
=
"
server
"
>
<
title
>
无标题页
</
title
>
</
head
>
<
body
>
<
form id
=
"
form1
"
runat
=
"
server
"
>
<
asp:GridView ID
=
"
GridView1
"
runat
=
"
server
"
AutoGenerateColumns
=
"
False
"
DataKeyNames
=
"
id
"
DataSourceID
=
"
AccessDataSource1
"
OnRowDataBound
=
"
GridView1_RowDataBound
"
>
<
Columns
>
<
asp:BoundField DataField
=
"
id
"
HeaderText
=
"
id
"
InsertVisible
=
"
False
"
ReadOnly
=
"
True
"
SortExpression
=
"
id
"
/>
<
asp:BoundField DataField
=
"
name
"
HeaderText
=
"
名字
"
SortExpression
=
"
name
"
/>
<
asp:CommandField ShowDeleteButton
=
"
True
"
/>
</
Columns
>
</
asp:GridView
>
<
asp:AccessDataSource ID
=
"
AccessDataSource1
"
runat
=
"
server
"
DataFile
=
"
~/App_Data/admin.mdb
"
SelectCommand
=
"
SELECT * FROM [adimn]
"
DeleteCommand
=
"
Delete from adimn where id = ?
"
></
asp:AccessDataSource
>
</
form
>
</
body
>
</
html
>
<
script runat
=
"
server
"
>
void
GridView1_RowDataBound(
object
sender, GridViewRowEventArgs e)
...
{
if (e.Row.RowType == DataControlRowType.DataRow)
...{
((LinkButton)(e.Row.Cells[2].Controls[0])).Attributes.Add("onclick", "return confirm(‘确定要删除吗?‘)");
}
}
</
script
>
本文介绍如何在ASP.NET的GridView控件中实现删除操作前的确认对话框功能。通过利用RowDataBound事件,可以在点击删除按钮时弹出确认提示,确保用户明确其操作。代码示例展示了具体的实现细节。

1148

被折叠的 条评论
为什么被折叠?



