简体   繁体   中英

How we pass variable to c# method from JavaScript

I want to pass variable to C# method from JavaScript is it possible ?

I had tried below code code:=

<script type="text/javascript">
    $(document).ready(function () {
        debugger;
        var Query = window.location.search;
        var i = "<%=QueryStringModule.Decrypt(Query)%>"
    });
</script>

but i am getting the name "Query" doesn't exist in the current context

Please help me in the same

This is not possible. C# is code behind, so runs on the server. Any input is generated before the page is build on screen. So passing values from Javascript to C# is the other way around. Look into ajax to send values to the server so they can be parsed.

You cannot pass JavaScript variables to the server side code in that way. JavaScript is client side, which means that the server side code has already run before JavaScript starts executing. To pass a variable back to the server, you will need to submit the variable to the server. You can use an AJAX request to do this without having to load the page again.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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