2013-10-28 120 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>