从分配给Label的Javascript函数返回值Label的属性正在发生变化

时间:2013-02-21 09:25:12

标签: jquery asp.net

在Javscript函数中,我为Label分配了一个值。已分配值,但标签属性正在更改为默认值。

document.getElementById("Label1").innerText = dispTime;

其中 dispTime 是javascript函数中的某个值。

<asp:Label ID="Label1" runat="server" ToolTip="Current Time" ForeColor="BlueViolet" BorderStyle="Ridge" BackColor="ButtonHighlight"></asp:Label>

这是我的标签的定义方式,但在页面加载后,Label属性更改为默认值

我的Page_Load

ClientScript.RegisterStartupScript(GetType(), "Javascript", "javascript:InIt();", true);

我的javascript部分

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="head1" runat="server">
<title>Time</title>
<script type="text/javascript">
function InIt() {
    var dateTime = new Date();
    var hours = dateTime.getHours();
    var minutes = dateTime.getMinutes();
    var seconds = dateTime.getSeconds();
    var day;
    if (hours < 11) {
        day = "AM";
    }
    else {
        day = "PM";
    }
    if (minutes < 10) {
        minutes = "0" + minutes;
    }
    if (seconds < 10) {
        seconds = "0" + seconds;
    }
    var dispTime = document.getElementById("time").innerHTML = hours + ":" + minutes + ":" + seconds + " " + day;
    setTimeout("InIt()", 1000);
    var updatetime = document.getElementById("Label1");
    updatetime.innerText = dispTime;
}
InIt();
</script>

<form id="form1" runat="server">
<div id="time" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
    <asp:Label ID="Label1" runat="server" ToolTip="Current Time" style="background-color:ButtonHighlight; border-style:ridge;"></asp:Label>
</div>
</form>

4 个答案:

答案 0 :(得分:0)

在jquery的document.ready上分配值。

如果您设置java-script的值,如果您在.cs页面的页面加载上写了一些内容,则该值会再次更改。

答案 1 :(得分:0)

在页面加载事件上你可以使用这段代码调用javascript函数将值赋值给Label()然后在函数内添加脚本

 protected void Page_Load(object sender, EventArgs e)
{
    ScriptManager.RegisterStartupScript(Page, this.GetType(),  "Assign", "AssignvaluetoLabel();", true);
}


<html>
<head runat="server">
<title>Untitled Page</title>
<script >
function AssignvaluetoLabel()
{
var dispTime="some value";
document.getElementById("Label1").innerText = dispTime;

}
</script>
</head>

 <body>
<form id="form1" runat="server">
<div>
    <asp:ScriptManager  ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</div>
</form>
</body>
</html>

答案 2 :(得分:0)

在页面中添加jquery文件引用,尝试以下代码。

<asp:Label ID="Label1" runat="server" ToolTip="Current Time" ForeColor="BlueViolet" BorderStyle="Ridge" BackColor="ButtonHighlight"></asp:Label>
    <script language="javascript" type="text/javascript">
        $(document).ready(function () {
            TestFunction("testvalue");
        });

        function TestFunction(dispTime) {
            $("#<%= Label1.ClientID %>").text(dispTime);
        }
    </script>

答案 3 :(得分:0)

这是您更新的代码......

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="head1" runat="server"  >
<title>Time</title>
<script type="text/javascript">

function AssignvaluetoLabel() {
    var dateTime = new Date();
    var hours = dateTime.getHours();
    var minutes = dateTime.getMinutes();
    var seconds = dateTime.getSeconds();
    var day;
    if (hours < 11) {
        day = "AM";
    }
    else {
        day = "PM";
    }
    if (minutes < 10) {
        minutes = "0" + minutes;
    }
    if (seconds < 10) {
        seconds = "0" + seconds;
    }



  document.getElementById("Label1").innerText = hours + ":" + minutes + ":" + seconds + " " + day;

}

</script>
</head>
<body>

   <form id="form1" runat="server">
<div id="time" runat="server">
    <asp:ScriptManager runat="server">
    </asp:ScriptManager>
    <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
    <asp:Label ID="Label1" runat="server" ToolTip="Current Time" style="background-color:ButtonHighlight; border-style:ridge;"></asp:Label>
</div>
</form>
</body>

`

protected void Page_Load(object sender, EventArgs e)
{
    ScriptManager.RegisterStartupScript(Page, this.GetType(), "Assign", "AssignvaluetoLabel();", true);
}