SIGN UP MEMBER LOGIN:    
ARTICLE

Image Rotator in Ajax

Posted by Mahak Gupta Articles | AJAX in C# June 16, 2011
In this article you will see how to use the Image Rotator in Ajax.
Reader Level:


Step 1: In this Example, there is an Image (img1) and a TextBox(TextBox1).

<img id="img1" src="Image/1.jpg" /><br />

<asp:TextBox ID="TextBox1" runat="server">1</asp:TextBox>

ImgRorator1.gif

Image 1

Step 2: We call a function ChangeImage() on the onload event of the <body> tag. So when the page loads the function will be called automatiacally.

var xmlHttp
        var arr;
        function ChangeImage() {
            xmlHttp = GetXmlHttpObject()
            var url = "Default.aspx"
            url = url + "?iname=" + document.getElementById('TextBox1').value
            xmlHttp.onreadystatechange = stateChanged
            xmlHttp.open("GET", url, true)
            xmlHttp.send(null)
            return false;
        }


We set the Text property of TextBox1:

<asp:TextBox ID="TextBox1" runat="server">1</asp:TextBox>

Now we write the function ChangeImage():

ImgRorator2.gif

Now we can write the GetXmlHttpObject() to check the browser:

function GetXmlHttpObject() {
var objXMLHttp = null
if (window.XMLHttpRequest) {
objXMLHttp = new XMLHttpRequest()
}
else if (window.ActiveXObject) {
objXMLHttp = new ActiveXObject("Microsoft.XMLHTTP")
}
return objXMLHttp
}

ImgRorator3.gif

Step 3: In the Page_Load Event:

if (Request.QueryString["iname"] != null)
        {
            string i = Request.QueryString["iname"].ToString();
            string str = "1";
            if (i == "2")
            {

                str = "2";
            }

            Response.Clear();

            Response.Write(str);
            Response.End();
        }


Complete Program

Default.aspx


<head runat="server">
<title></title>
<
script language="JavaScript" type="text/javascript" >
var xmlHttp
var arr;
function ChangeImage() {
xmlHttp = GetXmlHttpObject()
var url = "Default.aspx"
url = url + "?iname=" + document.getElementById('TextBox1').value
xmlHttp.onreadystatechange = stateChanged
xmlHttp.open("GET", url, true)
xmlHttp.send(null)
return false;
}
function stateChanged() {

if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {

var str;

str = xmlHttp.responseText;

if (str == "1") {
document.getElementById("img1").src = "Image/211825_100000984069347_8367124_q.jpg";
document.getElementById("TextBox1").value = "2";
setTimeout("ChangeImage()", 1000);
}
if (str == "2") {
document.getElementById("img1").src = "Image/17Dec20100846PMfinal33.jpg";
setTimeout("ChangeImage()", 1000);
document.getElementById("TextBox1").value = "1";
}

}
}
function GetXmlHttpObject() {

var objXMLHttp = null
if (window.XMLHttpRequest) {
objXMLHttp = new XMLHttpRequest()
}
else if (window.ActiveXObject) {
objXMLHttp = new ActiveXObject("Microsoft.XMLHTTP")
}
return objXMLHttp
}

</script>

</head>
<
body onload="ChangeImage()">
<form id="form1" runat="server">
<div>

<img alt=""  id="img1"
style="height: 156px; width: 233px" src="Image/1.jpg" /><br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<asp:TextBox ID="TextBox1" runat="server">1</asp:TextBox>
<br />

</div>
</
form>
</
body>
</
html>

Default.aspx.cs

protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString["iname"] != null)
{
string i = Request.QueryString["iname"].ToString();
string str = "1";
if (i == "2")
{

str = "2";
}

Response.Clear();

Response.Write(str);
Response.End();
}
}

Login to add your contents and source code to this article
share this article :
post comment
 

Thank you so much sir.

Posted by Mahak Gupta Jun 16, 2011

Thanx sir, Thank you so much.

Posted by Mahak Gupta Jun 16, 2011

Nice sample

Posted by Karthikeyan Anbarasan Jun 16, 2011

Good to see some AJAX code samples.

Posted by Mahesh Chand Jun 16, 2011
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Nevron Gauge for SharePoint
Become a Sponsor