如何从代码后面调用这个javascript函数

时间:2022-01-04 15:00:03

I'm putting javascript created controls (http://www.dariancabot.com/projects/jgauge_wip/) on my page.

我在我的页面上放了javascript创建的控件(http://www.dariancabot.com/projects/jgauge_wip/)。

<div class="jgauge" ></div>

$(document).ready(function () {  
    var ctl; 
    ctl = new jGauge();   
    ctl.init();
)}

Let's say I need to pass few parameters to init. like... ctl.init(a, b); from code behind, how can I achieve that? I tried something like this...

假设我需要将几个参数传递给init。像... ctl.init(a,b);从代码背后,我怎样才能实现呢?我试过这样的事......

string script2 = String.Format("init('{0}','{1}')", param1, param2);

this.Page.ClientScript.RegisterStartupScript(this.GetType(), "initialize control", script2, true);

But it's not working. I got:

但它不起作用。我有:

ReferenceError: init is not defined

2 个解决方案

#1


2  

The init is a method inside jGauge instance. So you must instantiate before you call those mathode. Try bellow:

init是jGauge实例中的一个方法。所以你必须在调用那些mathode之前进行实例化。试试吼叫:

string script2 = String.Format("var ctl; ctl = new jGauge();  ctl.init('{0}','{1}');", param1, param2);
this.Page.ClientScript.RegisterStartupScript(this.GetType(), "initialize control", script2, true);

#2


0  

one more way of doing it is to have public variables,

另一种方法是使用公共变量,

your script would be like:

你的脚本就像:

$(document).ready(function () {  
    var ctl; 
    ctl = new jGauge();   
    ctl.init(<%=param1 %>, <%=param2 %>);
)}

and in code behind have two public strings

并且在代码后面有两个公共字符串

public string param1 = string.Empty;
public string param2 = string.Empty;

on your page_load or any other event, you can set value of these 2 variables normally, like:

在page_load或任何其他事件中,您可以正常设置这两个变量的值,例如:

param1 = "100";

rest of the code will work as it is.

其余代码将按原样运行。

#1


2  

The init is a method inside jGauge instance. So you must instantiate before you call those mathode. Try bellow:

init是jGauge实例中的一个方法。所以你必须在调用那些mathode之前进行实例化。试试吼叫:

string script2 = String.Format("var ctl; ctl = new jGauge();  ctl.init('{0}','{1}');", param1, param2);
this.Page.ClientScript.RegisterStartupScript(this.GetType(), "initialize control", script2, true);

#2


0  

one more way of doing it is to have public variables,

另一种方法是使用公共变量,

your script would be like:

你的脚本就像:

$(document).ready(function () {  
    var ctl; 
    ctl = new jGauge();   
    ctl.init(<%=param1 %>, <%=param2 %>);
)}

and in code behind have two public strings

并且在代码后面有两个公共字符串

public string param1 = string.Empty;
public string param2 = string.Empty;

on your page_load or any other event, you can set value of these 2 variables normally, like:

在page_load或任何其他事件中,您可以正常设置这两个变量的值,例如:

param1 = "100";

rest of the code will work as it is.

其余代码将按原样运行。