0

I need to pass the newly selected row index of the gridview to my javascript in aspx page. But the code only fetch the initial value of the variable and when the script runs again, it contains the not updated value.

variable in my code behind is "SelectedRowIndex" which is an integer

    window.addEventListener("keydown", function (event) {
        console.log('<%= SelectedRowIndex %>');
        var validArrowKey = false;
        var index = '<%= SelectedRowIndex %>';

        if (event.keyCode == 40) {
            validArrowKey = true;
            index++;
        }
        else if (event.keyCode == 38 && index > -1) {
            validArrowKey = true;
            index--;
        }

        if (validArrowKey) {
            var trPaymentDetails = document.getElementById("trPaymentDetails_" + index.toString())

            if (trPaymentDetails) {
                __doPostBack('ctl00$MainContent$grdPaymentDetails', 'Select$' + index.toString());
            }
            else
                __doPostBack('ctl00$MainContent$grdPaymentDetails', 'Select$' + index.toString());

        }
    }, false);

To set the initial value for the selected row index, the user must click a row in the gridview first, inside that inclick trigger will set the variable to the user's selected row.

2
  • 1
    You just assigned an anonymous function that will be executed each time when keydown event was fired. So the variable declaration inside will be executed each time as well. That's why it will always be initial value. Try to declare index as a global variable. Commented Aug 24, 2017 at 8:21
  • The user first must click a row in the gridview to set the first index. Commented Aug 24, 2017 at 8:36

1 Answer 1

1

Make the index variable global by moving it outside the function, then it will only be set once.

var index = '<%= SelectedRowIndex %>';
window.addEventListener("keydown", function (event) {
        console.log('<%= SelectedRowIndex %>');
        var validArrowKey = false;

        if (event.keyCode == 40) {
            validArrowKey = true;
            index++;
        }
        else if (event.keyCode == 38 && index > -1) {
            validArrowKey = true;
            index--;
        }

        if (validArrowKey) {
            var trPaymentDetails = document.getElementById("trPaymentDetails_" + index.toString())

            if (trPaymentDetails) {
                __doPostBack('ctl00$MainContent$grdPaymentDetails', 'Select$' + index.toString());
            }
            else
                __doPostBack('ctl00$MainContent$grdPaymentDetails', 'Select$' + index.toString());

        }
    }, false);
Sign up to request clarification or add additional context in comments.

2 Comments

ok it works but why is that when I get the new total rows in the Gridview still returns 0 even if it has rows in it? I put it above the function as well " var totalRows = '<%= grdPaymentDetails.Rows.Count %>'; "
That's tricky to answer without having all of the code, both server side and client side. You could instead replace '<%= grdPaymentDetails.Rows.Count %>' with the Count property of the grids "datasource"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.