ASP. NET Website real-time display time, asp.net website real-time display

Source: Internet
Author: User

ASP. NET Website real-time display time, asp.net website real-time display

This document describes how to display the time of an ASP. NET Website in real time. Share it with you for your reference. The specific method is as follows:

When developing and designing websites or network applications in an ASP. NET environment, you must display the current date and time in real time. In this case, the AJAX control is usually used.

Note that in. NET Framework 2.0, there is no AJAX Extensions control in the toolbox. In. NET Framework 3.5, AJAX is integrated.

ASP. net ajax consists of three parts:

① A client library or framework that extends the JavaScript function of the client;
② A Server programming and development extension package that allows ASP. net ajax to be well integrated into Visual Studio;
③ A toolbox developed and supported by the community.

On the server side, the AJAX extension package contains a few AJAX controls: ScriptManager, ScriptManagerProxy, Timer, UpdatePanel, and UpdateProgess.

The ScriptManager Control instructs the ASP. NET configuration engine to send a response to the client in AJAX mode and introduce the script library when the response is sent.

Note: Each ASP. NET Web form that supports AJAX must contain only one ScriptManager control.
UpdatePanel is a control container in a new Web form implemented by AJAX. Each ASP. NET Web form that supports AJAX can contain one or more UpdatePanel controls.

To display the time in real time, you only need to perform the following two steps:

1. Create a new Web form in the ASP. NET project named ShowCurrentTime. The front-end code is as follows.
Copy codeThe Code is as follows: <% @ Page Language = "C #" AutoEventWireup = "true" CodeFile = "ShowCurrentTime. aspx. cs" Inherits = "ShowCurrentTime" %>

<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> dynamic display of real-time </title>
</Head>

<Body>
<Form id = "form1" runat = "server">
<! -- The. net Framework 3.5 version must be used. The toolbox will only have the built-in AJAX Extensions. -->
<Div>
<Asp: ScriptManager ID = "ScriptManager1" runat = "Server"> </asp: ScriptManager> <! -- This control must be included; otherwise, UpdatePanel cannot be used -->
</Div>
<Table style = "position: absolute; margin-left: 200px; margin-right: 200px; margin-top: 100px; width: 270px; height: 78px; top: 15px; left: 10px; ">
<Tr>
<Td> dynamic display of real-time </td>
</Tr>
<Tr>
<Td style = "height: 100px;">
<Asp: UpdatePanel ID = "UpdatePanel1" runat = "server">
<ContentTemplate> the current time is:
<! -- The Lable and Timer controls must be included in the UpdatePanel control -->
<Asp: Label ID = "Label1" runat = "server" Text = "Label"> </asp: Label> <! -- Used to display the time -->
<Asp: Timer ID = "Timer1" runat = "server" Interval = "1000"> </asp: Timer> <! -- Used for update time, updated every 1 second -->
</ContentTemplate>
</Asp: UpdatePanel>
</Td>
</Tr>

</Table>
</Form>
</Body>
</Html>

2. In the ShowCurrentTime. aspx. cs file, you only need to add a line of code. The Code is as follows:
Copy codeThe Code is as follows: using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Web;
Using System. Web. UI;
Using System. Web. UI. WebControls;

Public partial class ShowCurrentTime: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{
Label1.Text = DateTime. Now. ToString ();

}
}

So far, the real-time display time function of the Label has been completed. In addition, you can set the time display style as needed.

If you only want to display the date, but not the time, you can use SubString to retrieve the previous date.

I hope this article will help you design your asp.net program.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.