将JavaScript变量分配给C#变量(ASP.net) [英] Assign javascript variable to C# variable (ASP.net)

查看:109
本文介绍了将JavaScript变量分配给C#变量(ASP.net)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 < script>我知道我可以将C#变量赋值给js。 
var x =<%= CsharpVar%> ;
< / script>

即可。



选择做相反的事情?我的意思是这样的(它不适合我):

 < script> 
var x =Exmp;
<%CsharpVar =%> = x;
< / script>

谢谢!

解决方案

这是不可能的,因为你必须考虑如何在asp.net中生成页面。



服务器生成页面,然后将其推送到客户端。因此,它可以将c#变量添加到页面中并执行其他操作,但一旦它被推入,它就不能再进行交互。



从javascript获取数据的唯一方法代码到C#就是发送给服务器。



例如,如果用户填写表单上的文本框,他们必须按提交,所以它是在服务器知道它之前推送到服务器。它们完全断开连接,并且页面和服务器之间唯一可以发生的通信是当它特别发送一个请求时(例如提交表单)。

当一个表单或类似的提交,页面将发送服务器像'加载更新配置文件页面,我放在文本框中的名称是dave',那么服务器可以找出如何处理它收到的数据,例如。将人的姓名更新为'dave'。



如果您想更具动态性,您可以使用ajax更新内容而无需刷新整个页面。当你更新设置时,许多网站都会这样做,例如。你点击一个复选框来执行一些操作,它会执行一个ajax请求(基本上和上面的例子一样,但它在后台安静地发生)。

无论哪种方式需要花一点时间了解asp.net如何在底层工作,我认为要正确理解发生了什么以及为什么你不能做你想做的事情。最简单的想法是,当服务器显示你的页面并将它发送给你时,它会将它放入一个盒子中,并将它传送到你的房子。如果你对它做任何事情,那么服务器不会有任何线索,或者能够将任何东西放入C#变量等,除非你做了所有的更改,并将它恢复到原来的状态。


I know that I can assign C# variable to js like that:

<script >
var x = <%=CsharpVar%> ;
</script>

that's work.

but there is option to do the opposite? I mean something like that (It's not working for me):

<script >
var x= "Exmp";
<%CsharpVar=%>=x ;
</script>

Thank you!

解决方案

This is not possible, because you have to consider how pages are generated in asp.net

The server generates the page, then pushes it to the client. Therefore it can add c# variables into the page and do other stuff, but once it's pushed, it can no longer interact.

The only way to get data back from the javascript code into the C# is to send it to the server.

So for example, if a user fills in a textbox on a form, they have to press submit so it is pushed to the server before the server knows anything about it. They are completely disconnected, and the only communication that can occur between the page and the server is when it specifically sends a request (eg. submitting a form).

When a form or similar is submitted, the page will send the server something like 'load the update profile page, and the name i put in the textbox is dave' then the server can figure out what to do with the data it received, eg. update the person's name to 'dave'.

If you want to be more dynamic, you can use ajax to update something without refreshing the whole page. Many websites do this when you're updating settings, eg. you click a checkbox to do something, it will perform an ajax request (basically the same as the above example, but it happens quietly in the background).

Either way you need to spend a bit of time understanding how asp.net works under the hood I think to properly understand what's going on and why you can't do what you're trying to do. The easiest way to think of it is that when the server displays your page and sends it to you, it puts it into a box and fedex's it to your house. If you do anything with it then the server isn't going to have a clue, or be able to put anything into C# variables etc. unless you make all your changes, and fedex it back.

这篇关于将JavaScript变量分配给C#变量(ASP.net)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆