I have encountered an issue where I am attempting to dynamically update a label every 5 seconds using AJAX and interval in my Webforms application. However, the label only updates when I manually refresh the page. It does not change without reloading the page.
On the client side:
In my JavaScript code:
setInterval(function () {
function GetStatus() {
$.ajax({
type: "POST",
url: "RunningConsolePage.aspx/GetStatus",
data: '{name: "' + $("#<%=lblStatus.ClientID%>")[0].value + '" }',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnSuccess,
failure: function (response) {
alert(response.d);
}
});
}
function OnSuccess(response) {
$("#<%=lblStatus.Text=GetStatus()%>")
}
}, 5);
In my HTML:
<asp:Button ID="btnQArun" Text="QA" OnClick="btnQArun_Click" CssClass="btn btn-info btnSize" OnClientClick="Confirm('QA')" runat="server" />
<asp:Label ID="lblStatus" Text="Status" runat="server"/>
On the server side:
[System.Web.Services.WebMethod]
public static string GetStatus()
{
Random random = new Random();
const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
return new string(Enumerable.Repeat(chars, 11)
.Select(s => s[random.Next(s.Length)]).ToArray());
}