0

I have the following JS code which shows/hides buttons (had to be done this way, and please don't say do it another way).

Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(beginRequestHandle);
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(endRequestHandle);
function beginRequestHandle(sender, Args) {
                document.getElementById("ltInstructions").style.visibility = "hidden";
                document.getElementById("btnSubmit").style.visibility = "hidden";
                document.getElementById("btnToExcel").style.visibility = "hidden";
            }
function endRequestHandle(sender, Args) {
                    if(<%=resultsCount %> > 0)
                    {
                        document.getElementById("ltInstructions").style.visibility = "visible";
                        document.getElementById("btnSubmit").style.visibility = "visible";
                        document.getElementById("btnToExcel").style.visibility = "visible";
                    }
                    else
                    {
                        document.getElementById("results").innerHTML = "<br><b><center><font size=20>No results found, please try again.</b></font></center>";
                    }
                }

The problem is <%=resultsCount %> which gets initialized to 0 in the code behind and then later updated in Timer_Tick method. The Js above always picks it up as 0.

How to make the JS pick it up as the correct value?

2 Answers 2

1

Use asp:hiddenfield instead and change its value in Timer_Tick, You will get updated value for it.

In HTML

 <asp:HiddenField id="resultsCount" runat="server" Value="String" />

In Javascript

resultsCount = document.getElementById('<%= resultsCount.ClientID %>').value;
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. That has fixed it :)
0

Try looking at the source of the webpage in your browser. You have to write the timer in JavaScript, because <%=resultsCount %> is evaluated once only and therefore, all the client sees is

 if (0 > 0) {
   ...
 }

You can implement a timer in JavaScript using the setInterval and clearInterval methods. Define a function update() that should be called whenever something needs to be updated and then do

var updateInterval

function update() {
  if(condition) {
    /* update stuff */
  } else {
    /* no more updates needed */
    clearInterval(updateInterval) /* stop updating */
  }
}

updateInterval = setInterval(update, 1000) /* call update() every 1000 ms, that is every second */

1 Comment

yes that is correct I do get 0 > 0. How to perform the timer in JS?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.