Link to home
Start Free TrialLog in
Avatar of ASPDEV
ASPDEV

asked on

Get Gridview Cell Value using Javascript

I have a Gridview, where first column is a Link Button (text:view) and on click of the row, I need to get the cell value of next column.If I use below code, I get the row value but it fires on every cell is clicked .
 $(document).ready(function () {
            $("#<%=gvResults.ClientID%> tr:has(td)").click(function (e) {
                var selectedRow = $(this);
                var FirstRow = (selectedRow.children(0)[2].innerText);

Open in new window


I need a simple Javascript function, so I get the cell value(s), on when the very first column(link button) is being clicked.
Avatar of Albert Van Halen
Albert Van Halen
Flag of Netherlands image

According to your selector it will fire on every row having cells.
If you want the event being fired only when the link is being click (I'm not sure what the element name is for a Link Button; i'm only guessing that it's an A-tag), your selector would be this :
 $("#<%=gvResults.ClientID%> a")

Open in new window

In order to get the next cell of the cell in which your link is use the following code:
var theCell = $(this).parents("td:first").next("td");

Open in new window

The text of the cell can be retreived by using the text method :
var theText = theCell.text();

Open in new window

Avatar of ASPDEV
ASPDEV

ASKER

AlbervanHalen,

Tell me whether I can do this:

 <Columns>
                                            <asp:TemplateField>
                                                <HeaderTemplate>
                                                    View</HeaderTemplate>
                                                <HeaderStyle HorizontalAlign="Center" Width="75px" />
                                                <ItemTemplate>
                                                    <asp:LinkButton ID="lnkView" runat="server" Text="View" Font-Underline="false"
                                                        OnClientClick="return view();" ForeColor="Blue" />
                                                </ItemTemplate>
                                            </asp:TemplateField>
 <asp:TemplateField Visible="false">
                                                <HeaderTemplate>
                                                    ID</HeaderTemplate>
                                                <HeaderStyle HorizontalAlign="Center" Width="1px" />
                                                <ItemTemplate>
                                                    <asp:Literal runat="server" ID="ltrlID" Text='<%# Eval("ID") %>' />
                                                </ItemTemplate>
                                            </asp:TemplateField>

Open in new window


Can I pass the ltrlID client id value for the selected row  OnClientClick="return view();" like this view(document.getElementById("<%=ltrlID.ClientID %>"). Is it possible??
ASKER CERTIFIED SOLUTION
Avatar of Albert Van Halen
Albert Van Halen
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ASPDEV

ASKER

I think below code will work:
OnClientClick="return view('<%# Eval("ID") %>');"

Open in new window

But I get error , server tag not well formed.How can pass the EVAL to javascript function.
Tags with mixed single and double quotes...
You can use an html anchor as an alternative:
<a id='<%# Eval("ID") %>' onclick="return view(this.id);">View</a>

Open in new window

Avatar of ASPDEV

ASKER

Thanks, it worked.