Asp.net implements dynamic display of the current time (without javascript overhead)

Source: Internet
Author: User

Default. aspx page: first pull a ScriptManager control to the page, and then pull an UpdatePanel control. Put a Label in UpdatePanel to display the time, and put a timer control to control the update of the time. Note that both the Label and Label must be placed in the UpdatePanel control. Finally, set the Interval attribute of the timer control to 1000 so that it can be executed once every one second, that is, the update time.
Default. aspx. cs page:
Protected void Page_Load (object sender, EventArgs e)
Enter
Label1.Text = DateTime. Now. ToString ();
You can.
The source code of the two pages is as follows:
Default. aspx Copy codeThe Code is as follows: <% @ Page Language = "C #" AutoEventWireup = "true" CodeBehind = "Default. aspx. cs" Inherits = "dynamic display of real-time. _ Default" %>
<! 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>
<Style type = "text/css">
. Style1
{
Height: 183px;
}
</Style>
</Head>
<Body>
<Form id = "form1" runat = "server">
<Div> <asp: ScriptManager ID = "ScriptManager1" runat = "server">
</Asp: ScriptManager>
</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 class = "style1"> <asp: UpdatePanel ID = "UpdatePanel1" runat = "server">
<ContentTemplate>
The current time is:
<Asp: Label ID = "Label1" runat = "server" Text = "Label"> </asp: Label>
<Asp: Timer ID = "Timer1" runat = "server" Interval = "1000">
</Asp: Timer>
</ContentTemplate>
</Asp: UpdatePanel>
</Td> </tr>
</Table>
</Form>
</Body>
</Html>

Default. aspx. csCopy codeThe Code is as follows: using System;
Using System. Collections;
Using System. Configuration;
Using System. Data;
Using System. Linq;
Using System. Web;
Using System. Web. Security;
Using System. Web. UI;
Using System. Web. UI. HtmlControls;
Using System. Web. UI. WebControls;
Using System. Web. UI. WebControls. WebParts;
Using System. Xml. Linq;
Dynamic Display of real-time namespace
{
Public partial class _ Default: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{
Label1.Text = DateTime. Now. ToString ();
}
}
}

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.