在ASP Gridview删除命令中使用JavaScript提示来存储"删除原因"

时间:2016-06-08 13:45:55

标签: javascript c# asp.net gridview

我目前在GridView中为我的删除命令提供了一个JavaScript确认提示,如下所示:

<asp:TemplateField>
    <ItemTemplate>
        <asp:LinkButton  runat="server" 
            CommandName="Delete" 
            CausesValidation="False" 
            CssClass="adminLinks" 
            NavigateUrl="#" 
            Text="delete"
            OnClientClick="return confirm('Are you sure you want to DELETE this record?');"/>
    </ItemTemplate>
</asp:TemplateField>

然后,对于我的删除命令,我正在运行此更新代码:

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
    ConnectionString="<%$ ConnectionStrings:sqlConnection2 %>"
    DeleteCommand="UPDATE [jeakins].[tbl_submit] SET recycle_bin=1 WHERE [ID] = @ID">
</asp:SqlDataSource>

如何将此更改从确认框更改为提示框&#34;删除原因?&#34;然后在DeleteCommand中使用该变量来更新delete_reason字段?

1 个答案:

答案 0 :(得分:1)

您可以定义confirmDelete Javascript函数,该函数显示提示框,如果用户单击确定,则将原因存储在HiddenField中,但如果单击取消<取消删除< / KBD>

function confirmDelete() {
    var reason = prompt('Why do you want to delete?', '');

    if (reason !== null) {
        document.getElementById('hfDeleteReason').value = reason;
        return true;
    }
    else {
        return false;
    }
}

HiddenField被添加到标记中(GridView外部),并且LinkBut​​ton调用confirmDelete

<asp:HiddenField ID="hfDeleteReason" runat="server" ClientIDMode="Static" />

<asp:LinkButton  runat="server" OnClientClick="return confirmDelete();" ... />

如果用户确认删除,您可以从代码隐藏中的HiddenField中检索原因:

string reason = hfDeleteReason.Value;

然后,您必须调整DeleteCommand以包含用户输入的原因。它可以使用其他参数更新delete_reason字段:

DeleteCommand="UPDATE [jeakins].[tbl_submit] SET recycle_bin=1, delete_reason = @Reason WHERE [ID] = @ID">
...
<DeleteParameters>
    <asp:Parameter Name="Reason" Type="String" DefaultValue="" />
</DeleteParameters>

将在代码隐藏中设置:

SqlDataSource1.DeleteParameters["Reason"].DefaultValue = hfDeleteReason.Value;