I am trying to update a textbox using the code behind in an ASP application like this:
protected void click_handler(object sender, EventArgs e)
{
Thread worker = new Thread(new ThreadStart(thread_function));
worker.Start();
}
protected void thread_function()
{
int i = 0;
while(true)
{
Textbox.Text = i.ToString();
i++;
}
}
The textbox shows one variable the first time but it doesn't get updated after that, what am I doing wrong? I searched and people are suggestin calling Textbox.Update or Textbox.Refresh but I think these are old as they don't exist anymore.
Thanks
You can't use server side code to update client side values in this manner. Anyway, you seem to be missing some kind of pause (e.g. Thread.Sleep) as currently your loop will run wildly out of control.
You need to look into using client side scripting (I.e. JavaScript) along with something like setTimeout
:
https://developer.mozilla.org/en/docs/Web/API/window.setTimeout
Here's an example: http://jsfiddle.net/PXN9K/
Apologies for the poor formatting and slightly lazy naming/refactoring, but I'm doing this on my phone.
Here's the code from the fiddle, copied here for future posterity.
var i = 0;
var updateTextbox =function()
{ document.getElementById('textbox').value = '' + i; }
var update = function() {
window.setTimeout(function() {
i++;
updateTextbox();
update();
}, 1000);
};
updateTextbox();
update();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With