【问题标题】:How to get Asp.Net GridView control ID in Javascript如何在 Javascript 中获取 Asp.Net GridView 控件 ID
【发布时间】:2019-07-16 06:56:59
【问题描述】:

我确实尝试过搜索并尝试了提供的所有解决方案,但我仍然无法让它工作,javascript 在尝试获取网格视图的元素 ID 时返回 null。如何从显示为的 Web 浏览器获取 gridview 的客户端 ID --> id="ctl00_ContentPlaceHolder1_GridView5"

function Validate() {
var GridID = document.getElementById('<%= GridView5.ClientID %>');

alert(GridID);

}

<asp:GridView ID="GridView5" runat="server" OnRowDataBound="GridView5_RowDataBound" AutoGenerateColumns = "False"  HorizontalAlign="Center">

我做错了什么?

【问题讨论】:

  • ctl00_ContentPlaceHolder1_GridView5 表示您正在使用自动生成的客户端 ID。如果您只想查看GridView5 部分,请将ClientIDMode="Static" 放在该网格视图上。
  • @TetsuyaYamamoto,我试过了,并将 javascript 代码更改为 this ,但我得到了这个结果 --> [object HTMLTableElement],我的 js 代码 - var GridID = document.getElementById( "GridView5");
  • document.getElementById 获取id 属性指定的元素,即来自gridview 的&lt;table&gt; 元素。如果你只是想获取控制 ID 作为字符串,简单的var GridID = '&lt;%= GridView5.ClientID %&gt;'; 就足够了。
  • 不,它只是返回 this 作为它的值,看起来我在这里指定了一个字符串。
  • 这是aspx页面上的Javascript代码还是外部javascript文件??

标签: javascript asp.net gridview clientid


【解决方案1】:
Please try this,
var grid = $("[id*=GridView5]") 
or use
var grid = $("#<%=GridView5%>")
write this line in current page script

【讨论】:

    【解决方案2】:

    由于Javascript代码在外部文件中,不能使用&lt;%= GridView5.ClientID %&gt;表达式。 我建议您设置GridView.ClientIDMode="Static" 并使用以下代码访问外部javascript 文件中的gridview。 如果这是你的网格视图

    <asp:GridView ID="GridView5" runat="server" OnRowDataBound="GridView5_RowDataBound" AutoGenerateColumns = "False"  HorizontalAlign="Center" ClientIDMode="Static">
    

    然后使用爆破代码

    var grid = $("#GridView5");
    

    【讨论】:

      【解决方案3】:

      感谢您的建议。这对我有用,因为我的 java 脚本在一个单独的文件中。

      var tbl = document.getElementById("GridView5");
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-12-21
        • 2013-12-04
        • 2016-06-10
        • 2010-11-10
        相关资源
        最近更新 更多