views:

24

answers:

1

I have a HTML table which I add items to from javascript on an ASP.NET callback when a button is clicked.

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();
    }
}

This callback system works fine but it's not quite what I wanted.

I have a C# function to calculate a set of results based on a given input number. This can take a long time so I want to run it on a thread, and update the table whenever a new result is obtained.

But I can't figure out how to call the javascript from my C# thread... any ideas?

+1  A: 

You can´t send a notificacion from the server to the client when the thread finishes. The only way is to make the client to request periodically the server (with ajax or refreshing the page) to check if the result of the thread is available.

Alejandro Martin
Thanks, I decided to use AJAX in the end. Seems like a big failing in ASP.NET...
demoncodemonkey