我有一个HTML表,单击按钮时,在ASP.NET回调的javascript中将项目添加到该表中。
HTML:
<%@ Page Language="C#" CodeFile="Hello5.aspx.cs" Inherits="ClientCallback" %>
<html>
<head>
<script>
function AddResult()
{
GetResults("blah", "");
}
function UpdateTable(itemText)
{
var mytable = document.getElementById("mytable");
var rowID = mytable.rows.length;
var newrow = mytable.insertRow(rowID);
newrow.insertCell(0).appendChild(document.createTextNode(rowID));
newrow.insertCell(1).appendChild(document.createTextNode(itemText));
}
</script>
</head>
<body>
<form id="form1" runat="server" />
<input type="button" onclick="AddResult()" value="Add Result" />
<table border="1" id="mytable">
<th colspan="2">Results</th>
</table>
</body>
</html>
Codebehind:
public partial class ClientCallback : System.Web.UI.Page, System.Web.UI.ICallbackEventHandler
{
string _inputVal;
protected void Page_Load(object sender, EventArgs e)
{
string cbReference = Page.ClientScript.GetCallbackEventReference(this, "arg", "UpdateTable", "context");
string callbackScript = "function GetResults(arg, context)" + "{ " + cbReference + "} ;";
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "GetResults", callbackScript, true);
}
void ICallbackEventHandler.RaiseCallbackEvent(string eventArgument)
{
_inputVal = eventArgument;
}
string ICallbackEventHandler.GetCallbackResult()
{
return _inputVal + " " + DateTime.Now.ToString();
}
}
这个回拨系统工作正常,但我不太想这样。
我有一个C函数来计算一组基于给定输入数字的结果。这可能需要很长时间,所以我希望在线程上运行它,并在获得新结果时更新表。
但我不知道如何从我的C线程调用javascript…有什么想法吗?