如何将值传递给来自asp.net代码的Javascript [英] How to pass a Value to Javascript From asp.net Code

查看:42
本文介绍了如何将值传递给来自asp.net代码的Javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Javascript如下

I have a Javascript as Follows

function onLoad() {
var options = {
sourceLanguage: ['en'],
destinationLanguage: ['kn'],
}

如何从asp.net中的页面加载事件编码传递目标语言的值。

How to pass the value for Destination Language from coding on Page Load event in asp.net.

推荐答案

几种方式:



1:你可以通过服务电话使用ajax或其他不回发页面的网络电话来完成。

2:你可以向表单添加隐藏字段。从JS填充然后从帖子中读取它

3:你可以将它作为部分帖子发布回来(有几种方法可以做到这一点,但主要是调用__postback(sometarget,yourVar);)



这涵盖了大部分方式。最常见的是隐藏字段方法,因为大多数人在回发(或button.click)事件中使用该值。



编辑:



实际上 - 因为你的对象更复杂,只是一个字符串,你可以使用JSON样式stringify。在真正使用JSON字符串之前,你需要将它解析为一个对象,尽管
Several ways:

1: You can do it via a service call using ajax or some other web call that does not post back the page.
2: You can add a hidden field to the form. Populate that from JS then read it from the post
3: You can post it as a partial post back (there are several ways to do this, but mainly calling __postback(sometarget,yourVar); )

That covers most of the ways. The most common is the hidden field method as most people use the value in the postback (or button.click) event.



Actually - as your object is more complex that just a string, you could use a JSON style stringify. You would need to parse it to an object from a JSON string before you could really use it, though


一旦这样做就会使用隐藏字段 - 将JavaScript变量值传递给服务器使用C#和VB.Net的ASP.Net中的Side(代码隐藏) [ ^ ]。
Once way to do this would be using a hidden field - Pass JavaScript variable value to Server Side (Code Behind) in ASP.Net using C# and VB.Net[^].


As您的.net page_load首先运行并生成html以发送到浏览器,因此page_load在您的javascript开始之前已经完成,因此您只能在页面处理后将该数据传递给您的服务器代码。



最适合您可能想做的解决方案是调用Web服务,或使用页面方法传递数据到服务器方法,从那里你可能会将它存储在会话中,后续请求可以访问它。
As your .net page_load runs first and generates the html to send to the browser, so the page_load has finished before your javascript begins so you can only pass that data to your server-code after the page has processed.

The solution that best fits what it is you're probably wanting to do is to call a web service, or use "page methods" to pass the data to a server method and from there you'll probably store it in the session to that subsequent requests have access to it.


这篇关于如何将值传递给来自asp.net代码的Javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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