简体   繁体   English

从Java的ASP.Net隐藏字段中获取价值

[英]Get value from hidden field of ASP.Net in Javascript

<body onload="timer()">
        <style type="text/css">
        #time{
        font-size:50pt;
        }
        #body 
        {
            background-color:#F3F3F3;
        }
        </style>
        <script type="text/javascript">
            var digiclock = document.getElementById("<%= HiddenFieldMinutes.ClientID %>").value;
            i = 0;
            function timer() {
                var digiformat = "";
                if (i > 3599) {
                    var H = Math.floor(i / 3600);
                }
                else {
                    var H = 0;
                }

                var M = i - (H * 3600)

                if (M > 59) {
                    M = Math.floor(M / 60)
                }
                else {
                    M = 0
                }
                var S = i - (M * 60)
                if (H < 10) {
                    H = "0" + H;
                }
                if (M < 10) {
                    M = "0" + M;
                }
                if (S < 10) {
                    S = "0" + S;
                }

                document.getElementById('time').innerHTML = H + ":" + M + ":" + S;
                setTimeout('timer()', 1000);
                i++;
            }
        </script>
            <table style="background-color:#F3F3F3;">
                <tr>
                <td><div><center><p style="font-family:Calibri;font-size:1.8em;color:#104E8B;">Total Elapsed Time</p> </center></div>
                </td></tr>
                <tr>
                <td><div id="time"><center>90</center></div>
                </td></tr>
                <tr>
                <td>
                <center>
                <form id="Form1" runat="server">
                <asp:HiddenField ID="HiddenFieldMinutes" runat="server" Value="" />
                    <asp:Button ID="btnStop" runat="server" Text="Stop" 
                        style="width:150px;height:30px;font-weight:bold;background-color:#104E8B;color:White;border:1px solid" 
                        onclick="btnStop_Click" /></form></center>
                        <input id="HiddenTaskname" type="hidden" value="123" runat="server" />
                    </td></tr>
            </table>

As you see above , im trying to create a clock which starts from specified time provided by the user. 如上所示,我试图创建一个从用户提供的指定时间开始的时钟。 I m storing the starting time period in the hidden field. 我将开始时间段存储在隐藏字段中。 The Code behind of this page load of this page is as follow:- 该页面加载到该页面后的代码如下:-

protected void Page_Load(object sender, EventArgs e)
    {
        HiddenFieldMinutes.Value = null;
        if (! IsPostBack)
        {
            //Checking for any query string
            if (Request["Code"] != null)
            {
                _elapsedNonProdTimeEntryID =Convert.ToInt32 (Request["Code"].ToString());
                _starttime = _nonProduction.GetStartTimeOfActiveTImeEntryID(_elapsedNonProdTimeEntryID);

                TimeSpan elapsedtimespan = System.DateTime.Now.Subtract(_starttime);
                string hh = elapsedtimespan.Hours.ToString();
                string mm = elapsedtimespan.Minutes.ToString();
                string ss = elapsedtimespan.Seconds.ToString();
                _differenceOfTimeSpan = hh + ":" + mm + ":" + ss;                    
                HiddenFieldMinutes.Value = _differenceOfTimeSpan;
                //ScriptManager.RegisterStartupScript(this, this.GetType(), "CloCkTImer", "javascript:timer(); ", true);                    
            }
        }
    }

But when im debugging im getting error on this line . 但是当我调试时我在这条线上得到了错误。 var digiclock = document.getElementById("<%= HiddenFieldMinutes.ClientID %>").value;

Please help me to improve this webpage and full fill my requirement. 请帮助我改进此网页并完全满足我的要求。

I make one test which almost resembling your code,Just have a look here 我做了一个几乎和您的代码相似的测试,请看这里

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
<script type="text/javascript" language="javascript">
    function timer() {
        alert(document.getElementById("<%= Hiddenfield1.ClientID %>").value);
    }
</script>
</head>
<body onload="timer();">
<form id="form1" runat="server">
<div>
    <asp:hiddenfield ID="Hiddenfield1" runat="server" value="static value"></asp:hiddenfield>
</div>
</form>

and here is CS code 这是CS代码

 protected void Page_Load(object sender, EventArgs e)
{
    Hiddenfield1.Value = "dynamic value";
}

it give the Dynamic value as output.So it means your way of getting values is fine i think there is problem something else like you are trying to check hidden field value on postback or on another event if so then please take hidden field in update panel then you will get hidden field value otherwise you will remain getting the same error. 它提供动态值作为输出。因此,这意味着您获取值的方式很好,我认为还有其他问题,例如您正尝试检查回发或其他事件中的隐藏字段值(如果有),请在更新面板中使用隐藏字段那么您将获得隐藏字段值,否则您将继续遇到相同的错误。

Hope it helps you. 希望对您有帮助。

The problem is in order of script and html elements. 问题是脚本和html元素的顺序。 document.getElementById is called before html is rendered (DOM not ready yet). 在呈现html之前调用document.getElementById(DOM尚未准备好)。 Just put script block after html. 只需将脚本块放在html之后。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM