Click here to Skip to main content
15,886,799 members
Articles / Web Development / ASP.NET

An Easy Way to Implement ASP.NET Callback

Rate me:
Please Sign up or sign in to vote.
4.31/5 (5 votes)
3 Sep 2010CPOL5 min read 44.1K   689   17  
Implementing callback inside User Control, choosing between client callback and Ajax framework

Partial Class CallBackHandler
    Inherits System.Web.UI.UserControl
    Implements ICallbackEventHandler

#Region "Conts"

    Private Const C_CallbackHandlerNumberSpliter As String = "###"
    Private Const C_CallbackArgsSpliter As String = "$$"
    Private Const C_CallbackResultSpliter As String = "$$"

#End Region

#Region "Declarations"

    Dim returnValue As String = String.Empty

#End Region

#Region "Events And Delegates"

    Public Delegate Sub RaiseCallbackEventHandler(ByVal eventArgs() As String, ByVal HandlerNumber As Integer, ByRef CallBackResult As String, ByRef ExcutableClientScript As String, ByVal RaiseException As Exception)

    Public Event RaiseCustomCallback As RaiseCallbackEventHandler


#End Region

#Region "Page Load"

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Not Me.IsPostBack Then

            InitJustOnFirstLoad()

        End If

        InitOnEveryPostBack()

    End Sub

#End Region

#Region "Callback  Events"

    Public Function GetCallbackResult() As String Implements System.Web.UI.ICallbackEventHandler.GetCallbackResult

        Return returnValue

    End Function

    Public Sub RaiseCallbackEvent(ByVal eventArgument As String) Implements System.Web.UI.ICallbackEventHandler.RaiseCallbackEvent

        '===============  declarations  ==================
        Dim tempArgs As String(), HandlerNo As Integer, RealArgs As String() = Nothing

        Dim ExcutableClientScript As String = String.Empty, CallbakcResult As String = String.Empty

        Dim RaisedException As Exception = Nothing


        Try

            '===========  Extracting Arguments ==========================
            'sample  eventArgument with 3 parameter  :-->   param1$$param2$$param3###handlerno:1
            'there are 2 part which splited by ### . part 1 is parameters and part 2 is handler number


            tempArgs = eventArgument.Replace(C_CallbackHandlerNumberSpliter, "~").Split("~")

            HandlerNo = tempArgs(1).Split(":")(1)

            ' removing extra spliters
            If tempArgs(0).StartsWith(C_CallbackArgsSpliter) Then tempArgs(0) = tempArgs(0).Remove(0, C_CallbackArgsSpliter.Length)
            If tempArgs(0).EndsWith(C_CallbackArgsSpliter) Then tempArgs(0) = tempArgs(0).Remove(tempArgs(0).Length - C_CallbackArgsSpliter.Length, C_CallbackArgsSpliter.Length)

            'extracting parameters which send from client
            RealArgs = tempArgs(0).Replace(C_CallbackArgsSpliter, "~").Split("~")


        Catch ex As Exception
            RaisedException = ex
        End Try

        Try

            RaiseEvent RaiseCustomCallback(RealArgs, HandlerNo, CallbakcResult, ExcutableClientScript, RaisedException)

            returnValue = String.Format("{0}{1}{2}", CallbakcResult, C_CallbackResultSpliter, ExcutableClientScript)

        Catch ex As Exception

            returnValue = String.Format("{0}{1}{2}", "unexpected error!!!", C_CallbackResultSpliter, "")

        End Try

    End Sub

#End Region

#Region "Init"

    Protected Sub InitJustOnFirstLoad()

        'to do ...

    End Sub

    Protected Sub InitOnEveryPostBack()

        Dim cm As ClientScriptManager = Page.ClientScript

        Dim cbReferenceGeneral As String = cm.GetCallbackEventReference(Me, "arg", "ReceiveCustomCallBackData", "context")

        Dim callbackScript1 As String = "function  AsynCallServer(arg, context) {" & cbReferenceGeneral & "; };"

        Dim callbackScript2 As String = "function  asyncall(args,handlernumber ,receiverdivid) {var _arg = args + '" + C_CallbackHandlerNumberSpliter + "' + 'handlerno:' + handlernumber ; AsynCallServer(_arg, receiverdivid); };"

        cm.RegisterClientScriptBlock(Me.GetType(), "AsynCallKey1", callbackScript1, True)

        cm.RegisterClientScriptBlock(Me.GetType(), "AsynCallKey2", callbackScript2, True)


    End Sub

#End Region

End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer asemantek.ir
Iran (Islamic Republic of) Iran (Islamic Republic of)
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions