用C#和JS在ASP.NET标签中显示当前日期

|| 我正在尝试使用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,但我不太了解。 谢谢, 利亚姆     
已邀请:
尝试改用
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>
但是,您可能想阅读此SO线程,以获取使用
window.onload
的最佳实践,或者考虑使用诸如jQuery及其
document.ready
之类的框架。     
尝试这个:
//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>
    
您可以将
PageLoad
更改为
OnPreInit
,也可以将JS更改为
<script type=\"text/javascript\">
 function GetDate()
  {
     document.getElementById(\'<%Label1.ClientID%>\').value = new Date().toDateString(); 
 }
 </script>
    
您可以简单地设置为
Label1.Text = DateTime.Now.ToString();
编辑 如果您不想在服务器端设置DateTime。您可以使用以下代码,而无需任何
onload
RegisterStartupScript
方法
<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>
    
您可以使用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\");
    }

}
    

要回复问题请先登录注册