简体   繁体   中英

How to connect between ASP.NET page and javascript

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;
}

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.

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

Write function like this

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM