By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,025 Members | 2,118 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,025 IT Pros & Developers. It's quick & easy.

Ajax and Iframes

P: n/a
Hi.

I will start to migrate an asp.net 1.1 application to 2.0. This application
uses an Iframe that simply calls a method to update a field on my databse
every minute. In order to do this, I actually do a page refresh that runs my
method on the server side. how can I do this using Visual Studio 2005 and
Ajax? I do not need to update the interface, I just need to update the
database.

Regards,
Erik Cruz
Oct 1 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Hi,

Erik Cruz wrote:
Hi.

I will start to migrate an asp.net 1.1 application to 2.0. This application
uses an Iframe that simply calls a method to update a field on my databse
every minute. In order to do this, I actually do a page refresh that runs my
method on the server side. how can I do this using Visual Studio 2005 and
Ajax? I do not need to update the interface, I just need to update the
database.

Regards,
Erik Cruz
You can remove the IFrame and use XmlHttpRequest to send a periodic GET
to the server. Note that this was already possible in .NET 1.1, in fact
it is possible since 1997 :-)

Here is a previous post I made in that newsgroup:

The simplest implementation of AJAX in .NET is using ASHX Custom
HttpHandlers. It's very simple: In your web site or web application
project, Add New Item / Generic handler.

Then, in the code behind, implement the methods (Studio 2005 gives you a
template).

From the client, use JavaScript and XmlHttpRequest to send the request
and read the response. There are many tutorial online.

Example for a simple asynchronous request:

var oHttp = null;
if ( window.XMLHttpRequest )
{
oHttp = new window.XMLHttpRequest();
}
else
{
if ( window.ActiveXObject )
{
oHttp = new window.ActiveXObject( "Microsoft.XMLHTTP" );
}
else
{
throw "UNSUPPORTED PLATFORM";
}
}
if ( !oHttp )
{
throw "Cannot create XmlHttpRequest";
}

var strQuery = "?param1=value1&param2=value2";
oHttp.open( "GET",
"myHandler.ashx" + strQuery,
true ); // true = async, false = sync

oHttp.onreadystatechange = function()
{
if ( oHttp.readyState == 4 )
{
oHttp = null;
fnCallback( oHttp );
}
}

oHttp.send( null );

Code behind:

In ProcessRequest, use the "context" parameter to extract the
QueryString, and then you can process according to the parameters.

For the Response, if you want to send XML code back, make sure to set
context.Response.ContentType = "text/xml; charset=utf-8";

To save an XML document to the response, use
docResponse.Save(
new XmlTextWriter( context.Response.OutputStream,
context.Request.ContentEncoding ) );

In the JavaScript, the XML code will be available in oHttp.responseXML.
The response is also available in plain text in oHttp.responseText. Also
check the oHttp.status, which contains status like 200 (OK), 500 (Server
error), etc...

If you have question, don't hesitate.

Greetings,
Laurent
--
Laurent Bugnion, GalaSoft
Software engineering: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch
Oct 1 '06 #2

P: n/a
Hi Laurent,

thanks for the answer.

I see that your code has a trap for unsupported platforms, but do you know
if this code works with Netscape and Firefox also?

Thanks
Erik
"Laurent Bugnion" <ga*********@bluewin.chwrote in message
news:uL**************@TK2MSFTNGP04.phx.gbl...
Hi,

Erik Cruz wrote:
>Hi.

I will start to migrate an asp.net 1.1 application to 2.0. This
application uses an Iframe that simply calls a method to update a field
on my databse every minute. In order to do this, I actually do a page
refresh that runs my method on the server side. how can I do this using
Visual Studio 2005 and Ajax? I do not need to update the interface, I
just need to update the database.

Regards,
Erik Cruz

You can remove the IFrame and use XmlHttpRequest to send a periodic GET to
the server. Note that this was already possible in .NET 1.1, in fact it is
possible since 1997 :-)

Here is a previous post I made in that newsgroup:

The simplest implementation of AJAX in .NET is using ASHX Custom
HttpHandlers. It's very simple: In your web site or web application
project, Add New Item / Generic handler.

Then, in the code behind, implement the methods (Studio 2005 gives you a
template).

From the client, use JavaScript and XmlHttpRequest to send the request and
read the response. There are many tutorial online.

Example for a simple asynchronous request:

var oHttp = null;
if ( window.XMLHttpRequest )
{
oHttp = new window.XMLHttpRequest();
}
else
{
if ( window.ActiveXObject )
{
oHttp = new window.ActiveXObject( "Microsoft.XMLHTTP" );
}
else
{
throw "UNSUPPORTED PLATFORM";
}
}
if ( !oHttp )
{
throw "Cannot create XmlHttpRequest";
}

var strQuery = "?param1=value1&param2=value2";
oHttp.open( "GET",
"myHandler.ashx" + strQuery,
true ); // true = async, false = sync

oHttp.onreadystatechange = function()
{
if ( oHttp.readyState == 4 )
{
oHttp = null;
fnCallback( oHttp );
}
}

oHttp.send( null );

Code behind:

In ProcessRequest, use the "context" parameter to extract the QueryString,
and then you can process according to the parameters.

For the Response, if you want to send XML code back, make sure to set
context.Response.ContentType = "text/xml; charset=utf-8";

To save an XML document to the response, use
docResponse.Save(
new XmlTextWriter( context.Response.OutputStream,
context.Request.ContentEncoding ) );

In the JavaScript, the XML code will be available in oHttp.responseXML.
The response is also available in plain text in oHttp.responseText. Also
check the oHttp.status, which contains status like 200 (OK), 500 (Server
error), etc...

If you have question, don't hesitate.

Greetings,
Laurent
--
Laurent Bugnion, GalaSoft
Software engineering: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch

Oct 6 '06 #3

P: n/a
q
Just about anything you can do in IE, you can do in Firefox better.
That said, just because you can do it in Firefox, that in no way means
you can do it in IE7. Most things you actually can't do at all.
Firefox==nuclear bomb, IE7==watered down firecracker.

If you look at the code you will see "window.XMLHttpRequest ". That's
for standard browsers... real web browsers. But legacy Intranet
Explorer 6 required a hack "new window.ActiveXObject(
"Microsoft.XMLHTTP" );" (this one was fixed in IE7 thank goodness).

Erik Cruz wrote:
Hi Laurent,

thanks for the answer.

I see that your code has a trap for unsupported platforms, but do you know
if this code works with Netscape and Firefox also?

Thanks
Erik
"Laurent Bugnion" <ga*********@bluewin.chwrote in message
news:uL**************@TK2MSFTNGP04.phx.gbl...
Hi,

Erik Cruz wrote:
Hi.

I will start to migrate an asp.net 1.1 application to 2.0. This
application uses an Iframe that simply calls a method to update a field
on my databse every minute. In order to do this, I actually do a page
refresh that runs my method on the server side. how can I do this using
Visual Studio 2005 and Ajax? I do not need to update the interface, I
just need to update the database.

Regards,
Erik Cruz
You can remove the IFrame and use XmlHttpRequest to send a periodic GET to
the server. Note that this was already possible in .NET 1.1, in fact it is
possible since 1997 :-)

Here is a previous post I made in that newsgroup:

The simplest implementation of AJAX in .NET is using ASHX Custom
HttpHandlers. It's very simple: In your web site or web application
project, Add New Item / Generic handler.

Then, in the code behind, implement the methods (Studio 2005 gives you a
template).

From the client, use JavaScript and XmlHttpRequest to send the request and
read the response. There are many tutorial online.

Example for a simple asynchronous request:

var oHttp = null;
if ( window.XMLHttpRequest )
{
oHttp = new window.XMLHttpRequest();
}
else
{
if ( window.ActiveXObject )
{
oHttp = new window.ActiveXObject( "Microsoft.XMLHTTP" );
}
else
{
throw "UNSUPPORTED PLATFORM";
}
}
if ( !oHttp )
{
throw "Cannot create XmlHttpRequest";
}

var strQuery = "?param1=value1&param2=value2";
oHttp.open( "GET",
"myHandler.ashx" + strQuery,
true ); // true = async, false = sync

oHttp.onreadystatechange = function()
{
if ( oHttp.readyState == 4 )
{
oHttp = null;
fnCallback( oHttp );
}
}

oHttp.send( null );

Code behind:

In ProcessRequest, use the "context" parameter to extract the QueryString,
and then you can process according to the parameters.

For the Response, if you want to send XML code back, make sure to set
context.Response.ContentType = "text/xml; charset=utf-8";

To save an XML document to the response, use
docResponse.Save(
new XmlTextWriter( context.Response.OutputStream,
context.Request.ContentEncoding ) );

In the JavaScript, the XML code will be available in oHttp.responseXML.
The response is also available in plain text in oHttp.responseText. Also
check the oHttp.status, which contains status like 200 (OK), 500 (Server
error), etc...

If you have question, don't hesitate.

Greetings,
Laurent
--
Laurent Bugnion, GalaSoft
Software engineering: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch
Oct 6 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.