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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14397309/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 11:42:58  来源:igfitidea点击:

How to call JavaScript function in GridView Command Field?

c#javascriptasp.netgridview

提问by thevan

I have return one Javascript function which asks the confirm message to the user. The JavaScript functions is

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

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

and GridView is like below:

和 GridView 如下所示:

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

Here I want to call the JavaScript function when user clicking the Delete in the GridView Command Field. How to call this?

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

采纳答案by Brissles

Assuming you want to keep using your CommandField, you could do this programmatically using your GridView's OnRowDataBoundevent.

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

Specify the event handler for the RowDataBound event in your GridViewdeclaration:

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

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

Then in your event handler (code-behind) find your button (here I'm assuming ImageButton, though this depends on your ButtonTypeproperty in your CommandField) and add JavaScript to its OnClientClickproperty.

然后在您的事件处理程序(代码隐藏)中找到您的按钮(这里我假设为ImageButton,尽管这取决于您在 中的ButtonType属性CommandField)并将 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
    }
}

In the above example, cellrefers to the column index of your CommandFieldand ctrlrefers to the control index (Delete button) within the cell you're referencing.

在上面的示例中,单元格是指您的列索引,CommandFieldctrl是指您正在引用的单元格内的控件索引(删除按钮)。

回答by Manoj Purohit

In general you have to specify OnClientClick="return confirm('Are you sure you want to delete ?');"but that doesn't work with CommandFieldbetter use TemplateField, this explains better http://davesquared.net/2007/10/confirm-delete-for-gridview.html

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

回答by Sohail Hameed

use start up script

使用启动脚本

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

or you can also use onclientclick as well

或者您也可以使用 onclientclick

回答by MuhammadHani

You better avoid ask the confirmation for deletion on the server side, capture the user final decision using javascript then go to the server side to execute your logic. CommandField will not be the best solution here.

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

JS:

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:

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>

Code:

代码:

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

回答by Sakthivel

for cancel button have command name as "cancel" and for delete button "delete" , check

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

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

To call javascript function in codebehind,

要在代码隐藏中调用 javascript 函数,

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

回答by smith269

you can use OnClientclickevent of button to call the function.For example

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

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

回答by A Beginner

HTML:

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>

CODE:

代码:

    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>");

}

This will Call Javascript function when we will click on "Delete" Command Field in Gridview and Response.write function will give an alert that data has been deleted. You can add whwtever function you want to execute in this function.

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