Daniel Sh.
Daniel Sh.

Reputation: 2074

ASPX not accessing Javascript.js

Doing some javascript for the first time. I'm playing around with a sessiontimeout and it was working well while I had the code within the .aspx page. Next stape was to put the code in a .js page. So here's my current lineup.

Script.aspx.js

    var iddleTimeoutWarning = null;
    var iddleTimeout = null;
function pageLoad() 
{     
    if (iddleTimeoutWarning != null)
        clearTimeout(iddleTimeoutWarning);
    if (iddleTimeout != null)
        clearTimeout(iddleTimeout);    
    var millisecTimeOutWarning = <%= int.Parse(System.Configuration.ConfigurationManager.AppSettings["SessionTimeoutWarning"]) * 60 * 1000 %>;
    var millisecTimeOut = <%= int.Parse(System.Configuration.ConfigurationManager.AppSettings["SessionTimeout"]) * 60 * 1000 %>; 
    iddleTimeoutWarning = setTimeout("DisplayIddleWarning()", millisecTimeOutWarning);
    iddleTimeout = setTimeout("TimeoutPage()", millisecTimeOut);
} 

function DisplayIddleWarning() 
{                
    document.getElementById("LblWarning").innerHTML = "Warning Message";
} 

function TimeoutPage() 
{                   
    __doPostBack('FiresAutoIdle','');
}  

ASPX page (Pretty sure error is here since the code in .js page works)

<asp:ScriptManager ID="ScriptManager1" runat="server">
        <Scripts>            
            <asp:ScriptReference Path="Scripts/Script.aspx.js"/>
        </Scripts>
</asp:ScriptManager>

I also have some code lines in web.config to set keys SessionTimeout and SessionTimeoutWarning.

So, any idea on where's the glitch? Things used to run, now they won't.

EDIT

  1. I'm using a script manager for a quick handling of the necessary ASP.NET AJAX components to enable partial postback. I need __doPostBack.
  2. I found out if a js function is named pageLoad(), the function will activate when the page loads or when a partial postback. Just what I need.

So, asp.nex page is not loading pageLoad() function from .js.

Upvotes: 0

Views: 298

Answers (1)

Daniel Sh.
Daniel Sh.

Reputation: 2074

Solved.

You can't set asp variables inside of javascript. So we have to set the variables in ASPX page, and then let javascript use them.

ASPX

<script type="text/javascript">
    var millisecTimeOutWarning = <%= int.Parse(System.Configuration.ConfigurationManager.AppSettings["SessionTimeoutWarning"]) * 60 * 1000 %>;
    var millisecTimeOut = <%= int.Parse(System.Configuration.ConfigurationManager.AppSettings["SessionTimeout"]) * 60 * 1000 %>; 
</script>

Then use them in .js file.

Upvotes: 1

Related Questions