繁体   English   中英

如何在GridView命令字段中调用JavaScript函数?

[英]How to call JavaScript function in GridView Command Field?

我已经返回一个Javascript函数,它向用户询问确认消息。 JavaScript函数是

    function ConfirmOnDelete() {
    if (confirm("Are you sure to delete?"))
        return true;
    else
        return false;

和GridView如下所示:

    <asp:CommandField HeaderText="Delete" ShowDeleteButton="True" />

这里我想在用户单击GridView命令字段中的Delete时调用JavaScript函数。 怎么称呼这个?

假设您要继续使用CommandField ,可以使用GridView的OnRowDataBound事件以编程方式执行此操作。

GridView声明中为RowDataBound事件指定事件处理程序:

<asp:GridView ID="gv" runat="server" OnRowDataBound="gv_RowDataBound"....

然后在你的事件处理程序(代码隐藏)中找到你的按钮(这里我假设ImageButton ,虽然这取决于你的CommandField ButtonType属性)并将JavaScript添加到它的OnClientClick属性。

protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        ((ImageButton)e.Row.Cells[cell].Controls[ctrl]).OnClientClick = "return confirm('Are you sure you want to delete?');"; // add any JS you want here
    }
}

在上面的示例中, cell指的是CommandField的列索引, ctrl指的是您引用的单元格中的控件索引(Delete按钮)。

您最好避免在服务器端询问确认删除,使用javascript捕获用户最终决定,然后转到服务器端执行您的逻辑。 CommandField不是这里最好的解决方案。

JS:

<script type="text/javascript">
    function DeleteConfirm() {

        if (confirm("Are you sure you want to delete this customer from excluded customer list ?")) {
            return true;
        }
        return false;
    }
</script>

HTML:

            <asp:TemplateField HeaderText=" ">
                <ItemTemplate>
                    <asp:LinkButton ID="lnk_RemoveFromlist" runat="server" Text="Delete"
                        CommandName="Delete" OnCommand="Delete_Command" CommandArgument='<%# Eval("ID").ToString()' OnClientClick='return DeleteConfirm()'></asp:LinkButton>
                </ItemTemplate>
            </asp:TemplateField>

码:

protected void Remove_Command(object sender, CommandEventArgs e)
{
    //Implement your Delete Logic
}

取消按钮的命令名称为“取消”,删除按钮“删除”,检查

if(e.CommandName == "delete")
 {
   //script to delete();
 }
else if(e.commandName == "cancel")
 {
   //close with some script;
 }

在codebehind中调用javascript函数,

Page.ClientScript.RegisterStartupScript(this.GetType(), "Call my function","loadPopupBox();", true);

通常,您必须指定OnClientClick="return confirm('Are you sure you want to delete ?');" 但这不适用于CommandField更好地使用TemplateField ,这解释得更好http://davesquared.net/2007/10/confirm-delete-for-gridview.html

您可以使用按钮的OnClientclick事件来调用该函数。例如

<asp:button id="btndelete" runat="server" Text="delete" Onclientclick="if(!ConfirmOnDelete())return false;"/>

使用启动脚本

    ScriptManager.RegisterStartupScript(Page, this.GetType(), "ConfirmOnDelete", "ConfirmOnDelete();", true);

或者您也可以使用onclientclick

HTML:

<asp:GridView ID="GridView1" runat="server" OnRowDeleting="gv1_RowDeleting">
   <Columns>
       <asp:TemplateField HeaderText="Delete">
          <ItemTemplate>
                   <asp:CommandField ShowDeleteButton="true" HeaderText="delete" />  
          </ItemTemplate>
       </asp:TemplateField>
   </Columns>
</asp:GridView>

码:

    protected void gv1_RowDeleting (object sender, GridViewDeleteEventArgs e) 
{

    GridView1.Attributes.Add("OnClick", "return confirm('Really wanna delete?');");

       // implement your delete logic

    Response.Write("<script>alert("Delete Successful");</script>");

}

当我们点击Gridview中的“删除”命令字段时,这将调用Javascript函数,Response.write函数将发出数据已被删除的警报。 您可以在此函数中添加要执行的w​​hwtever函数。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM