Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: .NET time
I here about literal that it is used to show dynamic text.
 
So I used Literal
 
Literal.Text= DateTime.Now.ToString();
 
Time is showing but it is not ticking.
 

:: I just want to do it with literal, I know other ways ::
 

 
Please Help me.
 
[edit]SHOUTING removed - OriginalGriff[/edit]
Posted 11-Apr-12 9:59am
Pr!y@1.1K
Edited 11-Apr-12 10:09am
v2
Comments
OriginalGriff at 11-Apr-12 15:09pm
   
DON'T SHOUT. Using all capitals is considered shouting on the internet, and rude (using all lower case is considered childish). Use proper capitalisation if you want to be taken seriously.
Harshit Raj Singh at 11-Apr-12 15:13pm
   
Oh Sir I was Not Shouting
And sorry if it was so
SAKryukov at 11-Apr-12 15:18pm
   
Before making it to tick, which is possible, it's important to understand why it does not tick now.
I would say, if you do not understand that, all your further ASP.NET development could be pretty much useless, because this is about on of the key principles. So, let me ask you: do you understand why no ticking is shown, or you need the explanation?
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You can't do it that way. Sorry, but ASP doesn't work like that.
 
When you read the DateTime.Now property, you get a single use, one shot, non-updating copy of the current date and time. Just like nearly all other objects in .NET, it will not change from this moment on, unless you specifically execute code which changes it's value.
 
The problem is that you need to think a bit more about how webpages work. C# is normally executed on the server - there is a very good chance that C# cannot execute on the client at all, even if you could find a way to get it there: unless the client has .NET installed it cannot work.
 
When a use navigates to your page, your application is started, the page load event is called, and when the final HTML has been sent to the client, your app is free to close - and in a busy server, it probably will to free up memory.
In order to update the client display on a regular basis from the server you app would have to remain resident at all times. There would also be a significant increase in traffic between the client and server systems.
 
Just do it in Javascript. It is simple to get working!
  Permalink  
Comments
SAKryukov at 11-Apr-12 15:44pm
   
Even though I'm not sure OP gets it (yes, the question shows complete confusion on how Web works; and my idea was that all further ASP.NET development could be pretty much useless for OP), I voted 5 and credited this answer in mine where I've shown the solution you might mean, but I also shown how server-based solution can be rendered working.
 
I must confess that I did not try to explain anything. And I also would not recommend my solution without timer -- it's just the fastest way to show how Web works, a kind of a hint... :-)
--SA
Espen Harlinn at 11-Apr-12 16:15pm
   
Nice reply :-D
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Now, I hope you got the explanation by OriginalGriff.
 
Here comes my answer:
 
You don't have to do it in JavaScript. Just add to your header:
<html>
	<head>
		<title>...</title>
		<meta http-equiv="refresh" content="0.7">
	</head>
<body>
 
<!- ... ->
 
</body>
</html>
Can you understand what difference does it make? If not, look for http-equiv and "refresh".
 
That was about your solution based on the HTTP response provided by the server side. You can do it purely on the client side, like OriginalGriff advised. It can look like this:
<html>
    <head>
        <title>...</title>
        <meta http-equiv="refresh" content="0.7">
    </head>
<body>
 
<p id="clock">Clock goes here</p>
 
<script type="text/javascript"><!--
function updateClock () {
  var currentTime = new Date ( );
  var currentHours = currentTime.getHours ( );
  var currentMinutes = currentTime.getMinutes ( );
  var currentSeconds = currentTime.getSeconds ( );
  currentMinutes = ( currentMinutes < 10 ? "0" : "" ) + currentMinutes;
  currentSeconds = ( currentSeconds < 10 ? "0" : "" ) + currentSeconds;
  var timeOfDay = ( currentHours < 12 ) ? "AM" : "PM";
  currentHours = ( currentHours > 12 ) ? currentHours - 12 : currentHours;
  currentHours = ( currentHours == 0 ) ? 12 : currentHours;
  var currentTimeString = currentHours + ":" + currentMinutes + ":" + currentSeconds + " " + timeOfDay;
  document.getElementById("clock").innerHTML = currentTimeString;
}
updateClock ();
--></script>
 
</body>
</html>
 
Can you understand how it works?
 
[EDIT]
 
The solution based on full refresh and "http-equiv" is in fact pretty bad. I've shown it just to give an illustration on how Web works, and what server side does in your case, as a hint. A real solution should be based on JavaScript timer. There are many places where you can find it; here is just one:
http://stackoverflow.com/questions/9247977/clock-in-javascript[^].
 
—SA
  Permalink  
v3
Comments
Espen Harlinn at 11-Apr-12 16:15pm
   
Good answer :-D
SAKryukov at 12-Apr-12 11:16am
   
Thank you, Espen.
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

I am going to give a small story for you.
 
Think about wall clock(not digital) hour,minute,second needle is there having individual gear for every needle.Responsibility of the gear is to move 1 step of respective needle after a successive completion of a 360 degree rotation.
So, every second, time is getting update is because of a function(stepping needle one step) and having time interval(gear rotation for time), right.

Like that think about (earth, 24hr revolution gives day and night),(heart beat) and so on.....
 
Now go through your code,apply missing function and time interval and make your clock alive.
  Permalink  
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 436
1 Maciej Los 249
2 BillWoodruff 199
3 /\jmot 180
4 Suraj Sahoo | Coding Passion 170
0 OriginalGriff 8,484
1 Sergey Alexandrovich Kryukov 7,407
2 DamithSL 5,639
3 Maciej Los 5,159
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web02 | 2.8.1411023.1 | Last Updated 12 Apr 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100