ASP.net在网页上显示当前时间,利用AJAX不刷新网页
前台页面代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_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>当期日期时间</title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Timer ID="Timer1" runat="server" Interval="1000" OnTick="Timer1_Tick">
</asp:Timer>
<asp:Label ID="lblTime" runat="server" Text=""></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>
后台CS代码:
using System;
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;
using System.Globalization;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ } /// <summary>
/// 事件发生改变
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Timer1_Tick(object sender, EventArgs e)
{
//第一种方法获取星期几
//string[] str1 = { "星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日" };
//string str2 = DateTime.Now.ToString("tt") == "上午" ? "AM" : "PM";
//lblTime.Text = "当前时间为:" + DateTime.Now.ToString("yyyy年MM月dd日") + " " +
// str1[Convert.ToInt32(DateTime.Now.DayOfWeek)] + " " + DateTime.Now.ToLongTimeString()+;
//第二种方法获取星期几
string str = DateTime.Now.ToString("tt") == "上午" ? "AM" : "PM";
lblTime.Text = "当前时间为:" + DateTime.Now.ToString("yyyy年MM月dd日") + " " +
CultureInfo.GetCultureInfo("zh-cn").DateTimeFormat.GetDayName(DateTime.Now.DayOfWeek) + " " +
DateTime.Now.ToLongTimeString() + " " + str;
}
}
相关知识:
·UpdatePanel内放置需要被刷新的控件,如果是其内部控件事件导致其刷新,则不用另外做什么设置,因为UpdatePanel默认ChildrenAsTriggers="true"。
·如果是UpdatePanel外部控件导致其刷新的话,则应设置 Triggers。
·在Triggers内,如果AsyncPostBackTrigger未设置EventName,则为其指定控件的默认事件。
·UpdatePanel默认UpdateMode="Always",需要的话应设置UpdateMode="Conditional"。
·RenderMode="Block"对应div;RenderMode="Inline"对应span
举例:定时器控件Timer2在UpdatePannel外面,则设置如下,见黄色部分:
<asp:scriptmanager ID="Scriptmanager1" runat="server"></asp:scriptmanager>
<asp:updatepanel ID="Updatepanel1" runat="server">
<ContentTemplate>
<asp:Timer ID="Timer1" Interval="1000" runat="server" ontick="Timer1_Tick" >
</asp:Timer>
<asp:Label ID="Label1" runat="server" Text=""></asp:Label>
<p></p>
<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Timer2" EventName="Tick" />
</Triggers>
</asp:updatepanel>
</div>
<asp:Timer ID="Timer2" runat="server" Interval="3000" ontick="Timer2_Tick">
</asp:Timer>
推荐阅读:
ASP.net在网页上显示当前时间,利用AJAX不刷新网页的更多相关文章
- JavaScript总结-网页上显示时间
网页上显示时间 代码如下: <TABLE> <TR> <TD><span id="localtime"></span> ...
- JS文件中的中文在网页上显示为乱码解决方法
转自:http://www.pc6.com/infoview/Article_63835.html 如果JS文件中的中文在网页上显示为乱码,不妨采用本文章中的方法来试一试,或许能解决使你很头疼的问题. ...
- <body>标签,网页上显示的内容放在这里
在网页上要展示出来的页面内容一定要放在body标签中.如下图是一个新闻文章的网页. 在浏览器中的显示效果: 示例: <!DOCTYPE HTML> <html> <hea ...
- [转载].NET ASP.NET 中web窗体(.aspx)利用ajax实现局部刷新
之前开发的一套系统中用到了大量的 checkboxList 控件,但是每次选定之后都会刷新整个页面,用户体验很差,百度了之后查到这篇文章,尝试了一下可以实现,所以转载了过来,记录一下,也给其他有相同困 ...
- asp.net在网页上显示数据库中的数据
第一步: 第二步: 第三步: 第四步:在网页代码中写显示格式代码,如下 <asp:SqlDataSource ID="SqlDataSource1" runat=" ...
- 使用js在网页上显示时间
<html> <script> function getDate(){ var d,s,t; d = new Date(); s = d.getFullYear().toStr ...
- 如何在网页上显示html代码?
a: 把代码写在文本区域 <textarea> 标签中.可以设置 disabled="disabled" 属性,禁止用户操作.b: 把要显示在html文档中标签的 &q ...
- 设计Web程序,计算任意两个整数的和,并在网页上显示结果。要求在javabean中实现数据的求和功能。
<%--提交页面input.jsp--%> <%@ page language="java" import="java.util.*" pag ...
- psd图片不能在网页上显示
原因:web上不支持psd图片,web支持JPG,PNG等. 解决:打开ps点击文件--储存为web所用格式(选择转换成哪种格式).
随机推荐
- 基于jQuery个性圆圈倒计时特效
基于jQuery个性圆圈倒计时特效里面包含十几款不用效果的jQuery倒计时特效下载.效果图如下: 在线预览 源码下载 实现的代码. html代码: <section class=" ...
- docker探索-docker安装运行tomcat(六)
前言 本地已经搭建好了,tomcat容器,本来想自己写一篇,在开源中国一篇不错的教程,就转载过来了 本文转自:https://my.oschina.net/sunchp/blog/61652 ...
- winsock.h与winsock2.h出现重定义或不同的链接
经常遇到编译socket程序的时候生成几百个错误 以下是出错后的错误信息: >c:\program files\microsoft sdks\windows\v6.0a\include\ws2d ...
- 再谈git的http服务-权限控制hooks版
通过git-http-backend方法提供的http服务基本上可以做到认证用户才能使用,但只能控制到服务器路径访问,而且无法区分读写.经过不懈努力,找到了方法,相关脚本及配置见后. 基本思路就是利用 ...
- Remove 以及dorp做实验验证MongoDB删除文档后索引是否会自动删除
下面是实验步骤: > db.things.find(){ "_id" : ObjectId("5652d71a1524dc14663060e8"), &q ...
- HTML Notes
Label tag and input in form <form action=""> <label for="male">Male& ...
- Android安装后没有完成和打开按钮
File apkFile = new File(filePath); Intent intent = new Intent(); intent.setFlags(Intent.FLAG_ACTIVIT ...
- js动态加载图片核心代码
objMapHeart.src = obj.getAttribute("ItemImage"); //==============图片预加载处理================== ...
- [hadoop读书笔记] 第十五章 sqoop1.4.6小实验 - 将mysq数据导入hive
安装hive 1.下载hive-2.1.1(搭配hadoop版本为2.7.3) 2.解压到文件夹下 /wdcloud/app/hive-2.1.1 3.配置环境变量 4.在mysql上创建元数据库hi ...
- C++ operator关键字详解
C++中的operator主要有两个作用,一是操作符的重载,一是自定义对象类型的隐式转换. 类型转换操作符(type conversion operator)是一种特殊的类成员函数,它定义将类类型值转 ...