I placed a label that named as LabelTotalNumber on Asp.net webform. I want update a value every one minute by jQuery without refresh the form. How do I do it?
I can do this way in Page_Load event for one time.
var sqlResult = (From obj in db.RequestList
where obj.IsApproved == "Approved"
select obj).count();
LabelTotalNumber.text = sqlResult;
Out-of-the-Microsoft-box you could use an UpdatePanel and a Timer.
If you don't like the contents of the Microsoft box and wanna use jQuery/javascript you could send AJAX requests at regular intervals (using the window.setInterval
method) to an ASP.NET PageMethod (in which case basically you will have to give yourself a little pain to read jQuery's documentation/tutorials and write a little code).
So an ASP.NET PageMethod:
[WebMethod]
public static int Approved()
{
return (from obj in db.RequestList
where obj.IsApproved == "Approved"
select obj).Count();
}
and then hammer this PageMethod with an AJAX request every minute or so:
window.setInterval(function() {
$.ajax({
url: '/foo.aspx/Approved',
type: 'POST',
contentType: 'application/json',
data: '{ }',
success: function(result) {
var count = result.d;
// TODO: do something with the count returned by the server
// like assigning it to a label or something:
$('#someLabelId').html(count);
}
});
}, 60 * 1000);
You may use jQuery ajax to invoke a server page where you can do the database operation. Execute your script in a specific interval using setInterval method.
var tid = setInterval(UpdateData, 60000);
function UpdateData() {
$.get("yourserverpage.ashx",function(data){
$("#yourDiv").html(data)
});
}
Assumung yourserverpage.ashx
will handle your database updation when it is invoked and yourDiv is the div where you want to show the updated data ( assuming yourserverpage.ashx
is returning the markup to be displayed.