locked
Help with Code - Asychronous process - how to update UI from callback method RRS feed

  • Question

  • User1269867951 posted

    Hi, Here is my code. I am trying to update UI (label control) from the callback method (ResultCallBack). However, nothing happens, that is, UI is not updated if I do something like label1.text=results. (Probably because page has already rendered????)???

    So, my question is, what techniques are available to update the UI from the callback method?

    Also, how to set session variable in callback method. Right now, if i do httpcontext.current.session("complete")=1 in the callback method, I get that sessionstate must be enabled in web.config or page level. However, session state is enabled - which I tested by assigning session variable in page_load handler.

    So, 2nd question, how to use session from callback method?

    Thanks

    Partial Class test2
        Inherits System.Web.UI.Page
      
    
        'Private Delegate Sub AppendAllTextDelegate(ByVal path As String, ByVal content As String)
        Private Delegate Sub AppendAllTextDelegate()
    
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            IO.File.AppendAllText("d:\log2.txt", ControlChars.CrLf & System.DateTime.Now() & "--" & "Page_Loaded on thread: " & Threading.Thread.CurrentThread.GetHashCode().ToString())
    
            Dim myDelegateInstance As New AppendAllTextDelegate(AddressOf AppendAllTextBackground)
            Dim myCallBackInstance As New AsyncCallback(AddressOf ResultCallBack)
            ' myDelegateInstance.BeginInvoke("d:\log2.txt", ControlChars.CrLf & System.DateTime.Now & "--" & "File Operation Complete on thread: " & Threading.Thread.CurrentThread.GetHashCode().ToString(), myCallBackInstance, myDelegateInstance)
            myDelegateInstance.BeginInvoke(myCallBackInstance, myDelegateInstance)
    
    
    
    
        End Sub
    
        'Private Sub AppendAllTextBackground(ByVal path As String, ByVal content As String)
        '    System.Threading.Thread.Sleep(10000)
        '    IO.File.AppendAllText(path, content)
    
        'End Sub
    
    
        Private Sub AppendAllTextBackground()
            System.Threading.Thread.Sleep(10000)
            IO.File.AppendAllText("d:\log2.txt", ControlChars.CrLf & System.DateTime.Now & "--" & "File Operation Complete on thread: " & Threading.Thread.CurrentThread.GetHashCode().ToString())
    
        End Sub
        Private Sub ResultCallBack(ByVal ar As IAsyncResult)
            Dim myDelegateInstance As AppendAllTextDelegate = CType(ar.AsyncState, AppendAllTextDelegate)
            myDelegateInstance.EndInvoke(ar)
    
    
        End Sub
    
    
       
    End Class
    


    Wednesday, December 23, 2009 10:27 PM

Answers

  • User78336405 posted

    Hi,

    In asp.net 2 making a client callback is a 4 step process:

    1. First of all you need to retrieve some data from the client side, which is accessed by using DOM and JavaScript.

    2. The data accessed in step 1 will be sent to the server side function which will process the data, for that you need a client side fucntion which will call that server side method asynchronously.

    3. The server method called asynchronously in step 2 will now process the data and then return the some result back to the client.

    4. The result produced in step 3 by the server and sent to the client will be used to update the client by using DOM and JavaScript.

    You could update UI (label control) from the callback method as that. And session as same.

    For more details, please refer:http://www.themastech.net/Tutorials/ClientCallbacks/Default.aspx

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, December 28, 2009 1:50 AM
  • User78336405 posted

    Hi,

    Beside that, you could use ajax to implement.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, December 29, 2009 9:17 PM

All replies

  • User78336405 posted

    Hi,

    In asp.net 2 making a client callback is a 4 step process:

    1. First of all you need to retrieve some data from the client side, which is accessed by using DOM and JavaScript.

    2. The data accessed in step 1 will be sent to the server side function which will process the data, for that you need a client side fucntion which will call that server side method asynchronously.

    3. The server method called asynchronously in step 2 will now process the data and then return the some result back to the client.

    4. The result produced in step 3 by the server and sent to the client will be used to update the client by using DOM and JavaScript.

    You could update UI (label control) from the callback method as that. And session as same.

    For more details, please refer:http://www.themastech.net/Tutorials/ClientCallbacks/Default.aspx

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, December 28, 2009 1:50 AM
  • User1269867951 posted

    Hi thanks,

    But how would i do it in the code shown in my first post

    I dont want to use icallbackeventhandler - which is what i think you are talking about.


    Monday, December 28, 2009 7:32 PM
  • User78336405 posted

    Hi,

    You could change your code and use icallbackeventhandler. That will be easy to do.

    Tuesday, December 29, 2009 12:45 AM
  • User1269867951 posted

    Thanks. So is there no other way besides icallbackeventhandler to achieve what i have asked for in my first post?

    thanks


    Tuesday, December 29, 2009 11:46 AM
  • User78336405 posted

    Hi,

    Beside that, you could use ajax to implement.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, December 29, 2009 9:17 PM