【问题标题】:Call jQuery function from Gridview link button从 Gridview 链接按钮调用 jQuery 函数
【发布时间】:2016-08-30 05:56:35
【问题描述】:

在我的应用程序中,我有一个 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 函数?

提前致谢。

【问题讨论】:

  • 这个link 会帮助你。
  • Thnq @devansh-nigam。它正在工作
  • 如果您认为我的评论有用,请点赞。谢谢。

标签: c# jquery asp.net gridview


【解决方案1】:

不要通过 id 调用函数,因为它会为每一行生成多个 id。 您可以通过 javascript 来实现这一点,只需附加客户端事件 onclick='myTestFun();'到链接按钮。

【讨论】:

    【解决方案2】:

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

    ASPX 代码

    <asp:LinkButton runat="server" ID="lnkCustomer" Text='<%#Eval("Name") %>'
    OnClientClick="OpenModel();" > </asp:LinkButton>
    

    JS 代码

    $(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);
                }
            });
        });
    

    【讨论】:

      猜你喜欢
      • 2023-03-21
      • 1970-01-01
      • 2012-01-24
      • 2014-05-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多