admin管理员组文章数量:1389772
<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;
Please help me to improve this webpage and full fill my requirement.
<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;
Please help me to improve this webpage and full fill my requirement.
Share Improve this question edited Jul 11, 2012 at 9:59 Dharmendra Kumar Singh asked Jul 11, 2012 at 9:29 Dharmendra Kumar SinghDharmendra Kumar Singh 3,01112 gold badges49 silver badges66 bronze badges 8- can you paste rendered code for this hidden field? – Govind Malviya Commented Jul 11, 2012 at 9:31
-
Is that
HiddenField
is inside anyupdatepanel
?? – yogi Commented Jul 11, 2012 at 9:32 - Your syntax is correct , you may have not used proper id , view the source of your page or inspect element to find out the id. – Ravi Jain Commented Jul 11, 2012 at 9:33
-
You say it is a hidden field but is it actually a
<asp:HiddenField>
or is it another input that has it's visibility set? – Tim B James Commented Jul 11, 2012 at 9:43 - I had updated the question and briefly describe every thing with the code.Please check – Dharmendra Kumar Singh Commented Jul 11, 2012 at 10:00
2 Answers
Reset to default 0The 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.
I make one test which almost resembling your code,Just have a look here
<html xmlns="http://www.w3/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
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.
本文标签: Get value from hidden field of ASPNet in JavascriptStack Overflow
版权声明:本文标题:Get value from hidden field of ASP.Net in Javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744740593a2622584.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论