Friday, 10 June 2011

Classical Callback Through JQuery


Subscription.aspx
<script src="Scripts/JQuery/Jquery.js" language="javascript" type="text/javascript"></script>
<script src="Scripts/Subscription.js" language="javascript" type="text/javascript"></script>
<input id="btnGetDetails" type="button" value="Get Details" class="Button" style="width:75px;" onclick="GetSubscriptionDetails();" />
<input id="btnGo" type="button" value="Add" class="Button" onclick="ManipulateSubscription()" />


/Scripts/Subscription.Js
var SubscriptionCallBackPageName = "CallBacks/SubscriptionCallBack.aspx";
var ParamDelimiter = "[****]";
var ValueSeparator = "[,]";
var InputParam = “”;

function ManipulateSubscription()
{   
    var SelectedAction = "Add";   //"Edit"
    try
    {      
        InputParam  = SelectedAction;
        InputParam  += ParamDelimiter + $("#txtRegistryURNCtlId").val();
        ---
        ---
             
        var htmlResp = $.ajax({
            type: "GET",
            url: SubscriptionCallBackPageName,
            data: "callback=1&param1=" + InputParam,                  
            async:false,                         
            success: function(data){                             
                try
                {                      
                    ---
                    ---                
                }
                catch(ex){                   
                    alert("message:" + ex.message);
                }
            },
            error:function(){               
                alert("Error occured");
            },
            cache: false
        });  
                 
    }
    catch(ex){
    }   
}

function GetSubscriptionDetails()
{  
    try
    {                  
        InputParam  = $("txtRegistryURNCtlId").val();
       
        var htmlResp = $.ajax({
            type: "GET",
            url: SubscriptionCallBackPageName,
            data: "callback=2&param1=" + InputParam,                  
            async:false,                         
            success: function(data){                             
                try
                {  
                    ---
                    ---                
                }
                catch(ex){                  
                    alert("message:" + ex.message);
                }
            },
            error:function(){              
                alert("Error occured");
            },
            cache: false
        });  
    
    }
    catch(ex){       
    }   
}



/CallBacks/SubscriptionCallBack.aspx.cs

private enum CallBackType
{
    ManipulateSubscription = 1,
    GetSubscriptionDetails = 2
}

private void HandleCallBack()
{
    string RetVal = string.Empty;
    try
    {
        CallBackType _Callback;
        string RequestParam1 = string.Empty;

        if (Request.Params.Count > 0)
        {
            _Callback = (CallBackType)int.Parse(Request.Params[“callback”].ToString());
            RequestParam1 = Request.Params[“param1”];               

            switch (_Callback)
            {
                case CallBackType.ManipulateSubscription:
                    RetVal = this.ManipulateSubscription(RequestParam1);
                    break;
                case CallBackType.GetSubscriptionDetails:
                    RetVal = this.GetSubscriptionDetails(RequestParam1);
                    break;
                default:
                    break;
            }
        }
    }
    catch (Exception ex)
    {            
    }
    finally
    {
        Response.Write(RetVal);
    }
}

private string ManipulateSubscription(string requestParam)
{
    string RetVal = string.Empty;

    string[] Params = this.SplitString(requestParam, "[****]");

    string Action = string.Empty;
    ---
    ---
   

    try
    {
        Action = Params[0];
        ---
        ---
       
    }
    catch (Exception)
    {
    }

    return RetVal;
}

private string GetSubscriptionDetails(string requestParam)
{
    string RetVal = string.Empty;

    string[] Params = this.SplitString(requestParam, "[****]");

    string RegistryURN = string.Empty;
   

    try
    {
        RegistryURN = Params[0];
        ---
        ---
       
    }
    catch (Exception)
    {
    }
   

    return RetVal;
}


private static string[] SplitString(string valueString, string delimiter)
{
        string[] RetVal;
        int Index = 0;
        string Value;
        List<string> SplittedList = new List<string>();

        while (true)
        {
            Index = valueString.IndexOf(delimiter);
            if (Index == -1)
            {
                if (!string.IsNullOrEmpty(valueString))
                {
                    SplittedList.Add(valueString);
                }
                break;
            }
            else
            {
                Value = valueString.Substring(0, Index);
                valueString = valueString.Substring(Index + delimiter.Length);
                SplittedList.Add(Value);
               
            }

        }

        RetVal = SplittedList.ToArray();

        return RetVal;
}


protected void Page_Load(object sender, EventArgs e)
{
        this.HandleCallBack();
}

No comments:

Post a Comment