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

时间:2015-09-15 12:56:09

标签: javascript c# asp.net webforms

我在页面顶部有一个JavaScript函数,我希望它在gridview中从ButtonField(类型:Image)调用但是我不能,因为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 Event内。

3 个答案:

答案 0 :(得分: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>

答案 1 :(得分:1)

这是你如何做到的,

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

答案 2 :(得分:1)

您好,您只需在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?');" />