Solved

Get Gridview Cell Value using Javascript

Posted on 2013-01-07
6
1,777 Views
Last Modified: 2013-01-08
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.
0
Comment
Question by:ASPDEV
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 19

Expert Comment

by:Albert Van Halen
ID: 38752794
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

0
 

Author Comment

by:ASPDEV
ID: 38752915
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??
0
 
LVL 19

Accepted Solution

by:
Albert Van Halen earned 500 total points
ID: 38752985
Well, an asp:literal is just plain text client side, so it's not an element. In order to use an element you have to use a span or div.
But why don't you use the value directly in the itemtemplate where the linkbutton is...
<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('<%# Eval("ID") %>');" ForeColor="Blue" />
     </ItemTemplate>
</asp:TemplateField>

Open in new window

0
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 

Author Comment

by:ASPDEV
ID: 38755054
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.
0
 
LVL 19

Expert Comment

by:Albert Van Halen
ID: 38755164
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

0
 

Author Closing Comment

by:ASPDEV
ID: 38755298
Thanks, it worked.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses
Course of the Month10 days, 13 hours left to enroll

632 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question