2013-10-28 82 views
0

我试图得到确认消息同时点击的GridView删除按钮。如果我符合只有该行将被删除GridView在CommandField中删除确认消息?

* .ASPX

<Columns> 

    <asp:CommandField ButtonType="Button" ShowDeleteButton="true" /> 

</Columns> 

* .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 !"; 
} 

回答

3

我回答朋友

<asp:TemplateField> 
     <ItemTemplate> 
      <asp:Button ID="deletebtn" runat="server" CommandName="Delete" 
      Text="Delete" OnClientClick="return confirm('Are you sure?');" /> 
     </ItemTemplate> 
</asp:TemplateField> 

我改变CommandField中模板列

谢谢!

0

更改为下面的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?');"; 
    } 
} 
+0

我使用上面的代码..我得到错误,如“无法投入类型'System.Web.UI.LiteralControl'的对象来键入'System.Web.UI.WebControls.Button'。 “ – kasim

1

只需在onclientclick事件上调用javascript函数并要求确认。如果它返回true,那么你可以调用服务器端代码来删除。

下面是解释代码

<asp:LinkButton ID="lbDelete" runat="server" OnClick="lbDelete_Click" OnClientClick="return fnConfirm();"> Delete</asp:LinkButton> 

及以下JavaScript函数:

<script type="text/javascript"> 
function fnConfirm() { 
    if (confirm("The item will be deleted. Are you sure want to continue?") == true) 
     return true; 
    else 
     return false; 
} 
</script> 

您可以查看源代码的详细文章在下面的链接

http://www.dotnetpickles.com/2013/03/how-to-show-confirm-message-while.html

谢谢

0
<asp:TemplateField HeaderText="DELETE" ShowHeader="False"> 
         <ItemTemplate> 
         <span onclick="return confirm('Are you sure to Delete?')"> 
          <asp:Button ID="Button1" runat="server" CausesValidation="False" 
           CommandName="Delete" Text="Delete" /> 
         </ItemTemplate> 
</asp:TemplateField>