Tuesday, February 14, 2012

ASP.Net AJAX using Java Script

Ajax Technique using Java Script in ASP.Net








Step1)Add the following code in your ASP.NET page


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ajaxjavascript.aspx.cs" Inherits="ajaxjavascript" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>ASP.Net AJAX page</title>
<script language=javascript>

function ajax()
{
 var xmlhttp;
 if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
  }
 else
  {// code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
 xmlhttp.onreadystatechange=function()
  {
   if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
      document.getElementById("myp").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","HTMLPage.htm",true);
xmlhttp.send();
}

</script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <p id="myp"></p>
        <input id="Button1" type="button" value="Click me" onclick="ajax()" />
    </div>
    </form>
</body>
</html>

Step 2)Add the following code into HTMLPage.htm file .

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>Untitled Page</title>
</head>
<body>
    Hello AJAX
</body>
</html>

N.B:-Now when you will press the button the page content will change without reloading whole page.

No comments:

Post a Comment