一个删除记录的操作,代码如下:
Protected Sub GridView1_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)
If e.Row.RowState = DataControlRowState.Normal Or e.Row.RowState = DataControlRowState.Alternate Then
If e.Row.Cells(5).Controls.Count <> 0 Then
Dim btndelete As LinkButton = CType(e.Row.Cells(5).Controls(0), LinkButton)
If btndelete IsNot Nothing Then
btndelete.Attributes("onclick") = "return confirm('您确定要删除该记录吗?')"
End If
End If
End If
End Sub
Protected Sub GridView1_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)
If e.Row.RowState = DataControlRowState.Normal Or e.Row.RowState = DataControlRowState.Alternate Then
If e.Row.Cells(5).Controls.Count <> 0 Then
Dim btndelete As LinkButton = CType(e.Row.Cells(5).Controls(0), LinkButton)
If btndelete IsNot Nothing Then
btndelete.Attributes("onclick") = "return confirm('您确定要删除该记录吗?')"
End If
End If
End If
End Sub
1、在rowdatabound事件处理比较好
2、最好用e.row.findcontrol("控件名")的方式以下代码可用
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
Dim lb As LinkButton
lb = e.Row.FindControl("LinkButton1")
If DataBinder.Eval(e.Row.DataItem, "Status") < 5 Then
lb.Attributes.Add("onclick", "javascript:return confirm('您准备删除这条记录吗?')")
Else
lb.Attributes.Add("onclick", "javascript:return alert('不能删除此记录!')")
End If
End If
End Sub