传递变量值从C#的JavaScript [英] passing variable values from c# to javascript

查看:100
本文介绍了传递变量值从C#的JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,对不起,把已经回答了很多次的问题,但我已经试过最提到,目前仍是程序不能得到它的工作。

First of all, sorry for bringing a question that has been answered so many times, but I have tried most of the procedures mentioned and still can't get it working.

我有一个ASP.NET应用程序(服务器端),我想使用的WebGL(JavaScript的,客户端),以可视化的一些结果。我能够创造aspx文件画布上,并通过在JScript文件编写所有必要的JS code绘制3D对象(立方体)(只要我知道我要提前画什么,写作所有在JScript中)。然而,我的本意是写这取决于我的服务器端code不同的对象。当我点击一个按钮,可视化,回传被触发。在服务器上的某些code被执行,而有些坐标在返回数组中,aspx.cs文件中定义的全局。我需要通过节点的数组从服务器到JS文件,以实际绘制我想要的对象的坐标。

I have an ASP.NET app (server-side), and I would like to visualize some results by using WebGL (javascript, client-side). I am able to create the canvas in aspx file and to draw a 3D object (a cube) by writing ALL the necessary JS code in a jscript file (As long as I know what I want to draw in advance, and writing all that in Jscript). However, my intention is to write a different object depending on my server-side code. When I click a visualize button, a postback is triggered. In the server some code is executed, and some coordinates are returned in an array, defined global in the aspx.cs file. I would need to pass that array of node coordinates from the server to the JS file in order to draw actually the object I want.

下面基本上是我曾尝试:

Here is basically what I have tried:

-The最简单的方法我已经找到传递变量是aspx.cs申报财产公开(C#code),然后在JS写:
     VAR clientVariable ='<%= ServerVariable%GT;';

-The easiest way I have found to pass a variable is to declare a property public in aspx.cs (c# code) and then in JS writing: var clientVariable = '<%=ServerVariable%>';

我已经试过这不是一个数组,但只是一个字符串变量,如果写:
    警报(clientVariable);
我看&LT;%= ServerVariable%>窗口,而不是实际值。我不知道我是否需要任何额外的库或内容。如果我不能连得工作这个简单的例子,我不知道我怎么会连一个双数组做到这一点。 I'm使用MCVS08,ASP.NET 3.5 HTML5。

I have tried this not with an array, but just with a string variable and if writing: alert(clientVariable); I see "<%=ServerVariable%>" in the window, NOT the actual value. I don´t know if I need any additional library or what. If I can't even get this easy example working, I don't know how I would even do it with a double array. I´m using MCVS08, ASP.NET 3.5 with HTML5.

-Apart从,我曾尝试到数组转换不是JSON,而是:
      Page.ClientScript.RegisterArrayDeclaration();

-Apart from that, I have tried to convert the array not with JSON, but with: Page.ClientScript.RegisterArrayDeclaration();

-I've使用 ClientScript.RegisterStartupScript(的GetType(),警告,测试2('+ A +');,真);

-I've试图用一个隐藏的块存储会话值等。

-I've tried to use a hidden block to store the session value, etc.

所以基本上,总结,我想有:

So basically, summing Up, I would like to have:

-server侧:在服务器中执行后面的code的功能后,呈现页面时再次我将在aspx.cs一个全程变量,它是一个双[,]坐标,含有的坐标节点将定义3D对象。

-server-side: after executing a function of the code behind in the server, when rendering the page again I will have a global variable in aspx.cs which is a double[,] coordinates, containing the coordinates of nodes that will define the 3D object.

-aspx:在HTML(而不是asp.net)我有我打算有可视化canvas标签。将呈现图像的客户端脚本存储在JScript文件。在这个文件中,在函数内部我有一个变量var顶点= [];我需要用我在坐标阵列的服务器得到的值来养活。我不知道如何实现这一目标的最佳途径。你会推荐使用AJAX?

-aspx: in html (not asp.net) I have a canvas tag in which I intend to have the visualization. The script that will render the image client-side is stored in a JScript file. In this file, inside a function I have a variable var vertices = []; which I need to feed with the values I got from the server in coordinates array. I don't know how to achieve this the best way. Would you recommend to use AJAX?

任何建议/意见将是非常美联社preciated。作为一个简单的字符串虚拟实例不工作(忘掉画布,WebGL和所有的东西),可能我需要别的还是我误解如何正确地做到这一点?谢谢

Any suggestion/comment would be very appreciated. As the dummy example with a simple string is not working (forgetting about canvas, webGL and all that stuff), may I need anything else or am I misunderstanding how to do it properly?Thanks

推荐答案

当我需要传递变量转换成JavaScript我通常我preFER VAR clientVariable ='&LT;%= ServerVariable%GT; '; 解决方案。这种方法是足够的少数标变量。如果你需要传递一个复杂的对象或数组,可以考虑使用 JavaScriptSerizlizer

When I need to pass variables into JavaScript I usually I prefer var clientVariable = '<%=ServerVariable%>'; solution. This method is sufficient for small number of scalar variables. If you need to pass a complex object or a array, consider to use JavaScriptSerizlizer.

您有可能会发生的原因,一些行为。他们中的一个可能发生,如果已经包含一个scriptlet到的.js 文件,不进的.aspx 文件。

The behavior you are having it might happen for number of reasons. One of them might occur, if you have included a scriptlet into .js file, and not into .aspx file.

下面是我会怎么做:

WebGL的汲取file.js:

webgl-draw-file.js:

window.WebGlDraw = function(points /* point array */)
{
  // Draw points here
}

Page1.aspx.cs:

Page1.aspx.cs:

public string GetSerializedServerVariable()
{
    new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(ServerVariable);    
}

Page1.aspx的:

Page1.aspx:

<html>
<header>
    <script src="webgl-draw-file.js"/>
    <script type=text/javascript>
        window.WebGlDraw(<%=this.GetSerializedServerVariable()%>);
    </script>
</header>
<body>
...
</body>
</html>

要了解什么样的价值观传递给JS功能更好,看看使用浏览器的网页源代码。您应该看到您的阵列,而不是℃的JSON重新presentation;%= Page.GetSerializedServerVariable()%方式&gt; 小脚本

To understand a better what values are passed to the JS function, take a look at a page source using your browser. You should see a JSON representation of your array instead of <%=Page.GetSerializedServerVariable()%> scriptlet.

它应该是这个样子:

<html>
<header>
    <script src="webgl-draw-file.js"/>
    <script type=text/javascript>
        window.WebGlDraw([{x:1, y:2}, {x:1, y:2}, {x:1, y:2}, {x:1, y:2}]);
    </script>
</header>
<body>
...
</body>
</html>

这篇关于传递变量值从C#的JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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