0

I need to call a javascript function from aspx page (C#). This is shor function is located in aspx file, and it draws a few figures. The function is invoked by button press. The parameters of figures are taken as some indexes from mssql database. The javascript code is invoked by the next command:

ClientScriptManager cs = Page.ClientScript;
cs.RegisterStartupScript(this.GetType(), "myScript", "<script language='javascript'>htm("+param+")</script>");

The problem is that if I put javascript function htm in the section of aspx page, then I get a "htm function is not defined", in case if I put javascript at the end of the html code, I get error with getting canvas context (null object), because the canvas hasn't been loaded yet. The partial solution of the issue is assigning the javascript function to window.onload, but it is not the solution I need, because the figure must be drawn ONLY ONCE after button press.

4

1 回答 1

0

如果可以在服务器端代码中定义 OnClientClick,则可以将其与服务器端数据一起使用:

myButton.OnClientClick = "htm(" + param + "); return false";

单击此按钮时-它将导致纯客户端单击而没有任何回发。

更新如果您确实需要为服务器端调用它,则需要等到htm定义函数,您可以像这样使用超时:

cs.RegisterStartupScript(this.GetType(), "myScript", "(function htmCaller() { if (typeof (htm) == 'undefined') { setTimeout(htmCaller, 500) } else { htm("+param+") })(); }", true);
于 2013-09-06T15:39:25.997 回答