Protected Sub GvOperInfo_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GvOperInfo.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            e.Row.Cells(9).Attributes.Add("onclick", "return confirm('确认修改?')")
        End If
    End Sub上面是我写的GridView的删除确认代码。可是最终效果是,无论你点击的是“确定”还是“取消”,都会删除记录。
问题出在哪里?

解决方案 »

  1.   

    因為你的Cells[9]並不是刪除按包鈕,而衹是按包含按钮的单元格。
    如果你是用的CommandField,
    把你的这段代码删除,直接把页面里CommandField換成:
        <asp:CommandField ShowDeleteButton="True" DeleteText="&lt;div style=&quot;display:inline&quot; id=&quot;deleteme&quot; onclick=&quot;JavaScript:return confirm('确定删除吗?')&quot;&gt;删除&lt;/div&gt;">
                     </asp:CommandField>
    如果是用的TemplateField,
    把你的你代码改成e.Row.Cells(9).FindControl("删除按钮ID").Attributes.Add("onclick", "return confirm('确认修改?')")
      

  2.   

    你的代码也有点问题,FindControl后面没有Attributes属性。不过,你还是给我找到了问题所在。
    我把代码修改成下面这样就OK了。
        Protected Sub GridView_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView.RowDataBound
            If e.Row.RowType = DataControlRowType.DataRow Then
                Dim BtTemp As New Button
                BtTemp = e.Row.Cells(5).FindControl("BtDelete")
                BtTemp.OnClientClick = "return confirm('你确定要删除吗?');"
            End If
        End Sub