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

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

我在页面顶部有一个 JavaScript 函数,我希望它从 gridview 中的 ButtonField(类型:图像)调用,但我不能,因为 OnClick 不可用。怎么做 ?

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

函数:

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

如果我按是,按钮后面的事件应该只有 运行,否则不会。

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

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

            }

此代码在 RowCommand 事件中。

不适用于 ButtonField

替换

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

<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>

这是你的做法,

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

您好,您可以简单地在 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?');" />