Click here to Skip to main content
11,636,889 members (76,640 online)
Click here to Skip to main content
Articles » Web Development » Ajax » Controls » Downloads
Add your own
alternative version

An Easy Way to Implement ASP.NET Callback

, 3 Sep 2010 CPOL 21.6K 529 16
Implementing callback inside User Control, choosing between client callback and Ajax framework
My Project

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


        End If


    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


            '===========  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


            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.


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


About the Author

Mohammad A Gharakhanlou
Software Developer
Iran (Islamic Republic Of) Iran (Islamic Republic Of)
No Biography provided

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150728.1 | Last Updated 3 Sep 2010
Article Copyright 2010 by Mohammad A Gharakhanlou
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid