将JavaScript确认提示添加到ASP.NET网格视图中的删除命令按钮?

时间:2010-01-04 15:53:38

标签: c# asp.net javascript gridview

所以我有一个ASP.NET网格视图:

<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>

我想在删除命令按钮上添加一些JavaScript,以便在删除行之前要求确认。

有什么想法吗?

4 个答案:

答案 0 :(得分:14)

您始终可以使用TemplateField而不是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>

答案 1 :(得分:3)

当我这样做时,我使用了一个带有Ajax Control Toolkit中ConfirmButtonExtender的模板字段。

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

答案 2 :(得分:0)

这是一个用于删除确认的JavaScript。

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

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

这是一个gridview字段,您可以在其中调用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>

答案 3 :(得分:0)

RowDataBound添加 -

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