Update text property of asp label control on a webform

G

Guest

Hi,
Is there a way to update the text of an asp:label on a webform without
refreshing the entire page? What is called by button clicks and other
events that refresh a webform control?

See the example WebForm1.aspx and WebForm1.aspx.cs code below:

WebForm1.aspx
=======================================================

<%@ Page language="c#" Codebehind="WebForm1.aspx.cs"
AutoEventWireup="false" Inherits="updateLabelTest.WebForm1" %>

<HTML>
<HEAD>
<title>WebForm1</title>
</HEAD>
<body >

<form id="Form1" method="post" runat="server">
<asp:Button id=Button1 runat="server" Text="Button"></asp:Button>
<asp:Label id=Label1 runat="server">Label</asp:Label>
<br>
<asp:Button id=Button2 runat="server" Text="Button"></asp:Button>
<asp:Label id=Label2 runat="server">Label</asp:Label>
</form>

</body>
</HTML>




WebForm1.aspx.cs
=======================================================

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace updateLabelTest
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.Label Label2;
protected System.Web.UI.WebControls.Button Button2;

int nTotalSeconds = 0;
int nIntervalInSeconds = 5;

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Button2.Click += new System.EventHandler(this.Button2_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

// Option 1: Simply update the label Text within a loop
private void Button1_Click(object sender, System.EventArgs e)
{
// Trying to update the text of the asp:label every second.
// Label1.Text does not get updated until
// after Button1_Click returns.
// Thus, i = 4 shows up, but we never see i = 1, 2, or 3.
//
// I realize this could be done via javascript using:
// <meta http-equiv="refresh" content="5">
// However, I have a more complex C# website application that
// requires updating from within the asp.net CodeBehind.
for( int i=1; i < 5; i++ )
{
Label1.Text = i.ToString();
System.Threading.Thread.Sleep( 1000 );
}
}

// Option 2: Update using a System Timer
private void Button2_Click(object sender, System.EventArgs e)
{
// I'd prefer to use a Timer and update based on the Timer Interval.
// I don't understand why Label2.Text never gets updated despite
// MyTimerEventHandler firing even 5 seconds.
System.Timers.Timer myTimer = new System.Timers.Timer();
myTimer.Elapsed += new System.Timers.ElapsedEventHandler(
MyTimerEventHandler );
myTimer.Interval = nIntervalInSeconds*1000;
myTimer.Start();
}

private void MyTimerEventHandler( object source,
System.Timers.ElapsedEventArgs e )
{
nTotalSeconds += nIntervalInSeconds;
Label2.Text = "timer " + nTotalSeconds.ToString();
}

}
}
 
B

Brock Allen

You need to realize that the threading you're doing in your C# code is on
the server. So while your serevr code is doing Thread.Sleep, the browser
is still waiting for the page to be rendered. This is not the approach you
want. If you want the browser to come back to the server every 5 seconds,
then, as you mentioned, you'll need the meta-refresh or you'll need to do
something in the browser with window.setinterval. The code you write in the
page doesn't render back to the browser until it's completely processed,
so the browser's just going to wait. Also, if you spin up another thread
and then try to do something there, the main thread that was rendering your
page will have completed, so the secondary thread will be out of context
if it tries to emit anything else.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top