Luciano Minetti
Luciano Minetti

Reputation: 27

How to get value from GridView cell with Javascript

I'm trying to get a value from a GridView cell to change the source of an embed PDF. I execute a function when a button that is placed on each row of the GridView is clicked.

This is the code I have so far:

<script>

    function changepdf(pdf) {

        var value = document.getElementById('<%= GridView1.ClientID %>').rows[pdf.rowIndex].cells[3].innerText;
        var file = document.getElementById('pdf');
        file.src = 'pdf/catalogosh.pdf#page=' + value + '&#toolbar=0&#view=fit';
        return false;
    }

</script>

The Javascript function works fine when I write exactly the new source like this:

<script>

    function changepdf(pdf) {

        var file = document.getElementById('pdf');
        file.src = 'pdf/catalogosh.pdf#page=4&#toolbar=0&#view=fit';
        return false;
    }

</script>

The main idea is to change the PDF page taking the value from de GridView.

Thanks in advance for any help.

Upvotes: 0

Views: 2364

Answers (1)

Luciano Minetti
Luciano Minetti

Reputation: 27

I've just solved it. I'm pasting the code here for anyone that's trying to solve the same problem:

        function changepdf(pdf) {

        var row = pdf.parentNode.parentNode;
        var rowIndex = row.rowIndex;
        var value = document.getElementById('<%= GridView1.ClientID %>').rows[rowIndex].cells[2].innerText;
        var file = document.getElementById('pdf');
        file.src = 'pdf/catalogosh.pdf#page=' + value + '&#toolbar=0&#view=fit';

        return false;

    }

My problem was the rowIndex. The code was bad and I was getting the wrong value. I added this two lines to get it well:

var row = pdf.parentNode.parentNode;
var rowIndex = row.rowIndex;

And then I got the cell value with this line:

var value = document.getElementById('<%= GridView1.ClientID %>').rows[rowIndex].cells[3].innerText;

Upvotes: 1

Related Questions