简体   繁体   English

如何在gridview的ButtonField中调用JS函数?

[英]How do I call a JS function from ButtonField in gridview ?

I have a JavaScript function at the top of the page, I want it to call from ButtonField (type: Image) in gridview but I can't since OnClick is not available. 我在页面顶部有一个JavaScript函数,我希望它可以从gridview中的ButtonField(类型:Image)中调用,但由于OnClick不可用,所以无法使用。 How to do it ? 怎么做 ?

 <asp:ButtonField CommandName="cmdDelete" ImageUrl="~/assets/global/images/shopping/delete.png" ButtonType="Image" ControlStyle-Width="25px" ControlStyle-Height="20px" />

Function: 功能:

 function GetConfrim() {
            if (confirm("Are You Sure ?")) {
                return true;
            } else {
                return false;
            }
        }

And the event behind button should Only run if I press yes otherwise not. 并且按钮后面的事件应该仅在我按是的情况下运行,否则不运行。

if (e.CommandName == "cmdDelete")
            {

                MngPropertyDetails.DeletePropertyDetails(PropertyDetailsID);
                ResultLabel.ResultLabelAttributes("Record Delete Successfully!", ProjectUserControls.Enums.ResultLabel_Color.Green);
                ShowPropertyDetails();

            }

This code is inside RowCommand Event. 这段代码在RowCommand事件内部。

that doesn't work with a ButtonField 不适用于ButtonField

Replace 更换

<asp:ButtonField  CommandName="cmdDelete" ImageUrl="~/assets/global/images/shopping/delete.png" ButtonType="Image" ControlStyle-Width="25px" ControlStyle-Height="20px" />

with

<asp:TemplateField>
  <ItemTemplate>
    <asp:ImageButton ID="btnDelete" runat="server"  ImageUrl="~/assets/global/images/shopping/delete.png"  CommandName="cmdDelete" OnClientClick="return confirm('Are you Sure ?');" Width="25" Height="20"/>
  </ItemTemplate>
</asp:TemplateField>

Here's how you do it, 这是你的做法,

   Button btn = (Button)e.Row.Cells[1].Controls[1];
   btn.Attributes.Add("onclick","yourFunction()");

Hi you can simply display confirm box on OnclientClick as below 您好,您可以在OnclientClick上简单地显示确认框,如下所示

   <asp:ImageButton runat="server" ImageUrl="/xx/xx/icon-close.png" CausesValidation="false" CommandName="xxx"
                                    CommandArgument='xxx' OnClientClick="return confirm('Are you sure you want to delete this?');" />

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

相关问题 如何在POST中从另一个js文件调用函数? - How do I call a function from another js file in a POST? 如何使用html按钮从.js文件调用函数? - How do I call a function from .js file with a html button? 如何从Node.js调用Python函数? - How do I call a Python function from Node.js? 如何从node.js中的函数调用结构内部的函数? - How do I call a function inside a struct from that function in node.js? 如何从作为AJAX调用的回调执行的js.erb触发JS函数? - How do I trigger a JS function from a js.erb executed as a callback on an AJAX call? 我如何在 vanilla js 中调用 node js 的函数? - How do i call a function of node js in vanilla js? 如何在需要return语句的GraphQL解析器中调用异步node.js函数? - How do I call an asynchronous node.js function from within a GraphQL resolver requiring a return statement? 如何从ejs模板调用app.js中定义的函数? - How do I call a function defined in app.js from my ejs template? 如何从Rails资产管道中的不同文件调用JS函数 - How do I call a JS function from a different file in the Rails asset pipeline 如何从视图调用我的 viewmodel 函数 - viewmodel 的子级(knockout.js) - How do I call my viewmodel function from its view - child of viewmodel (knockout.js)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM