0

I'm trying to put current time on ASP.NET page. How can i get the control ID from Javascript? I'm using C# ASP.NET

On my Form

<body onload="updateClock(); setInterval('updateClock()', 1000 )">
    <form id="form1" runat="server" >
       <span id="clock">&nbsp;</span>
   </form>
</body>

Javascript Function

function updateClock ( )
{
   document.getElementById("clock").firstChild.nodeValue = currentTimeString;
}

3 Answers 3

1

Try this:

<script type="text/javascript">
   function ShowTime() {
   var dt = new Date();
   document.getElementById("<%= TextBox1.ClientID %>").value = dt.toLocaleTimeString();
   window.setTimeout("ShowTime()", 1000);
 }  
</script>

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

<script type="text/javascript">
    // a startup script to put everything in motion
    window.setTimeout("ShowTime()", 1000);
</script>

If you are using normal web form then it can also be called on Body onload event. If you are using MasterPage then it can be called within ContentTemplate at the end after all the controls have been rendered.

Sign up to request clarification or add additional context in comments.

1 Comment

"<%= TextBox1.ClientID %>" This one is what i wanted to know. Thanks a lot!
0

You can directly use JavaScript for displaying current time on the page.

Here is the link :- http://www.w3schools.com/js/tryit.asp?filename=tryjs_timing_clock

Comments

0

Write function like this

 function updateClock ( )
 {
   document.getElementById("clock").innerHTML = currentTimeString;
 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.