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

发布于 2024-11-09 05:34:29 字数 936 浏览 0 评论 0原文

我正在尝试使用 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 其他地方< /a> 但我不太明白。

谢谢, 利亚姆

I'm trying to display the current date in an ASP.NET label using JavaScript and C#. Here's what I have:

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>

Can anyone see where I'm going wrong? Also, can the JS be run when the page loads without using the C# Page_Load method? I picked up the RegisterStartupScript elsewhere but I don't really understand it.

Thanks,
Liam

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

烟凡古楼 2024-11-16 05:34:29

尝试使用 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 线程,了解使用 < code>window.onload,或者考虑使用诸如 jQuery 及其 document.ready

Try using element.innerText = dt.toDateString(); instead.

Also, you don't need to use RegisterStartupScript. You can just call the getDate() function in the onLoad event.

<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>

However, you might want to read this SO thread for best practice in using window.onload, or consider using a framework such as jQuery and its document.ready

无风消散 2024-11-16 05:34:29

试试这个:

//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>

Try this:

//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>
倒带 2024-11-16 05:34:29

您可以将 PageLoad 更改为 OnPreInit,也可以将 JS 更改为

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

You can change Your PageLoad to OnPreInit and also you can change your JS to

<script type="text/javascript">
 function GetDate()
  {
     document.getElementById('<%Label1.ClientID%>').value = new Date().toDateString(); 
 }
 </script>
穿透光 2024-11-16 05:34:29

您可以简单地设置为

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

编辑

如果您不想在服务器端设置日期时间, 。您可以使用以下代码,无需任何 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>

You can simply set as

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

EDIT

If you don't want to set the DateTime in serverside. You can use the below code without any onload or RegisterStartupScript methods

<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>
余厌 2024-11-16 05:34:29

您可以使用 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");
    }

}

You can use pagemethods , so that you can Display Time Eventually

<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");
    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文