简体   繁体   English

从代码传递的变量到javascript不更新

[英]Passed variable from code behind to javascript not updating

I need to pass the newly selected row index of the gridview to my javascript in aspx page. 我需要将gridview的新选择的行索引传递给我的aspx页面中的javascript。 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 我后面的代码中的变量是“ SelectedRowIndex”,它是一个整数

  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. 要为选定的行索引设置初始值,用户必须首先在gridview中单击一行,在该inclick触发器内部,将变量设置为用户的选定行。

Make the index variable global by moving it outside the function, then it will only be set once. 通过将index变量移到函数外部,使index变量变为全局变量,然后将其仅设置一次。

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM