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

How do I get the selected row in Gridview in Javascript or JQuery - Stack Overflow

programmeradmin4浏览0评论

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.

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.

Share Improve this question asked Jun 1, 2015 at 17:25 Paul T. RykielPaul T. Rykiel 1,2276 gold badges27 silver badges52 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

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>

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') to this and that will get you the button that was clicked.
发布评论

评论列表(0)

  1. 暂无评论