最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - Accessing a Label control within a GridViewRow with Javascript - Stack Overflow

programmeradmin5浏览0评论

I have a Label control within a GridViewRow cell, that I need to access from the client side using Javascript. I can access the GridView row fine, but I can't for the life of me work out how to access the Label within it.

Layout:

<asp:TemplateField>
    <ItemTemplate>
        <asp:Label ID="link_lbl" runat="server" Visible="false" Text='<%# BIND("link") %>'></asp:Label>
    </ItemTemplate>
</asp:TemplateField>

Javascript:

var gridview = document.getElementById("<%= list_gv.ClientID %>");
for (i = 1; i < gridview.rows.length; i++) {
    var label = gridview.rows[i].cells[0].?????
}

I've tried various things where the ????? is, but can't get to the Label control to read from it!

Any ideas please?

I have a Label control within a GridViewRow cell, that I need to access from the client side using Javascript. I can access the GridView row fine, but I can't for the life of me work out how to access the Label within it.

Layout:

<asp:TemplateField>
    <ItemTemplate>
        <asp:Label ID="link_lbl" runat="server" Visible="false" Text='<%# BIND("link") %>'></asp:Label>
    </ItemTemplate>
</asp:TemplateField>

Javascript:

var gridview = document.getElementById("<%= list_gv.ClientID %>");
for (i = 1; i < gridview.rows.length; i++) {
    var label = gridview.rows[i].cells[0].?????
}

I've tried various things where the ????? is, but can't get to the Label control to read from it!

Any ideas please?

Share Improve this question asked Feb 8, 2013 at 15:17 BenBen 531 silver badge5 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

Try this code

var gridview = document.getElementById("<%= list_gv.ClientID %>");
for (i = 1; i < gridview.rows.length; i++) {
    var labels = gridview.rows[i].cells[0].getElementsByTagName("span");
     alert(labels[0].innerHTML);
}

This will give you the first label text in cell[0]. You can also directly get by using label id.

var gridview = document.getElementById("<%= list_gv.ClientID %>");
for (i = 1; i < gridview.rows.length; i++) {
    var RowNumber=(i+1).toString();
    if (i<10)
    RowNumber="0"+(i+1).toString();
    var label =  document.getElementById(gridview.id.toString()+"_ctl" + RowNumber + "_link_lbl");
    alert(label.innerHTML);
}

Try this

var label =gridview.rows[i].cells[0].getElementsByTagName("span")
发布评论

评论列表(0)

  1. 暂无评论