在使用DataGrid开发程序时经常遇到需要用户确认的操作,最常见的为更改和删除操作,问题是 ASP.NET并没有提供直接的方法,所以一般都使用Javascript来实现.实现方法如下在DataGrid的ItemGreated事件中添加如下代码,下面代码首先找到触发事件的控件,我用的是按钮列,如果是用的
模板列的话可以直接用findcontrol方法来做.
Private
Sub dgMdl2_ItemCreated()
Sub dgMdl2_ItemCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgMdl2.ItemCreated
Dim lb As LinkButton
Dim con As Control
Dim tc As TableCell
'这里我的删除按钮是一个LinkButton,ASP.NET把该按钮的回传事件放在了href属性,所以可以用空出的onclick来做
For Each con In e.Item.Cells(e.Item.Cells.Count - 1).Controls '因为我的删除按钮放在了最后一列
If con.GetType.ToString() = "System.Web.UI.WebControls.DataGridLinkButton" Then
lb = CType(con, LinkButton)
If lb.CommandName = "Delete" Then
lb.Attributes.Add("onclick", "return confirm('您确认要删除吗?')")
End If
End If
Next
End Sub
Dim lb As LinkButton
Dim con As Control
Dim tc As TableCell
'这里我的删除按钮是一个LinkButton,ASP.NET把该按钮的回传事件放在了href属性,所以可以用空出的onclick来做
For Each con In e.Item.Cells(e.Item.Cells.Count - 1).Controls '因为我的删除按钮放在了最后一列
If con.GetType.ToString() = "System.Web.UI.WebControls.DataGridLinkButton" Then
lb = CType(con, LinkButton)
If lb.CommandName = "Delete" Then
lb.Attributes.Add("onclick", "return confirm('您确认要删除吗?')")
End If
End If
Next
End Sub