Wednesday, 7 June 2017

Call Other Page Using Ajax API

Call Other Page Using Ajax API



Let's Example :- Call other page on current page

First Page :- CallAjaxPage.aspx
Second Page :- AjaxExample.aspx

I want to call AjaxExample page on particular div of CallAjaxPage

Code of CallAjaxPage



<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="CallAjaxPage.aspx.cs" Inherits="VIJAYRNDWebApp.CallAjaxPage" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="Scripts/AjaxAPI.js"></script>
    <script>
        function callAjaxpaGE() {

            var url = "AjaxExample.aspx";

              //Pass flag,divId,pageAddress
            fnCallAjaxAPI(1, 'otherPageLoad', url);

        }

    </script>
</head>
<body>
    <form id="form1" runat="server">
        <input type="button" value="call" onclick="callAjaxpaGE()" />
    <div id="otherPageLoad" onload="callAjaxpaGE();">
    
    </div>
    </form>
</body>
</html>

Code of AjaxExample


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AjaxExample.aspx.cs" Inherits="VIJAYRNDWebApp.AjaxExample" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <h3>Welcome on AJAX PAGE </h3>
    </div>
    </form>
</body>
</html>



USING AJAX API
-----------------------------------------------------------------------------------
//Function XHConn
function XHConn() {
    var xmlhttp, bComplete = false;
    try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); }
    catch (e) {
        try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
        catch (e) {
            try { xmlhttp = new XMLHttpRequest(); }
            catch (e) { xmlhttp = false; }
        }
    }
    if (!xmlhttp) return null;
    this.connect = function (sURL, sMethod, sVars, fnDone) {
        if (!xmlhttp) return false;
        bComplete = false;
        sMethod = sMethod.toUpperCase();
        try {
            if (sMethod == "GET") {
                xmlhttp.open(sMethod, sURL + "?" + sVars, true);
                sVars = "";
            }
            else {
                xmlhttp.open(sMethod, sURL, true);
                xmlhttp.setRequestHeader("Method", "POST " + sURL + " HTTP/1.1");
                xmlhttp.setRequestHeader("Content-Type",
                  "application/x-www-form-urlencoded");
            }
            xmlhttp.onreadystatechange = function () {
                if (xmlhttp.readyState == 4 && !bComplete) {
                    bComplete = true;
                    fnDone(xmlhttp);
                }
            };
            xmlhttp.send(sVars);
        }
        catch (z) { return false; }
        return true;
    };
    return this;
}


//
//
var GetCntlrResponse = function (oXML) {
    var response = oXML.responseText;
    document.getElementById(CurrDivName).innerHTML = response;

};

var doAJAXCall = function (PageURL, ReqType, PostStr, FunctionName) {
    var myConn = new XHConn();
    if (myConn) {
        myConn.connect('' + PageURL + '', '' + ReqType + '', '' + PostStr + '', FunctionName);
    }
    else {
        alert("XMLHTTP not available. Try a newer/better browser, this application will not work!");
    }
}


//Call Function 
function fnCallAjaxAPI(flg, divName, myUrl) {
    CurrDivName = divName;
    var PostStr = "";
    doAJAXCall(myUrl, 'POST', '' + PostStr + '', GetCntlrResponse);
}

------------------------------------------------------------------------------------------------


No comments:

Post a Comment

Factorial of a Number

Recently Viewed