Javascript 在Javascript中从ASP.Net的隐藏字段中获取值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11429603/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get value from hidden field of ASP.Net in Javascript
提问by Dharmendra Kumar Singh
<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 , i m 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 i m debugging i m getting error on this line .var digiclock = document.getElementById("<%= HiddenFieldMinutes.ClientID %>").value;
但是当我调试时,我在这条线上出错了。var digiclock = document.getElementById("<%= HiddenFieldMinutes.ClientID %>").value;
Please help me to improve this webpage and full fill my requirement.
请帮助我改进此网页并完全满足我的要求。
采纳答案by Ivan Doroshenko
The problem is in order of script and html elements. document.getElementById is called before html is rendered (DOM not ready yet). Just put script block after html.
问题在于脚本和 html 元素的顺序。在呈现 html 之前调用 document.getElementById(DOM 尚未准备好)。只需将脚本块放在 html 之后。
回答by Sunny
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.
希望对你有帮助。