开发者

Display the current date in an ASP.NET label with C# and JS

开发者 https://www.devze.com 2023-03-08 11:16 出处:网络
I\'m trying to display the curren开发者_如何学Got date in an ASP.NET label using JavaScript and C#.Here\'s what I have:

I'm trying to display the curren开发者_如何学Got 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


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


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>


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>


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>


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

}
0

精彩评论

暂无评论...
验证码 换一张
取 消