【问题标题】:How do I call a JS function from ButtonField in gridview ?如何从 gridview 中的 ButtonField 调用 JS 函数?
【发布时间】:2015-09-15 12:56:08
【问题描述】:

我在页面顶部有一个 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 事件中。

【问题讨论】:

标签: javascript c# asp.net webforms


【解决方案1】:

这不适用于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>

【讨论】:

  • 好的,谢谢先生,它起作用了,但是无论我单击“是”还是“否”,DELETE 功能都不会处理。它什么也没做
  • 即出现一个MESSAGE BOX,很好,但是之后没有调用DELETE函数,为什么?
【解决方案2】:

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

【讨论】:

    【解决方案3】:

    这就是你的做法,

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-07-31
      • 1970-01-01
      • 2017-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多