【问题标题】:return confirm inside gridview does not fire在gridview中返回确认不会触发
【发布时间】:2013-08-06 18:41:20
【问题描述】:

在我粘贴在网格下方的代码中,可以选择在单击删除按钮时删除该行。在点击服务器端代码之前,我想确认用户删除记录。

但总是点击服务器端代码,而不是首先显示确认弹出窗口。

 <asp:GridView ID="grdDelegateList" runat="server" CssClass="gridviewBorder" AutoGenerateColumns="False"
                    CellPadding="1" Style="margin-left: 0px" BackColor="White" Font-Names="Calibri"
                    BorderWidth="1px" Width="100%" AllowPaging="True" GridLines="Horizontal" RowHoverBackColor="#666666"
                    RowHoverForeColor="White" SelectedRowStyle-BackColor="#333333" SelectedRowStyle-ForeColor="White"
                    PageSize="10" OnPageIndexChanging="grdDelegateList_PageIndexChanging" OnRowCommand="grdDelegateList_RowCommand"
                    OnRowDataBound="grdDelegateList_RowDataBound" OnRowDeleting="grdDelegateList_RowDeleting">
                    <Columns>
                        <asp:BoundField HeaderText="Employee ID" DataField="DelegateID" ItemStyle-HorizontalAlign="Center" HeaderStyle-HorizontalAlign="Center" />
                        <asp:TemplateField HeaderText="Full Name" ItemStyle-HorizontalAlign="Center" HeaderStyle-HorizontalAlign="Center">
                            <ItemTemplate>
                                <p>
                                    <%#DataBinder.Eval(Container.DataItem, "Owner.FirstName")%>
                                    &nbsp;&nbsp;
                                    <%#DataBinder.Eval(Container.DataItem, "Owner.LastName")%>
                                </p>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Remove" ItemStyle-HorizontalAlign="Center" HeaderStyle-HorizontalAlign="Center">
                            <ItemTemplate>
                                <span style="cursor: pointer">
                                    <asp:LinkButton ID="ImgRemove" runat="server" CommandName="Delete" CommandArgument='<%# Eval("ID") %>'
                                        Text="Remove" OnClientClick="return confirm(Are you sure you want to remove this Delegate);">
                                    </asp:LinkButton></span>
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                                    </asp:GridView>

我想要做的是显示一个确认框,如果用户按下按钮服务器端代码(即:行命令)事件应该被击中,否则取消它应该什么都不做。但它不起作用。

【问题讨论】:

标签: asp.net


【解决方案1】:

confirm() 函数的代码无效。

confirm() 需要一个字符串变量 (see window.confirm) 但在您的情况下,您传入的是无效对象并会收到错误

Uncaught SyntaxError: Unexpected identifier

将您的代码更新为;

<asp:LinkButton 
    ID="ImgRemove" 
    runat="server" 
    CommandName="Delete" 
    CommandArgument='<%# Eval("ID") %>' 
    Text="Remove" OnClientClick="return confirm('Are you sure you want to remove this Delegate');">

【讨论】:

    【解决方案2】:

    我的建议是在 OnClientClick 中添加单引号

    OnClientClick="return confirm('Are you sure you want to remove this Delegate');
    

    你也可以试试下面的代码:

    OnClientClick="if (!confirm('Are you sure you want to remove this Delegate')) return false;" 
    

    【讨论】:

    • 之所以有效,是因为 confirm() 方法中的文本周围有 '。没有理由需要将if 添加到OnClientClick 中。 confirm() 表示布尔值,因此您应该始终使用代码 return confirm('text');
    【解决方案3】:

    试试下面的 sn-p。

    <asp:LinkButton ID="ImgRemove" runat="server" CommandName="Delete" CommandArgument='<%# Eval("ID") %>' Text="Remove" OnClientClick="if (!confirm('Are you sure you want to remove this Delegate?'));">
    
    • OnClientClick="if (!confirm('Are you sure you want to remove this Delegate?'));
      将显示带有是/否选项的警报。如果您单击“否”,则不会发生任何事情。如果您单击是,则将执行相应的代码

    【讨论】:

      猜你喜欢
      • 2023-03-10
      • 1970-01-01
      • 1970-01-01
      • 2014-08-21
      • 2013-04-11
      • 2019-09-05
      • 2016-03-17
      • 2012-04-02
      • 2022-01-11
      相关资源
      最近更新 更多