locked
How to diplay real time timing(clock) on asp.net web page RRS feed

  • Question

  • User-337330076 posted

    Hi everyone,

    Does anyone know how to display real time timing(clock) on asp.net web page by using C# language? Previously, I have done the following code, but the code is written in Javascript and declared in HTML page. But now, I have to change it to C# language and call in page_load method. Is there any solution to do this?


    <script type="text/javascript">
    function dTime()
    	{
    	  var d = new Date();
    	  var h = d.getHours();
    	  var m = d.getMinutes();
    	  var s = d.getSeconds();
    	
    	  //add a zero in front of numbers < 10
    	  m = checkTime(m);
    	  s = checkTime(s);
    	  
    	  document.getElementById('txt').innerHTML = "Time: " + h + ":" + m + ":" + s;
    	  
    	  t = setTimeout ('dTime()', 500);
    	}
    
    	function checkTime(i)
    	{
    
    		if (i < 10)
    		{
    	  	   i= "0" + i;
    		}
    	
    		return i;
    	}
    </script>


    Thanks.

    Monday, February 22, 2010 10:34 PM

Answers

  • User845932231 posted

    Is there any special reason you want to do it in C#?

    You can use following C# code to show the current date time

    lbl.Text = DateTime.Now.ToLongDateString();

    but it will show only the server time.. not the client time.. if you want the client time to be displayed, you should use js..



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, February 22, 2010 11:15 PM
  • User-1629691846 posted


    For getting live time on webpage by using c#, you have to use Ajax.e.g

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>Untitled Page</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div style="background-color: #0076AE">
        
            <asp:ScriptManager ID="ScriptManager1" 
                runat="server">
            </asp:ScriptManager>
    <asp:Timer ID="Timer1" runat="server" Interval="1000">
                    </asp:Timer>
            <asp:UpdatePanel ID="UpdatePanel1" 
                runat="server">
                <ContentTemplate>
                       <asp:Label ID="Label1" runat="server" 
                        ForeColor="White" ></asp:Label>
                    <br />
                </ContentTemplate>
                <Triggers>
                    <asp:AsyncPostBackTrigger ControlID="tIMER1" EventName="Tick"></asp:AsyncPostBackTrigger>
                </Triggers>
            </asp:UpdatePanel>
            <br />
        
        </div>
        </form>
    </body>
    </html>

     Protected Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            Label1.Text = TimeOfDay.ToLongTimeString
        End Sub




    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, February 22, 2010 11:31 PM
  • User-1629691846 posted

    Hi Rohit Rao,

    I have tried what you have given just now. However, the time is still fixed, it does not change to real time.


    Below is the code that I have tried. Please take a look.


    ·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150
    1. protected void Page_Load(object sender, EventArgs e)  
    2. {  
    3.         lblTime.Text = DateTime.Now.ToString("hh:mm:ss");  
    4.           
    5.     }  
    6.     protected void TimerTime_Tick(object sender, EventArgs e)  
    7.     {  
    8.         lblTime.Text = DateTime.Now.ToString("hh:mm:ss");  
    9.     }  
    10.   
    11. <asp:ScriptManager ID="ScriptManager1" runat="server">  
    12.         </asp:ScriptManager>  
    13.         <asp:Timer ID="TimerTime" runat="server" Interval="1000">  
    14.         </asp:Timer>  
    15.         <asp:UpdatePanel ID="UpdatePanel1" runat="server">  
    16.         <ContentTemplate>  
    17.         <asp:Label ID="lblDateToday" runat="server"></asp:Label>  
    18.                 <asp:Label ID="lblTime" runat="server"></asp:Label>  
    19.             </ContentTemplate>  
    20.             <Triggers>  
    21.                 <asp:AsyncPostBackTrigger ControlID="TimerTime" EventName="Tick" />  
    22.             </Triggers>  
    23.         </asp:UpdatePanel>  
    24.         </div>  
    25.         <br />  
    protected void Page_Load(object sender, EventArgs e)
    {
            lblTime.Text = DateTime.Now.ToString("hh:mm:ss");
            
        }
        protected void TimerTime_Tick(object sender, EventArgs e)
        {
            lblTime.Text = DateTime.Now.ToString("hh:mm:ss");
        }
    
    <asp:ScriptManager ID="ScriptManager1" runat="server">
            </asp:ScriptManager>
            <asp:Timer ID="TimerTime" runat="server" Interval="1000">
            </asp:Timer>
            <asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
            <asp:Label ID="lblDateToday" runat="server"></asp:Label>
                    <asp:Label ID="lblTime" runat="server"></asp:Label>
                </ContentTemplate>
                <Triggers>
                    <asp:AsyncPostBackTrigger ControlID="TimerTime" EventName="Tick" />
                </Triggers>
            </asp:UpdatePanel>
            </div>
            <br />





    Thanks.

    hi buddy,

                i have tested your code.Its working fine.Check it once again.its working as you want.



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 23, 2010 12:41 AM
  • User-1629691846 posted

    buddy,

    when i have tested your code the seconds is moving.There may be another issue.

    Do check it on other development machine.If you want then i will show you by uploading 

    this page on web in which seconds is moving by using your code.

    On which browser you are testing it?

    Check this link -> your code is working here. ->http://aspspider.biz/RohitRao/Default2.aspx

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 23, 2010 1:26 AM
  • User-1629691846 posted

    Check this - >

    http://aspspider.biz/RohitRao/Default2.aspx

    its working fine in mozilla.If you want then i can mail you the default2.aspx & its .cs file.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 23, 2010 1:38 AM
  • User-1629691846 posted

    Have you checked it????

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 23, 2010 2:00 AM
  • User-1629691846 posted

    ok...Check your mail & use that ajax lib. i have sent u.The code is the same as you post earlier.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 23, 2010 2:09 AM
  • User-1629691846 posted

    have you tested it on other browsers???have you used my ajax library????

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 23, 2010 2:30 AM
  • User-1629691846 posted

    Have u added the reference of Dll of AjaxLibrary in ur Project or in Bin folder of your Application?

    u have to add the reference of AjaxControlToolkit.dll to your application.then scriptmanager & timer will be there 

    in your Toolbox in Ajax Extensions.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 23, 2010 2:40 AM
  • User-1629691846 posted

    Right click on Ajax Control toolkit option in Toolbox then Choose Items - > .Net Framework Component.Then find Timer & Script Manager there.

    Do you have Visual Studio 2008?

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 23, 2010 3:31 AM
  • User-1629691846 posted

    ok....if you are using Vs 2005 then download the ajax control toolkit for vs 2005

    & then add that dll into your project then you will find Timer & scriptManager.

    At present i cant connect with you by team viewer.m in office.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 23, 2010 3:55 AM
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 23, 2010 6:40 AM

All replies

  • User845932231 posted

    Is there any special reason you want to do it in C#?

    You can use following C# code to show the current date time

    lbl.Text = DateTime.Now.ToLongDateString();

    but it will show only the server time.. not the client time.. if you want the client time to be displayed, you should use js..



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, February 22, 2010 11:15 PM
  • User1353953242 posted

    You can use following code for this...

    between <head>....</head> tag put

     

    <% Response.AddHeader("Refresh", "timeinseconds") %>  OR

    <% Response.AddHeader("Refresh", "1") %> 

    and on page_load  assign

    lable.text = DateTime.Now().Tostring("TimeFormate");

     

    Or You can also use Ajax Timer control for this.

    Monday, February 22, 2010 11:19 PM
  • User-1629691846 posted


    For getting live time on webpage by using c#, you have to use Ajax.e.g

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>Untitled Page</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div style="background-color: #0076AE">
        
            <asp:ScriptManager ID="ScriptManager1" 
                runat="server">
            </asp:ScriptManager>
    <asp:Timer ID="Timer1" runat="server" Interval="1000">
                    </asp:Timer>
            <asp:UpdatePanel ID="UpdatePanel1" 
                runat="server">
                <ContentTemplate>
                       <asp:Label ID="Label1" runat="server" 
                        ForeColor="White" ></asp:Label>
                    <br />
                </ContentTemplate>
                <Triggers>
                    <asp:AsyncPostBackTrigger ControlID="tIMER1" EventName="Tick"></asp:AsyncPostBackTrigger>
                </Triggers>
            </asp:UpdatePanel>
            <br />
        
        </div>
        </form>
    </body>
    </html>

     Protected Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            Label1.Text = TimeOfDay.ToLongTimeString
        End Sub




    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, February 22, 2010 11:31 PM
  • User-337330076 posted

    Hi Rohit Rao,

    I have tried what you have given just now. However, the time is still fixed, it does not change to real time.


    Below is the code that I have tried. Please take a look.

    protected void Page_Load(object sender, EventArgs e)
    {
            lblTime.Text = DateTime.Now.ToString("hh:mm:ss");
            
        }
        protected void TimerTime_Tick(object sender, EventArgs e)
        {
            lblTime.Text = DateTime.Now.ToString("hh:mm:ss");
        }
    
    <asp:ScriptManager ID="ScriptManager1" runat="server">
            </asp:ScriptManager>
            <asp:Timer ID="TimerTime" runat="server" Interval="1000">
            </asp:Timer>
            <asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
            <asp:Label ID="lblDateToday" runat="server"></asp:Label>
                    <asp:Label ID="lblTime" runat="server"></asp:Label>
                </ContentTemplate>
                <Triggers>
                    <asp:AsyncPostBackTrigger ControlID="TimerTime" EventName="Tick" />
                </Triggers>
            </asp:UpdatePanel>
            </div>
            <br />


    Thanks.

    Tuesday, February 23, 2010 12:13 AM
  • User-1629691846 posted

    Hi Rohit Rao,

    I have tried what you have given just now. However, the time is still fixed, it does not change to real time.


    Below is the code that I have tried. Please take a look.


    ·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150
    1. protected void Page_Load(object sender, EventArgs e)  
    2. {  
    3.         lblTime.Text = DateTime.Now.ToString("hh:mm:ss");  
    4.           
    5.     }  
    6.     protected void TimerTime_Tick(object sender, EventArgs e)  
    7.     {  
    8.         lblTime.Text = DateTime.Now.ToString("hh:mm:ss");  
    9.     }  
    10.   
    11. <asp:ScriptManager ID="ScriptManager1" runat="server">  
    12.         </asp:ScriptManager>  
    13.         <asp:Timer ID="TimerTime" runat="server" Interval="1000">  
    14.         </asp:Timer>  
    15.         <asp:UpdatePanel ID="UpdatePanel1" runat="server">  
    16.         <ContentTemplate>  
    17.         <asp:Label ID="lblDateToday" runat="server"></asp:Label>  
    18.                 <asp:Label ID="lblTime" runat="server"></asp:Label>  
    19.             </ContentTemplate>  
    20.             <Triggers>  
    21.                 <asp:AsyncPostBackTrigger ControlID="TimerTime" EventName="Tick" />  
    22.             </Triggers>  
    23.         </asp:UpdatePanel>  
    24.         </div>  
    25.         <br />  
    protected void Page_Load(object sender, EventArgs e)
    {
            lblTime.Text = DateTime.Now.ToString("hh:mm:ss");
            
        }
        protected void TimerTime_Tick(object sender, EventArgs e)
        {
            lblTime.Text = DateTime.Now.ToString("hh:mm:ss");
        }
    
    <asp:ScriptManager ID="ScriptManager1" runat="server">
            </asp:ScriptManager>
            <asp:Timer ID="TimerTime" runat="server" Interval="1000">
            </asp:Timer>
            <asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
            <asp:Label ID="lblDateToday" runat="server"></asp:Label>
                    <asp:Label ID="lblTime" runat="server"></asp:Label>
                </ContentTemplate>
                <Triggers>
                    <asp:AsyncPostBackTrigger ControlID="TimerTime" EventName="Tick" />
                </Triggers>
            </asp:UpdatePanel>
            </div>
            <br />





    Thanks.

    hi buddy,

                i have tested your code.Its working fine.Check it once again.its working as you want.



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 23, 2010 12:41 AM
  • User-337330076 posted

    Hi Brother,

    Yes. It is working fine. But the seconds is not moving at all or another words the time is fixed unless i refresh the page. What should I do?

    Tuesday, February 23, 2010 1:18 AM
  • User-1629691846 posted

    buddy,

    when i have tested your code the seconds is moving.There may be another issue.

    Do check it on other development machine.If you want then i will show you by uploading 

    this page on web in which seconds is moving by using your code.

    On which browser you are testing it?

    Check this link -> your code is working here. ->http://aspspider.biz/RohitRao/Default2.aspx

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 23, 2010 1:26 AM
  • User845932231 posted

    Following links may help you..

    http://www.javascriptkit.com/script/cut2.shtml

    http://www.javascriptkit.com/script/cut7.shtml

    http://www.javascriptkit.com/script/script2/servertime.shtml

    http://www.dynamicdrive.com/dynamicindex6/index.html

    To show real time clock you need to use JS.. but if you want to display the server time, you can make some teaks in one of the above scripts and make it easily..

    Tuesday, February 23, 2010 1:35 AM
  • User-337330076 posted

    Brother,

    Would you please upload the page so that i can take a look at the output you get? I am using Mozilla FireFox for my testing.

    thanks.


    Tuesday, February 23, 2010 1:35 AM
  • User-1629691846 posted

    Check this - >

    http://aspspider.biz/RohitRao/Default2.aspx

    its working fine in mozilla.If you want then i can mail you the default2.aspx & its .cs file.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 23, 2010 1:38 AM
  • User-337330076 posted

    Hi Buddy,

    You can just send me to my email. I have sent an email to you with my email address.

    Tuesday, February 23, 2010 1:55 AM
  • User-1629691846 posted

    Have you checked it????

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 23, 2010 2:00 AM
  • User-337330076 posted

    Ya. I tested my code on another machine but still the same.


    Tuesday, February 23, 2010 2:05 AM
  • User-1629691846 posted

    ok...Check your mail & use that ajax lib. i have sent u.The code is the same as you post earlier.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 23, 2010 2:09 AM
  • User-337330076 posted

    Erm... I had tried your code that you sent me. It's the same, the time didn't move also. What's going on? I really have no idea.

    Tuesday, February 23, 2010 2:22 AM
  • User-1629691846 posted

    have you tested it on other browsers???have you used my ajax library????

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 23, 2010 2:30 AM
  • User-337330076 posted

    Erm... The ajax library need to add into the visual studio right? Then, I had added the ajax library into visual studio. But there is no script manager and timer.

    Tuesday, February 23, 2010 2:36 AM
  • User-1629691846 posted

    Have u added the reference of Dll of AjaxLibrary in ur Project or in Bin folder of your Application?

    u have to add the reference of AjaxControlToolkit.dll to your application.then scriptmanager & timer will be there 

    in your Toolbox in Ajax Extensions.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 23, 2010 2:40 AM
  • User-337330076 posted

    Ya. I have added and add the reference. But in the toolkit, i didn't see any timer or scriptmanager as you mentioned. I have sent you a print screen of the toolbox to your email.

    Tuesday, February 23, 2010 3:24 AM
  • User-1629691846 posted

    Right click on Ajax Control toolkit option in Toolbox then Choose Items - > .Net Framework Component.Then find Timer & Script Manager there.

    Do you have Visual Studio 2008?

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 23, 2010 3:31 AM
  • User-337330076 posted

    I tried the method you provided. It's still not working. Do you mind to remote control my computer via Team Viewer?

    Nope. I am using VS 2005.


    Tuesday, February 23, 2010 3:47 AM
  • User-1629691846 posted

    ok....if you are using Vs 2005 then download the ajax control toolkit for vs 2005

    & then add that dll into your project then you will find Timer & scriptManager.

    At present i cant connect with you by team viewer.m in office.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 23, 2010 3:55 AM
  • User-337330076 posted

    Opps. I can't find ajax control toolkit for vs 2005.

    Oh ok. It's ok.

    Tuesday, February 23, 2010 4:30 AM
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 23, 2010 6:40 AM
  • User-337330076 posted

    Hi Buddy,

    I have managed to figure out how to solve this problem. Thank you very much. I appreciate yr help for spending so much time on this problem. Thanks!!!



    Tuesday, February 23, 2010 11:10 AM
  • User-337330076 posted

    Hey Buddy,

    If you dont mind, I have another question to ask you regarding on asp.net web page. I am trying on calling web method on a web page.

    Currently, I have tried calling the web method on C# Web Form, but i need to do this on asp.net web page. The following is my code on web method and C# web application.

            private void button3_Click(object sender, EventArgs e)
            {
                RequestWS.Service rWS = new RequestWS.Service();
                int result = rWS.DoIT();
    
                if (result > 0)
                {
                    MessageBox.Show("YES! WE ARE SUCEEDDED!");
    
                }
                else
                {
                    MessageBox.Show("Invalid IpAddress");
                }
            }

        [WebMethod]
        public int DoIT()
        {
    
            // Get the hostname 
            string myHost = System.Net.Dns.GetHostName();
    
            string connect = "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA Source=c:\\inetpub\\wwwroot\\RequestWS\\App_Data\\TV.mdb";
            string query = "SELECT COUNT(IPAddress) FROM Request WHERE IPAddress = ?";
            int result = 0;
    
            using (OleDbConnection conn = new OleDbConnection(connect))
            {
                using (OleDbCommand cmd = new OleDbCommand(query, conn))
                {
                    cmd.CommandType = CommandType.Text;
                    cmd.Parameters.AddWithValue("IPAddress", myHost);
    
                    conn.Open();
                    result = (int)cmd.ExecuteScalar();
                }
            }
            
            return result;
    
        }
    


    Tuesday, February 23, 2010 3:17 PM
  • User-1199946673 posted

    If you dont mind, I have another question to ask you regarding on asp.net web page
     

    Please open a new thread:

    http://www.mikesdotnetting.com/Article/69/How-to-get-your-forum-question-answered-avoid-thread-jacking

    Tuesday, February 23, 2010 6:44 PM
  • User-156848378 posted

    Thanks Rohit Rao, created a user name and login to say thank you. Continuous seconds on ie10. vs2010

     

     

    -------------------------

    protected void Page_Load(object sender, EventArgs e)
    {
            lblTime.Text = DateTime.Now.ToString("hh:mm:ss");
            
    
        }
        protected void TimerTime_Tick(object sender, EventArgs e)
        {
            lblTime.Text = DateTime.Now.ToString("hh:mm:ss");
        }
    
    
    <asp:ScriptManager ID="ScriptManager1" runat="server">
            </asp:ScriptManager>
            <asp:Timer ID="TimerTime" runat="server" Interval="1000">
            </asp:Timer>
            <asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
            <asp:Label ID="lblDateToday" runat="server"></asp:Label>
                    <asp:Label ID="lblTime" runat="server"></asp:Label>
                </ContentTemplate>
                <Triggers>
                    <asp:AsyncPostBackTrigger ControlID="TimerTime" EventName="Tick" />
                </Triggers>
            </asp:UpdatePanel>
            </div>
            <br />

    Monday, May 6, 2013 12:12 PM