如何在标签中显示当前时间 [英] How to show current Time in a label

查看:99
本文介绍了如何在标签中显示当前时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好

我的主页上有一个这个div

Hi guys

I have a this div in my master page

<div id="navbar2">
<ul>
<li><asp:Label ID="lblTime" runat="server" ></li>
</ul>
</div>



现在,我有了下面的代码来获取当前时间并将其放在标签中



Now I have the code below to get current time and put it in my label

protected void Page_Load(object sender, EventArgs e)
       {
           if (Page.IsPostBack)
           {
               DateTime dateTime = DateTime.Now;
               lblTime.Text = dateTime.ToShortTimeString();
               System.Windows.Forms.MessageBox.Show(dateTime.ToShortTimeString());
           }
       }


我目前收到一条错误消息,提示已使用相同的参数类型对名为"Page_Load"的成员进行了定义"
另外,我的当前时间在标签


I currently get an error saying "Already difines a member called ''Page_Load'' with the same parameter type"
Also my current time is not visible inside the label

推荐答案

中不可见.这里有很多问题:
1)不要在Web应用程序中使用MessageBox:它显示在服务器上,而不是客户端上,并且会使所有人烦恼!
2)您的类中已经有一个Page_Load方法-移动代码,使母版页文件仅包含一个.在解决错误之前,您将不会在标签中看到任何文本
3)当您确实执行此操作时,您将获得服务器时间,而不是客户端时间.这些可能有很大的不同!例如,我的虚拟主机比我晚7个小时...
There are a number of problems here:
1) Don''t use MessageBox in web applications: it shows at the server, not the client, and will annoy the heck out of everybody!
2) You already have a Page_Load method in your class - move your code so that the master page file contains only one. Until you fix the errors, you will not see any text in your label
3) When you do get this working, you will get server time, rather than client. These could be considerably different! For example, my web host is seven hours behind me...


lable.Text = DateTime.Now.ToString();
lable.Text = DateTime.Now.ToString();


您的页面加载方法也存在于aspx代码中.
页面成功加载后将显示当前时间.
如果您只想显示小时和分钟,也可以使用此代码
may be your page load method also exists in aspx code.
Current time will be shown when page successfully loaded.
you can also use this code if you want to show only hour and minutes
DateTime.Now.Hour+":"+DateTime.Now.Minute


这篇关于如何在标签中显示当前时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆