最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - Adding a JavaScript confirmation prompt to a delete command button in an ASP.NET grid view? - Stack Overflow

programmeradmin0浏览0评论

So I have an ASP.NET grid view:

<asp:GridView ID="gvReferences" runat="server" AutoGenerateColumns="False" ShowHeader="False"
    OnRowEditing="gvReferences_RowEditing" 
    OnRowDeleting="gvReferences_RowDeleting" onrowcreated="gvReferences_RowCreated">
    <Columns>
        <asp:TemplateField ItemStyle-Width="400px">
            <ItemTemplate>
                <asp:Label ID="lblId" Visible="false" runat="server" Text='<%# Eval("Id") %>' />
                <asp:Label ID="lblAssociatedSpecies" runat="server" Text='<%# Eval("Text") %>' />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:Label ID="lblKind" runat="server" Text='<%# Eval("Kind") %>' />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:CommandField ButtonType="Button" DeleteText="delete" ShowDeleteButton="True"
            ShowCancelButton="False" EditText="edit" ShowEditButton="True">
            <ControlStyle Width="60px" />
        </asp:CommandField>
    </Columns>
</asp:GridView>

I'd like to attach some JavaScript to the Delete command button to ask for confirmation before a row is deleted.

Any ideas?

So I have an ASP.NET grid view:

<asp:GridView ID="gvReferences" runat="server" AutoGenerateColumns="False" ShowHeader="False"
    OnRowEditing="gvReferences_RowEditing" 
    OnRowDeleting="gvReferences_RowDeleting" onrowcreated="gvReferences_RowCreated">
    <Columns>
        <asp:TemplateField ItemStyle-Width="400px">
            <ItemTemplate>
                <asp:Label ID="lblId" Visible="false" runat="server" Text='<%# Eval("Id") %>' />
                <asp:Label ID="lblAssociatedSpecies" runat="server" Text='<%# Eval("Text") %>' />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:Label ID="lblKind" runat="server" Text='<%# Eval("Kind") %>' />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:CommandField ButtonType="Button" DeleteText="delete" ShowDeleteButton="True"
            ShowCancelButton="False" EditText="edit" ShowEditButton="True">
            <ControlStyle Width="60px" />
        </asp:CommandField>
    </Columns>
</asp:GridView>

I'd like to attach some JavaScript to the Delete command button to ask for confirmation before a row is deleted.

Any ideas?

Share Improve this question asked Jan 4, 2010 at 15:53 Jeremy McGeeJeremy McGee 25.2k10 gold badges64 silver badges96 bronze badges 1
  • 1 I deleted my answer and i agree with @Stephen Wrighton answer – Amr Elgarhy Commented Jan 4, 2010 at 16:01
Add a comment  | 

4 Answers 4

Reset to default 14

You could always use a TemplateField rather than the CommandField.

<asp:TemplateField>
  <ItemTemplate>
    <asp:Button name="btnDelete" commandName="Delete" OnClientClick="return confirm('Delete 
this Item');" Text="Delete" runat="server" />
    <asp:Button name="btnEdit" commandName="Edit" Text="Edit" runat="server" />
  </ItemTemplate>
</asp:TemplateField>

When I've done this I've used a Template Field with the ConfirmButtonExtender from the Ajax Control Toolkit.

<asp:TemplateField>
   <ItemTemplate>   
       <asp:Button name="DeleteButton" commandName="Delete" Text="Delete" runat="server" />   
       <ajaxToolkit:ConfirmButtonExtender TargetControlId="DeleteButton" ConfirmText="Delete this entry?" />
   </ItemTemplate>   
</asp:TemplateField>  

This is a javascript for delete confirmation.

 function not_check1()
            {
              var where_to1= confirm("Do you really want to delete this record??");

                                    if (where_to1 == true)
                                        {
                                            return true;
                                        }
                                    else
                                        {
                                            return false;
                                        }
           }

This is a gridview field from where you call the javascript.

 <asp:TemplateColumn ItemStyle-Width="20" >
<ItemTemplate>
 <asp:ImageButton ID="ib_delete" runat="server" ImageUrl="~/image/images.jpg" commandName="Delete"  OnClientClick="return not_check1();" ImageAlign="Middle"/></ItemTemplate>

</asp:TemplateColumn>

In RowDataBound add -

LinkButton objDelete = e.Row.Cells[0].Controls[0] as LinkButton;
objDelete.Attributes.Add("onclick", "javascript:return confirm('Do you want to delete this item?');");
发布评论

评论列表(0)

  1. 暂无评论