How do get the data of the selected row in a datagridusing javascript.

amukta
amukta used Ask the Experts™
on
I have a datagrid.

I have a link button for one of the columns.
When I click on the linkbutton, I should be able to get the data of the selected row.

Any code/logic is greatly appreciated.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
Here's a sample of what you are asking about (see snippet). By simply attaching a JS event to your linkbutton and passing the 'this' keyword you can use that reference to easily get the row the linkbutton resides in, and then the individual data.
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
    <script type="text/javascript">
		function getRowData(lbtn)
		{
			var row = lbtn.parentNode.parentNode; //refers to row
			alert(row.cells[1].innerHTML + ' / ' + row.cells[2].innerHTML);
		}
    
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
           <Columns>
               <asp:TemplateField>
                    <ItemTemplate>
                     <asp:CheckBox runat="server" ID="chkSelected"/>
                    </ItemTemplate>
               </asp:TemplateField>
               <asp:BoundField DataField="testid" HeaderText="ID" />
               <asp:BoundField DataField="fName" HeaderText="First Name" />
               <asp:TemplateField>
				<ItemTemplate>
					<asp:LinkButton ID="lnkBtn" runat="server" Text="ClickMe" OnClientClick="getRowData(this);return false;" />
				</ItemTemplate>
				</asp:TemplateField>
           </Columns>
        </asp:GridView>
        <br />
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Submit" />
        
        <span>Checked Row Values Here: </span>
        <asp:TextBox ID="txtResults" runat="server" />
    </form>
</body>
</html>

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial