These are the steps of what you need to do, with jQuery and WebMethod:
1) You will change your runTestimonial()
function into a WebMethod
that will return a string (the random testimonial). So your function's signature would look like this:
[WebMethod]
public static string runTestimonial()
{
return randomTestimonial; //include your code
}
2) Add jQuery library in the head of your file.
<script src="http://code.jquery.com/jquery-latest.js"></script>
3) Create a function that will do an ajax call to your webmethod.
function getTestimonial()
{
$.ajax({
type: "POST",
url: "Default.aspx/runTestimonial",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
$("#Fader").html(msg); //msg contains the response from the server, this appends it to the div
}
});
}
4) Your div in your markup will NOT be a server control, so remove the onload
. So it will look like this:
<div class="testimonial" ID="Fader"></div>
5) We are almost done, just make sure to call your getTestimonial()
function inside your setInterval.
The [WebMethod]
attribute is found in the System.Web.Services
library, so make sure to include it at the top of your page:
using System.Web.Services;
That's all, that should work.