简体   繁体   English

从ASP.NET代码背后调用JS函数

[英]Calling JS function from ASP.NET codebehind

I have two javascript functions in my aspx page. 我的aspx页面中有两个javascript函数。 They use some fabric.js functions. 他们使用一些fabric.js函数。

function saveCanvas() {
    js = JSON.stringify(canvas.toDatalessJSON());
    $get('<%= txtJSON.ClientID%>').value = js;

}
function loadCanvas() {
    js = $get('<%= txtJSON.ClientID%>').value;
    canvas.clear();
    canvas.loadFromDatalessJSON(js);
    canvas.renderAll();
}

And in the codebehind: 在后面的代码中:

 Protected Sub SaveJSON()
    Dim scriptKey As String = "123"
    Dim javaScript As String = "<script type='text/javascript'>saveCanvas();</script>"
    ClientScript.RegisterStartupScript(Me.GetType(), scriptKey, javaScript)
 End Sub

Protected Sub LoadJSON()
    Dim scriptKey As String = "456"
    Dim javaScript As String = "<script type='text/javascript'>loadCanvas();</script>"
    ClientScript.RegisterStartupScript(Me.GetType(), scriptKey, javaScript)
End Sub

Now my question: Why does loadCanvas work while saveCanvas does not? 现在我的问题是:为什么loadCanvas起作用而saveCanvas却不起作用? txtJSON is not populated with the JSON-string. txtJSON未填充JSON字符串。 Calling the saveCanvas function from the aspx page works fine. 从aspx页面调用saveCanvas函数可以正常工作。

The problem is that you call saveCanvas after the postback is done and at that point the canvas data is long gone. 问题是回发完成后您调用saveCanvas ,并且此时画布数据已经消失了。

If you have a button "Save" then you need to call saveCanvas when it is clicked so that the data is saved before the browser posts the page back to the server: 如果具有“保存”按钮,则需要在单击它时调用saveCanvas ,以便在浏览器将页面发回到服务器之前保存数据:

<asp:Button runat="server" Text="Save" OnClickClick="saveCanvas()" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM