Accessing a Label Control in a GridViewRow Using Javascript

I have a Label control inside a GridViewRow cell that I need to get from the client side using Javascript. I can access the row of the GridView, but I have been unable for my entire life to figure out how to access the shortcut inside it.

Markup:

<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 different things where ?????

there is, but can't get to the Label control to read from it!

Any ideas please?

+3


source to share


2 answers


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 get the tag id directly.

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

      

+3


source


try it



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

      

+1


source







All Articles