Friday, 24 June 2011

Classical Callback Through JavaScript

User.js
var httpRequest;
var requestFor;
var XMLDoc;
var xmlVersions = new Array("Msxml2.XMLHTTP.7.0","Msxml2.XMLHTTP.6.0","Msxml2.XMLHTTP.5.0","MSXML2.XMLHTTP.4.0","MSXML2.XMLHTTP.3.0","MSXML2.XMLHTTP","Microsoft.XMLHTTP");
var InputParam = "";
var ParamDelimiter = "[****]";


function getXMLHTTPRequest()
{
    if(window.XMLHttpRequest)
    {
        return new window.XMLHttpRequest();
    }
    else
    {
        for(i=0;i<xmlVersions.length;i++)
        {
            try
            {
                return new ActiveXObject(xmlVersions[i]);
            }
            catch(ex)
            {
            }
        }
    }
}

function UserCallBack(callfor,param)
{
    httpRequest = getXMLHTTPRequest()
    httpRequest.onreadystatechange = UserLoadValue; 

    if (window.ActiveXObject)
    {
        httpRequest.open("POST","Callbacks/UserCallBack.aspx",true)
    }
    else if(window.XMLHttpRequest)
    {
        httpRequest.open("GET","Callbacks/UserCallBack.aspx",true)
    } 

    if(httpRequest)
    {
        if (param)
        {
            if(param==""){
                param="DNT"
            }
        }
        else
        {
            param = "DNT"
        }          

        requestFor = callfor;
        httpRequest.setRequestHeader("callback", callfor);
        param=encodeURIComponent(param);
        httpRequest.send(param);
    }
}


function UserLoadValue()
{
    if(httpRequest.readyState == 4 && httpRequest.status == 200)
    {
        if(requestFor == "1") //CreateNewUser
        {
            if(httpRequest.responseText=='-1')
            {
                ----

                ----      
            }
        }  
    }     
}

function SaveUser()
{    
    InputParam  = $$(txtLoginIdCtdId,true).value;
    InputParam  += ParamDelimiter + $$(txtFirstNameCtlId,true).value;
    InputParam  += ParamDelimiter + $$(txtLastNameCtlId,true).value;
    InputParam  += ParamDelimiter + $$(txtPasswordCtlId,true).value;
    InputParam  += ParamDelimiter + $$(txtEMailCtlId,true).value;
    InputParam  += ParamDelimiter + $$(ddlUserRoleCtlId,true).value;
    UserCallBack(1, InputParam);            

    return false;
}

-----------

CallBacks/UserCallBack.aspx.cs
private enum CallBackType
{
    CreateNewUser = 1,
    ---
    ---
}

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


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

        _Callback = (CallBackType)int.Parse(Request.Headers[DIMonitoringConstants.RequestHeaderParamNames.CallBack].ToString());

        Reader = new StreamReader(Request.InputStream);

       string[] Param = HttpUtility.UrlDecode(Reader.ReadToEnd()).Split(new String[] { DIMonitoringConstants.Delimiters.ParamDelimiter }, StringSplitOptions.None);

        switch (_Callback)
        {
                    case CallBackType.CreateNewUser:
                        RetVal = this.CreateNewUser(Param);
                        break;
                    ----
                    ----                       
                    default:
                        break;
        }

        Response.Write(RetVal);
    }
    catch (Exception ex)
    {
     
    }
}

private string CreateNewUser(string[] arrParam)
{   
    string RetVal = "-1";

    try
    {
        ---
        ---
    }
    catch (Exception)
    {
    }

    return RetVal;
}

No comments:

Post a Comment