Friday, March 16, 2012

Callback in .Net

        First create a javascript function with name EndGetData(), You can use any name.
        this function will get executed after successfun call back.

        function EndGetData(arg) {          
                    alert(arg);         
                }

        Then Inherite your page by "ICallbackEventHandler" like
        public partial class _Default : System.Web.UI.Page ,ICallbackEventHandler
         {

        Now use the following code in the page load method of the page.

        String strfunction = Page.ClientScript.GetCallbackEventReference(this, "arg", "EndGetData", "context");
        String strcallbackScript;
        strcallbackScript = "function CallServer(arg, context)" +
        "{ " + strfunction + ";}";
        Page.ClientScript.RegisterClientScriptBlock(this.GetType(),
        "CallServer", strcallbackScript, true);

        Here we have used two function in the client script(You can use any name for function.)
        1. EndGetdata - Which will called after call back.
        2. CallServer - Which will called in the begining of the callback.

        Now call the CallServer() function on click of the button from the .aspx page.
        <input type="button" onclick="CallServer('Passing_Parameter',"");" value="Click Me" />

        string globalVariable;
        public void RaiseCallbackEvent(String Passing_Parameter)
            {
                //Code for callback function....
                //And store the output in the global variable.
            }
            public String GetCallbackResult()
            {
                //Do after callback result
                //Return the global variable.
                return globalVariable
            }

            If you have any query then feel free to comment.
           

No comments:

Post a Comment