How do I get the selected row in Gridview in Javascript or JQuery
Solution 1
Try this... Change naming conventions according to your requirement
<script type = "text/javascript">
function GetSelectedRow(UserLink) {
var row = UserLink.parentNode.parentNode;
var rowIndex = row.rowIndex - 1;
var Userid = row.cells[0].innerHTML;
alert("RowIndex: " + rowIndex + " Userid : " + Userid + ");
return false;
}
</script>
Solution 2
Your first problem is that in your button template column you are using an Id=UserLink
which will get repeated for each row. Your element id's should be unique. See this answer for more details.
Your second problem is that document.getElementById('UserLink');
will actually find the first element with the id of UserLink
which is always going to be the first row. In your case you can us var ul = this;
and that should give you the button that was actually clicked.
To summarize:
- I would suggest getting ride of the id on your button or make them unique.
- Change
document.getElementById('UserLink')
tothis
and that will get you the button that was clicked.
Paul T. Rykiel
Hi, I am 20+ year IT guy, I am in my 2.0 phase, having learned how to code years ago in Cobol and RPG and Pascal. I stayed working in software and ERP systems development, and later consulting and Project management. I earned a Master's Degree in Project Management and hold a Bachelor's degree in Computer Information systems. I am also PMP and CSM certified. In my 2.0 Phase, I am Java, .NET and c, c++, c#, JavaScript, Objective c, and Xcode guy, I work as a .NET web developer and I am in school continuing my education in pursuit of a Master's Degree in Computer Science. So, I may ask questions that are work related, and questions that are school related and I hope that I am in the position to pay it forward to someone else in need!! I like helping people and I believe in Karma!!
Updated on July 09, 2022Comments
-
Paul T. Rykiel almost 2 years
Let me elaborate ... below is my partial gridview with a button field ... what I want is the text value of the button to use in building a url:
<asp:GridView CssClass="gridUser" ID="UsersGrid" ClientIDMode="Static" runat="server" AllowPaging="true" AllowSorting="true" PageSize="5" OnPageIndexChanging="PageIndexChange" OnRowCommand="Paging" OnSorting="SortPage" OnRowCreated="RowCreated" AutoGenerateColumns="false"> <PagerSettings Mode="NextPreviousFirstLast" FirstPageImageUrl="~/Images/Navigation/firstpageIcon.png" LastPageImageUrl="~/Images/Navigation/lastpageIcon.png" NextPageImageUrl="~/Images/Navigation/forwardOneIcon.png" PreviousPageImageUrl="~/Images/Navigation/backOneIcon.png" Position="Bottom" /> <Columns> <asp:TemplateField HeaderText="User Name" HeaderStyle-CssClass="gridHeader"> <ItemTemplate> <asp:Button ID="UserLink" ClientIDMode="Static" runat="server" Text='<%#Eval("UserName") %>' CssClass="linkButton" OnClientClick = "return GetSelectedRow(this)"></asp:Button> </ItemTemplate> </asp:TemplateField>
and here is my javascript :
$(".linkButton").click(function () { var ul = document.getElementById('UserLink'); var row = ul.parentNode.parentNode; var rowIndex = row.rowIndex - 1; var Userid = row.cells[0].childNodes[1].value; var User = Userid; alert("Row is : " + row + "RowIndex is : " + rowIndex + "User ID is " + User); });
My issue is that my row returned is the first row ... not the selected row ... how do I get the selected row? Thank you for any assistance.
-
Paul T. Rykiel almost 9 yearsThank you this will work, but I need it to work inside of the Document.ready object ... it will not be recognized inside of document ready.
-
Paul T. Rykiel almost 9 yearsthis works, but when I change "document.getElementbyId(this) ... my var is null, eventhough this is showing the correct linkbutton. ... I am think I am close to my answer. Thank you
-
Paul T. Rykiel almost 9 yearsso, I took out the document.getElementbyId(this) to var ul = this; works great!! Thank you so much for your assistance.
-
Jared almost 9 yearsLet me clarify. Don't use
document.getElementById(this)
usevar ul = this;
. -
Jared almost 9 years@PaulT.Rykiel, no problem. If you can mark it as the answer so that others may reference it that would be great.