删除CommandField中的确认消息?
我正在尝试获取确认消息, 同时单击 GridView中的 删除按钮。 如果我符合,那么将在GridView中删除该行。
* .ASPX
* .ASPX.CS
protected void grdPersTable_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { Button buttonCommandField = e.Row.Cells[0].Controls[0] as Button; buttonCommandField.Attributes["onClick"] = string.Format("return confirm('Are you want delete ')"); } } protected void grdPersTable_RowDeleting(object sender, GridViewDeleteEventArgs e) { Label lbl0 = (Label)grdPersTable.Rows[e.RowIndex].FindControl("lblId"); txtId.Text = lbl0.Text; obj.DeleteV(Convert.ToInt32(txtId.Text)); grdPersTable.DataSource = obj.GetTableValues(); grdPersTable.DataBind(); lblMessage.Text = "Deleted successfully !"; }
我得到了答案的朋友
我将CommandField更改为TemplateField
谢谢 !
如下所示更改rowdatabound事件。
protected void gv_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { ((Button)e.Row.Cells[0].Controls[0]).OnClientClick = "return confirm('Are you sure you want to delete?');"; } }
只需在onclientclick事件上调用javascript函数并请求确认。 如果返回true,则可以调用服务器端代码进行删除。
以下是解释代码
Delete
以下是javascript函数:
您可以在下面的链接中查看包含源代码的详细文章
http://www.dotnetpickles.com/2013/03/how-to-show-confirm-message-while.html
谢谢