4

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 を別の場所で取り上げましたが、よくわかりません。

ありがとう、リアム

4

5 に答える 5

3

element.innerText = dt.toDateString();代わりに使用してみてください。

また、使用する必要はありませんRegisterStartupScript。イベントでgetDate()関数を呼び出すだけです。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 スレッドを参照するか、 jQueryなどのフレームワークとそのwindow.onload使用を検討することをお勧めします。document.ready

于 2011-05-24T10:09:48.120 に答える
0

あなたはあなたPageLoadをに変更OnPreInitすることができ、またあなたはあなたのJSをに変更することができます

<script type="text/javascript">
 function GetDate()
  {
     document.getElementById('<%Label1.ClientID%>').value = new Date().toDateString(); 
 }
 </script>
于 2011-05-24T10:15:02.253 に答える
0

これを試して:

//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>
于 2011-05-24T10:13:26.030 に答える
0

簡単に次のように設定できます

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

編集

サーバーサイドで日時を設定したくない場合。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>
于 2011-05-24T10:18:39.193 に答える
0

最終的に時間を表示できるように、 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");
    }

}
于 2011-05-24T10:24:33.027 に答える