Switching visibilty of a control fed by Javascript causes exception
I have a counter on my page that displays the idle time left for a session.
The counter should not always be visible. But when hiding it an exception is thrown although the Javascript that feeds the counter should not be executed in this case:
The C# Code:
protected void Page_Load(object sender, EventArgs e)
{
if(DIV_Countdown.Visible == true)
ClientScript.RegisterStartupScript(this.GetType(), "Countdown", "SessionCountdown();", true);
The Javascript:
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<script type="text/javascript">
function SessionCountdown()
var seconds = 60;
$get("<%=LBL_Countdown.ClientID %>").innerHTML = seconds;
setInterval(function ()
seconds--;
$get("<%=LBL_Countdown.ClientID %>").innerHTML = seconds;
, 1000);
setTimeout(function ()
window.location = "http://essbp1s8000/RRHH/Default.aspx";
, seconds * 1000);
</script>
The markup:
<div id="DIV_Countdown" runat="server" visible="true">Session expires in <asp:label id="LBL_Countdown" runat="server"/ seconds.</div>
This works perfect until I switch DIV_Countdown.Visible to false.
JavaScript runtime error: Unable to set property 'innerHTML' of undefined or null reference
javascript c# asp.net
add a comment |
I have a counter on my page that displays the idle time left for a session.
The counter should not always be visible. But when hiding it an exception is thrown although the Javascript that feeds the counter should not be executed in this case:
The C# Code:
protected void Page_Load(object sender, EventArgs e)
{
if(DIV_Countdown.Visible == true)
ClientScript.RegisterStartupScript(this.GetType(), "Countdown", "SessionCountdown();", true);
The Javascript:
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<script type="text/javascript">
function SessionCountdown()
var seconds = 60;
$get("<%=LBL_Countdown.ClientID %>").innerHTML = seconds;
setInterval(function ()
seconds--;
$get("<%=LBL_Countdown.ClientID %>").innerHTML = seconds;
, 1000);
setTimeout(function ()
window.location = "http://essbp1s8000/RRHH/Default.aspx";
, seconds * 1000);
</script>
The markup:
<div id="DIV_Countdown" runat="server" visible="true">Session expires in <asp:label id="LBL_Countdown" runat="server"/ seconds.</div>
This works perfect until I switch DIV_Countdown.Visible to false.
JavaScript runtime error: Unable to set property 'innerHTML' of undefined or null reference
javascript c# asp.net
Did you try to use display style instead of Visible?
– Jitendra G2
Nov 13 '18 at 13:22
Jitendra, your hint got me on the right path. I can make it work using DIV_Countdown.Style.Add("display", "show"); to trigger display and if (DIV_Countdown.Style["display"] == "show") for the PageLoad check to start JavaScript. BUT: I still need an extra PostBack to display the Figure in LBL_Countdown. Any suggestion how to avoid that?
– Barnabeck
Nov 13 '18 at 23:08
add a comment |
I have a counter on my page that displays the idle time left for a session.
The counter should not always be visible. But when hiding it an exception is thrown although the Javascript that feeds the counter should not be executed in this case:
The C# Code:
protected void Page_Load(object sender, EventArgs e)
{
if(DIV_Countdown.Visible == true)
ClientScript.RegisterStartupScript(this.GetType(), "Countdown", "SessionCountdown();", true);
The Javascript:
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<script type="text/javascript">
function SessionCountdown()
var seconds = 60;
$get("<%=LBL_Countdown.ClientID %>").innerHTML = seconds;
setInterval(function ()
seconds--;
$get("<%=LBL_Countdown.ClientID %>").innerHTML = seconds;
, 1000);
setTimeout(function ()
window.location = "http://essbp1s8000/RRHH/Default.aspx";
, seconds * 1000);
</script>
The markup:
<div id="DIV_Countdown" runat="server" visible="true">Session expires in <asp:label id="LBL_Countdown" runat="server"/ seconds.</div>
This works perfect until I switch DIV_Countdown.Visible to false.
JavaScript runtime error: Unable to set property 'innerHTML' of undefined or null reference
javascript c# asp.net
I have a counter on my page that displays the idle time left for a session.
The counter should not always be visible. But when hiding it an exception is thrown although the Javascript that feeds the counter should not be executed in this case:
The C# Code:
protected void Page_Load(object sender, EventArgs e)
{
if(DIV_Countdown.Visible == true)
ClientScript.RegisterStartupScript(this.GetType(), "Countdown", "SessionCountdown();", true);
The Javascript:
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<script type="text/javascript">
function SessionCountdown()
var seconds = 60;
$get("<%=LBL_Countdown.ClientID %>").innerHTML = seconds;
setInterval(function ()
seconds--;
$get("<%=LBL_Countdown.ClientID %>").innerHTML = seconds;
, 1000);
setTimeout(function ()
window.location = "http://essbp1s8000/RRHH/Default.aspx";
, seconds * 1000);
</script>
The markup:
<div id="DIV_Countdown" runat="server" visible="true">Session expires in <asp:label id="LBL_Countdown" runat="server"/ seconds.</div>
This works perfect until I switch DIV_Countdown.Visible to false.
JavaScript runtime error: Unable to set property 'innerHTML' of undefined or null reference
javascript c# asp.net
javascript c# asp.net
asked Nov 13 '18 at 13:19
BarnabeckBarnabeck
127211
127211
Did you try to use display style instead of Visible?
– Jitendra G2
Nov 13 '18 at 13:22
Jitendra, your hint got me on the right path. I can make it work using DIV_Countdown.Style.Add("display", "show"); to trigger display and if (DIV_Countdown.Style["display"] == "show") for the PageLoad check to start JavaScript. BUT: I still need an extra PostBack to display the Figure in LBL_Countdown. Any suggestion how to avoid that?
– Barnabeck
Nov 13 '18 at 23:08
add a comment |
Did you try to use display style instead of Visible?
– Jitendra G2
Nov 13 '18 at 13:22
Jitendra, your hint got me on the right path. I can make it work using DIV_Countdown.Style.Add("display", "show"); to trigger display and if (DIV_Countdown.Style["display"] == "show") for the PageLoad check to start JavaScript. BUT: I still need an extra PostBack to display the Figure in LBL_Countdown. Any suggestion how to avoid that?
– Barnabeck
Nov 13 '18 at 23:08
Did you try to use display style instead of Visible?
– Jitendra G2
Nov 13 '18 at 13:22
Did you try to use display style instead of Visible?
– Jitendra G2
Nov 13 '18 at 13:22
Jitendra, your hint got me on the right path. I can make it work using DIV_Countdown.Style.Add("display", "show"); to trigger display and if (DIV_Countdown.Style["display"] == "show") for the PageLoad check to start JavaScript. BUT: I still need an extra PostBack to display the Figure in LBL_Countdown. Any suggestion how to avoid that?
– Barnabeck
Nov 13 '18 at 23:08
Jitendra, your hint got me on the right path. I can make it work using DIV_Countdown.Style.Add("display", "show"); to trigger display and if (DIV_Countdown.Style["display"] == "show") for the PageLoad check to start JavaScript. BUT: I still need an extra PostBack to display the Figure in LBL_Countdown. Any suggestion how to avoid that?
– Barnabeck
Nov 13 '18 at 23:08
add a comment |
1 Answer
1
active
oldest
votes
This sounds like a page lifecycle problem. If you are setting the visibility of the div on some postback method, it's too late for the Page_Load-event to see that. The Page_Load-event would have been executed before you set the visibility, hence the javascript has been registered already.
I recommend moving the script-block inside the div in the markup so that it can only run if the element is infact visible. That way all you need to worry about it the visibility of the div:
<div id="DIV_Countdown" runat="server" visible="true">
Session expires in
<asp:Label ID="LBL_Countdown" runat="server" />
seconds.
<script type="text/javascript">
function SessionCountdown()
var seconds = 60;
$get("<%=LBL_Countdown.ClientID %>").innerHTML = seconds;
setInterval(function ()
seconds--;
$get("<%=LBL_Countdown.ClientID %>").innerHTML = seconds;
, 1000);
setTimeout(function ()
window.location = "http://essbp1s8000/RRHH/Default.aspx";
, seconds * 1000);
SessionCountdown();
</script>
</div>
Also if you only need to use the label in the client side (not in server code) I would change that to a
<span id="myCountdown"></span>
It would make the javascript code much cleaner:
document.getElementById("myCountDown").innerHTML...
Yes it looked like a page lifecycle to me too, but I don't know how to fix it. I moved the scriptblock into the div, but as soon as I set DIV_Countdown.Visible to false an exception is raised: "JavaScript runtime error: The value of the property 'SessionCountdown' is null or undefined, not a Function object. Why and who calls the function at that point when the div is invisible???
– Barnabeck
Nov 13 '18 at 16:22
If you look at my example, it is calling the function at the end of the script-block. You need to remove the RegisterStartupScript-method call from your Page_Load if you use this method.
– Esko
Nov 14 '18 at 6:21
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53281913%2fswitching-visibilty-of-a-control-fed-by-javascript-causes-exception%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
This sounds like a page lifecycle problem. If you are setting the visibility of the div on some postback method, it's too late for the Page_Load-event to see that. The Page_Load-event would have been executed before you set the visibility, hence the javascript has been registered already.
I recommend moving the script-block inside the div in the markup so that it can only run if the element is infact visible. That way all you need to worry about it the visibility of the div:
<div id="DIV_Countdown" runat="server" visible="true">
Session expires in
<asp:Label ID="LBL_Countdown" runat="server" />
seconds.
<script type="text/javascript">
function SessionCountdown()
var seconds = 60;
$get("<%=LBL_Countdown.ClientID %>").innerHTML = seconds;
setInterval(function ()
seconds--;
$get("<%=LBL_Countdown.ClientID %>").innerHTML = seconds;
, 1000);
setTimeout(function ()
window.location = "http://essbp1s8000/RRHH/Default.aspx";
, seconds * 1000);
SessionCountdown();
</script>
</div>
Also if you only need to use the label in the client side (not in server code) I would change that to a
<span id="myCountdown"></span>
It would make the javascript code much cleaner:
document.getElementById("myCountDown").innerHTML...
Yes it looked like a page lifecycle to me too, but I don't know how to fix it. I moved the scriptblock into the div, but as soon as I set DIV_Countdown.Visible to false an exception is raised: "JavaScript runtime error: The value of the property 'SessionCountdown' is null or undefined, not a Function object. Why and who calls the function at that point when the div is invisible???
– Barnabeck
Nov 13 '18 at 16:22
If you look at my example, it is calling the function at the end of the script-block. You need to remove the RegisterStartupScript-method call from your Page_Load if you use this method.
– Esko
Nov 14 '18 at 6:21
add a comment |
This sounds like a page lifecycle problem. If you are setting the visibility of the div on some postback method, it's too late for the Page_Load-event to see that. The Page_Load-event would have been executed before you set the visibility, hence the javascript has been registered already.
I recommend moving the script-block inside the div in the markup so that it can only run if the element is infact visible. That way all you need to worry about it the visibility of the div:
<div id="DIV_Countdown" runat="server" visible="true">
Session expires in
<asp:Label ID="LBL_Countdown" runat="server" />
seconds.
<script type="text/javascript">
function SessionCountdown()
var seconds = 60;
$get("<%=LBL_Countdown.ClientID %>").innerHTML = seconds;
setInterval(function ()
seconds--;
$get("<%=LBL_Countdown.ClientID %>").innerHTML = seconds;
, 1000);
setTimeout(function ()
window.location = "http://essbp1s8000/RRHH/Default.aspx";
, seconds * 1000);
SessionCountdown();
</script>
</div>
Also if you only need to use the label in the client side (not in server code) I would change that to a
<span id="myCountdown"></span>
It would make the javascript code much cleaner:
document.getElementById("myCountDown").innerHTML...
Yes it looked like a page lifecycle to me too, but I don't know how to fix it. I moved the scriptblock into the div, but as soon as I set DIV_Countdown.Visible to false an exception is raised: "JavaScript runtime error: The value of the property 'SessionCountdown' is null or undefined, not a Function object. Why and who calls the function at that point when the div is invisible???
– Barnabeck
Nov 13 '18 at 16:22
If you look at my example, it is calling the function at the end of the script-block. You need to remove the RegisterStartupScript-method call from your Page_Load if you use this method.
– Esko
Nov 14 '18 at 6:21
add a comment |
This sounds like a page lifecycle problem. If you are setting the visibility of the div on some postback method, it's too late for the Page_Load-event to see that. The Page_Load-event would have been executed before you set the visibility, hence the javascript has been registered already.
I recommend moving the script-block inside the div in the markup so that it can only run if the element is infact visible. That way all you need to worry about it the visibility of the div:
<div id="DIV_Countdown" runat="server" visible="true">
Session expires in
<asp:Label ID="LBL_Countdown" runat="server" />
seconds.
<script type="text/javascript">
function SessionCountdown()
var seconds = 60;
$get("<%=LBL_Countdown.ClientID %>").innerHTML = seconds;
setInterval(function ()
seconds--;
$get("<%=LBL_Countdown.ClientID %>").innerHTML = seconds;
, 1000);
setTimeout(function ()
window.location = "http://essbp1s8000/RRHH/Default.aspx";
, seconds * 1000);
SessionCountdown();
</script>
</div>
Also if you only need to use the label in the client side (not in server code) I would change that to a
<span id="myCountdown"></span>
It would make the javascript code much cleaner:
document.getElementById("myCountDown").innerHTML...
This sounds like a page lifecycle problem. If you are setting the visibility of the div on some postback method, it's too late for the Page_Load-event to see that. The Page_Load-event would have been executed before you set the visibility, hence the javascript has been registered already.
I recommend moving the script-block inside the div in the markup so that it can only run if the element is infact visible. That way all you need to worry about it the visibility of the div:
<div id="DIV_Countdown" runat="server" visible="true">
Session expires in
<asp:Label ID="LBL_Countdown" runat="server" />
seconds.
<script type="text/javascript">
function SessionCountdown()
var seconds = 60;
$get("<%=LBL_Countdown.ClientID %>").innerHTML = seconds;
setInterval(function ()
seconds--;
$get("<%=LBL_Countdown.ClientID %>").innerHTML = seconds;
, 1000);
setTimeout(function ()
window.location = "http://essbp1s8000/RRHH/Default.aspx";
, seconds * 1000);
SessionCountdown();
</script>
</div>
Also if you only need to use the label in the client side (not in server code) I would change that to a
<span id="myCountdown"></span>
It would make the javascript code much cleaner:
document.getElementById("myCountDown").innerHTML...
answered Nov 13 '18 at 13:38
EskoEsko
3,14621230
3,14621230
Yes it looked like a page lifecycle to me too, but I don't know how to fix it. I moved the scriptblock into the div, but as soon as I set DIV_Countdown.Visible to false an exception is raised: "JavaScript runtime error: The value of the property 'SessionCountdown' is null or undefined, not a Function object. Why and who calls the function at that point when the div is invisible???
– Barnabeck
Nov 13 '18 at 16:22
If you look at my example, it is calling the function at the end of the script-block. You need to remove the RegisterStartupScript-method call from your Page_Load if you use this method.
– Esko
Nov 14 '18 at 6:21
add a comment |
Yes it looked like a page lifecycle to me too, but I don't know how to fix it. I moved the scriptblock into the div, but as soon as I set DIV_Countdown.Visible to false an exception is raised: "JavaScript runtime error: The value of the property 'SessionCountdown' is null or undefined, not a Function object. Why and who calls the function at that point when the div is invisible???
– Barnabeck
Nov 13 '18 at 16:22
If you look at my example, it is calling the function at the end of the script-block. You need to remove the RegisterStartupScript-method call from your Page_Load if you use this method.
– Esko
Nov 14 '18 at 6:21
Yes it looked like a page lifecycle to me too, but I don't know how to fix it. I moved the scriptblock into the div, but as soon as I set DIV_Countdown.Visible to false an exception is raised: "JavaScript runtime error: The value of the property 'SessionCountdown' is null or undefined, not a Function object. Why and who calls the function at that point when the div is invisible???
– Barnabeck
Nov 13 '18 at 16:22
Yes it looked like a page lifecycle to me too, but I don't know how to fix it. I moved the scriptblock into the div, but as soon as I set DIV_Countdown.Visible to false an exception is raised: "JavaScript runtime error: The value of the property 'SessionCountdown' is null or undefined, not a Function object. Why and who calls the function at that point when the div is invisible???
– Barnabeck
Nov 13 '18 at 16:22
If you look at my example, it is calling the function at the end of the script-block. You need to remove the RegisterStartupScript-method call from your Page_Load if you use this method.
– Esko
Nov 14 '18 at 6:21
If you look at my example, it is calling the function at the end of the script-block. You need to remove the RegisterStartupScript-method call from your Page_Load if you use this method.
– Esko
Nov 14 '18 at 6:21
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53281913%2fswitching-visibilty-of-a-control-fed-by-javascript-causes-exception%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Did you try to use display style instead of Visible?
– Jitendra G2
Nov 13 '18 at 13:22
Jitendra, your hint got me on the right path. I can make it work using DIV_Countdown.Style.Add("display", "show"); to trigger display and if (DIV_Countdown.Style["display"] == "show") for the PageLoad check to start JavaScript. BUT: I still need an extra PostBack to display the Figure in LBL_Countdown. Any suggestion how to avoid that?
– Barnabeck
Nov 13 '18 at 23:08