【问题标题】:Display the current date in an ASP.NET label with C# and JS使用 C# 和 JS 在 ASP.NET 标签中显示当前日期
【发布时间】:2011-05-24 10:05:22
【问题描述】:

我正在尝试使用 JavaScript 和 C# 在 ASP.NET 标签中显示当前日期。这是我所拥有的:

C#:

protected void Page_Load(object sender, EventArgs e)
{
    Page.ClientScript.RegisterStartupScript(Type.GetType("System.String"), "addScript", "GetDate()", true);
}

JS:

<script type="text/javascript">
    function GetDate() 
    {
        var dt = new Date();
        var element = document.getElementById("MainContent_FormView1_Label1");

        element.text = dt.toDateString();
    }
</script>

ASP.NET:

<asp:Label ID="Label1" runat="server" Text='<%# Bind("Date", "{0:d}") %>'></asp:Label>

谁能看出我哪里出错了?另外,不使用C# Page_Load 方法,页面加载时可以运行JS吗?我拿起了RegisterStartupScript elsewhere,但我不是很明白。

谢谢, 利亚姆

【问题讨论】:

    标签: c# javascript asp.net vwdexpress


    【解决方案1】:

    尝试改用element.innerText = dt.toDateString();

    另外,您不需要使用RegisterStartupScript。您可以在onLoad 事件中调用getDate() 函数。

    <script type="text/javascript">
    
        window.onload = function(){
            getDate();
        };
    
        function getDate() 
        {
            var dt = new Date();
            var element = document.getElementById("MainContent_FormView1_Label1");
    
            element.text = dt.toDateString();
        }
    </script>
    

    但是,您可能需要阅读 this SO thread 以了解使用 window.onload 的最佳实践,或考虑使用诸如 jQuery 及其 document.ready 之类的框架

    【讨论】:

    • @Liam 如果 Geoff 的回答解决了它,您应该将其标记为答案
    • @Liam。谢谢。你是对的。在您接受答案之前有很短的时间限制...
    【解决方案2】:

    试试这个:

    //C#
    protected void Page_Load(object sender, EventArgs e)
    {
        Page.ClientScript.RegisterStartupScript(this.GetType(), "addScript", "GetDate()", true);
    }
    
    //JS
    <script type="text/javascript">
        function GetDate() 
        {
            var dt = new Date();
            var element = document.getElementById("<%# Label1.ClientID %>");
    
            element.innerHTML = dt.toDateString();
        }
    </script>
    
    //ASP.Net
    <asp:Label ID="Label1" runat="server" Text=''></asp:Label>
    

    【讨论】:

    • 每当我尝试使用 时,该行(不是列号)都会出现“;预期”错误。知道这是为什么吗?
    • @Liam,你忘记了井号。它是 ,而不是 。
    【解决方案3】:

    你可以把你的PageLoad改成OnPreInit,你也可以把你的JS改成

    <script type="text/javascript">
     function GetDate()
      {
         document.getElementById('<%Label1.ClientID%>').value = new Date().toDateString(); 
     }
     </script>
    

    【讨论】:

      【解决方案4】:

      你可以简单地设置为

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

      编辑

      如果您不想在服务器端设置 DateTime。您可以在没有任何 onloadRegisterStartupScript 方法的情况下使用以下代码

      <asp:Label ID="label1" runat="server" />
      
      <!-- At the end of ASPX Page -->
      
      <script type="text/javascript">
      document.getElementById("MainContent_FormView1_Label1").innerHTML = new Date().toDateString()
      </script>
      

      【讨论】:

      • 我正在使用 Text 属性将此标签绑定到数据库。我认为我不能同时使用您的解决方案。不过还是谢谢。
      【解决方案5】:

      你可以使用pagemethods,这样你就可以最终显示时间

      <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
      <head>
          <title>jQuery to call page methods in ASP.NET</title>
          <script type="text/javascript" src="http://code.jquery.com/jquery-1.6.1.min.js">  </script>
      </head>
      <body>
          <form id="Form1" runat="server">
          <asp:ScriptManager EnablePageMethods="true" ID="ScriptManager1" runat="server">
          </asp:ScriptManager>
      
          <script type="text/javascript">
              $(document).ready(function() {
      
                  setInterval(setTime, 1000);
                  function setTime() {
                      PageMethods.getTime(function(res) {
                          document.title = res;
                          $('#time').html(res);
                      });
                  };
              });
          </script>
          Time : <span id='time'></span>
          </form>
      </body>
      </html>
      

      C#

      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Web;
      using System.Web.UI;
      using System.Web.UI.WebControls;
      using System.Web.Services;
      public partial class _Default : System.Web.UI.Page
      {
          protected void Page_Load(object sender, EventArgs e)
          {
      
      
          }
          [WebMethod]
          public static string getTime()
          {
              return DateTime.Now.ToString("hh:mm:ss tt");
          }
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-05-21
        • 1970-01-01
        • 2020-01-12
        • 2022-11-22
        • 2015-06-22
        • 1970-01-01
        相关资源
        最近更新 更多