MusicLovingIndianGirl
MusicLovingIndianGirl

Reputation: 5947

Unable to get selected row index of gridview in javascript

I have got a gridview with certain rows/columns and an edit button for each row. When the edit button is clicked, it opens a popup window with a textbox and a button. I want to know the index of the selected row on click of the button inside the popup. I added code like so

var table = document.getElementById('<%= gvTimeSlots.ClientID%>');
var Row;
for (var i = 1; i < table.rows.length; i++) {
    Row = table.rows[i];
    alert(Row);
}

But the alert gives me "Undefined". What am I missing here?

Upvotes: 1

Views: 15615

Answers (2)

Ramesh Rajendran
Ramesh Rajendran

Reputation: 38683

Simply you can get the row index like

function GetSelectedRow(lnk) {
        alert("RowIndex: " + lnk.$index;);//This lnk.$index will get the index 
        return false;
    }

Upvotes: 1

MusicLovingIndianGirl
MusicLovingIndianGirl

Reputation: 5947

This is my fix..

function GetSelectedRow(lnk) {
        var row = lnk.parentNode.parentNode;
        var rowIndex = row.rowIndex - 1;
        alert("RowIndex: " + rowIndex);
        return false;
    }

I am calling this function in Onclientclick event of the link button.

<asp:TemplateField HeaderStyle-HorizontalAlign="Left" HeaderStyle-Width="10%" Visible="true">
     <ItemTemplate>
          <asp:LinkButton ID="lnkViewTimeSlots" runat="server" Text="Edit" ForeColor="Blue" OnClick="lnkViewTimeSlots_click" OnClientClick="return GetSelectedRow(this); javascript:shouldsubmit=true;" CausesValidation="false" Style="padding: 0px; margin: 0px;"></asp:LinkButton>
     </ItemTemplate>
 </asp:TemplateField>

Upvotes: 5

Related Questions