从Gridview链接按钮调用jQuery函数

本文关键字:jQuery 函数 调用 按钮 Gridview 链接 | 更新日期: 2023-09-27 18:04:34

在我的应用程序中,我有一个jQuery函数,我想从gridview链接按钮调用这个函数。我尝试了下面的代码,但是没有调用函数:

<script type="text/javascript">     
    $(function () {
        var JavascriptBlah = '<%=msUntilFour%>'
        var fileName = '<%=msUntilFour%>';          
        $('input[id$="lnkCustomer"]').click(function () {
            $("#dialog").dialog({
                modal: true,
                title: fileName,
                width: 600,
                height: 600,
                buttons: {
                    Close: function () {
                        $(this).dialog('close');
                    }
                },
                open: function () {
                    var object = "<object data='"{FileName}'" type='"application/pdf'" width='"700px'" height='"700px'">";
                    object += "If you are unable to view file, you can download from <a href = '"{FileName}'">here</a>";
                    object += "</object>";
                    object = object.replace(/{FileName}/g, "Doc/Demo.pdf");
                    $("#dialog").html(object);
                }
            });
        });
    });
</script>

Aspx标记:

 <asp:GridView ID="gridView1" runat="server" CssClass="mydatagrid" 
      HeaderStyle-CssClass="header" RowStyle-CssClass="rows" 
      AutoGenerateColumns="false"
      PageSize="10" EmptyDataText="No Records Available">
      <HeaderStyle CssClass="header"></HeaderStyle>
      <PagerStyle ForeColor="Red" HorizontalAlign="Center"></PagerStyle>
      <Columns>
          <asp:BoundField DataField="Id" HeaderText="Id" />
          <asp:BoundField DataField="Name" HeaderText="Name" />
          <asp:TemplateField HeaderText="CustomerID">
              <ItemTemplate>
                  <asp:LinkButton runat="server" ID="lnkCustomer" Text='<%#Eval("Name") %>'>
                  </asp:LinkButton>
              </ItemTemplate>
          </asp:TemplateField>
      </Columns>
      <RowStyle CssClass="rows"></RowStyle>
   </asp:GridView>
</div>       
<div id="dialog" style="display: none">

如何从gridview链接按钮调用jQuery函数?

提前感谢。

从Gridview链接按钮调用jQuery函数

不要按id调用函数,因为它会为每行生成多个id。你可以通过javascript简单地附加一个客户端事件onclick='myTestFun();'到LinkButton。

首先可以在脚本部分声明函数,然后在LinkButton中添加OnClientClick属性。

<<p> ASPX代码/strong>
<asp:LinkButton runat="server" ID="lnkCustomer" Text='<%#Eval("Name") %>'
OnClientClick="OpenModel();" > </asp:LinkButton>
<<p> JS代码/strong>
$(document).ready(function(){
   OpenModel();
});
function OpenModel() {
        $("#dialog").dialog({
            modal: true,
            title: fileName,
            width: 600,
            height: 600,
            buttons: {
                Close: function () {
                    $(this).dialog('close');
                }
            },
            open: function () {
                var object = "<object data='"{FileName}'" type='"application/pdf'" width='"700px'" height='"700px'">";
                object += "If you are unable to view file, you can download from <a href = '"{FileName}'">here</a>";
                object += "</object>";
                object = object.replace(/{FileName}/g, "Doc/Demo.pdf");
                $("#dialog").html(object);
            }
        });
    });