I am trying to change the value of an hidden input every n seconds with a timer in my webpart.ascx.cs to retrieve the value later on in JavaScript, but the value never seems to change. Can you help me figuring out why?
Here's an exemple of what I'm trying to do :
//Initialising the timer
protected void Page_Load(object sender, EventArgs e)
{
Timer timer = new Timer(10 * 1000);
timer.Elapsed += new ElapsedEventHandler(changeValue);
timer.Start();
}
private void changeValue(Object source, ElapsedEventArgs e)
{
myInput.Value = "Changed Value";
}
Then in my ascx I have this input:
<input id="myInput" type="hidden" runat="server" />
If I call changeValue in Page_Load, the value of the input does change, but when it is called afterward with the timer, the value stays the same in the Javascript side.
//Work for the first time, but not when the timer elapsed.
var myInputValue = document.getElementById("myInput").value;
Thanks for your help.
I looked up the UpdatePanels as @tigerdi suggested me and I was able to update the html from the server side using an ASP Timer. Here the solution that I found :
This Is my UpdatePanel and my timer.
<asp:UpdatePanel id="updatePanel" runat="server" `UpdateMode="Conditional">
<ContentTemplate>
<asp:Timer ID="timer" OnTick="timer_Tick" Interval="10000" runat="server"></asp:Timer>
<input id="myInput" type="hidden" runat="server" />
</ContentTemplate>
Then with the event timer_Tick, I can change the value of my input and then call the UpdatePanel Update() function like so
protected void timer_Tick(object sender, EventArgs e)
{
myInput.Value = "Changed Value";
updatePanel.Update();
}