Click here to Skip to main content
15,887,294 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Updated 11-Apr-12 9:09am
v2
Comments
OriginalGriff 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.
Pr!y@ 11-Apr-12 15:13pm    
Oh Sir I was Not Shouting
And sorry if it was so
Sergey Alexandrovich Kryukov 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

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!
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 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 11-Apr-12 16:15pm    
Nice reply :-D
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
<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:
XML
<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
 
Share this answer
 
v3
Comments
Espen Harlinn 11-Apr-12 16:15pm    
Good answer :-D
Sergey Alexandrovich Kryukov 12-Apr-12 11:16am    
Thank you, Espen.
--SA
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.
 
Share this answer
 
v2

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900