Display current date on ASP.NET label using C # and JS

I am trying to display the current date on an ASP.NET label using JavaScript and C #. Here is what I have:

WITH#:

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 wrong? Also, can JS run when the page loads without using the C # Page_Load method? I took RegisterStartupScript elsewhere , but I really don't understand it.

Thanks Liam

+3
source share
5 answers

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

In addition, you do not need to use RegisterStartupScript. You can just call the function getDate()in the event onLoad.

<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

+3

:

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

You can change your PageLoadto OnPreInitas well as change your JS to

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

You can just set how

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

EDIT

If you do not want to set DateTime in serveride. You can use the code below without methods onloadorRegisterStartupScript

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

You can use pagemethods so you can display time over time

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

WITH#

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
source

All Articles