Label text update on every second in web form

Ketan Vadodariya

I am doing some .Net Stuff for the application.I have a web form with one button and label,Here i have to update the some label message at every one second.so i can see the run time log on the web form.I Have a very simple web form with button and label, but lable is not updating.

Once i click on the submit button @MyTimerEventHandler will be called at every one second.

can you suggest how to do i see the run time log at web form using label or text box something.

protected void Page_Load(object sender, EventArgs e)
        {
              // To update the first time.
              Label2.Text = "hello";

        }
    private void MyTimerEventHandler(object source, System.Timers.ElapsedEventArgs e)
        {
              nTotalSeconds += nIntervalInSeconds;
              Label2.Text = "timer " + nTotalSeconds.ToString();
              System.Diagnostics.Debug.WriteLine("called");
              
        }
        protected void Button2_Click(object sender, EventArgs e)
        {

              System.Timers.Timer myTimer = new System.Timers.Timer();
              myTimer.Elapsed += new System.Timers.ElapsedEventHandler(MyTimerEventHandler);
              myTimer.Interval = nIntervalInSeconds * 1000;
              myTimer.Start();
    }

enter image description here

Thomson Mixab

Try using asp:ScriptManager and asp:UpdatePanel.

Html (.aspx)

  <form id="form1" runat="server">             
        <asp:ScriptManager ID="ScriptManager1" runat="server" />
        <asp:Timer runat="server" id="UpdateTimer" ontick="UpdateTimer_Tick" />
        <asp:UpdatePanel runat="server" id="TimedPanel" updatemode="Conditional">
            <Triggers>
                <asp:AsyncPostBackTrigger controlid="UpdateTimer" eventname="Tick" />
            </Triggers>
            <ContentTemplate>
                 <asp:Label id="Label1" runat="server" Text="1" />
            </ContentTemplate>
        </asp:UpdatePanel>
    </form>
    
    
    
Code behind

protected void Button2_Click(object sender, EventArgs e)
{
    UpdateTimer.interval = 1000;    
}

protected void UpdateTimer_Tick(object sender, EventArgs e)
{
    System.Diagnostics.Debug.WriteLine("called");
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related