Link to home
Start Free TrialLog in
Avatar of mgmhicks
mgmhicks

asked on

JavaScript doesnt execute before code finishes

I have a page that loads checks for a session variable and responds in the following way

Master.myTitle = "Inspection Charges Report"
                    GetDates()
                    If IsDate(hfBeginDate.Value) Then
                        If IsDate(hfBeginDate.Value) Then
                            If InspectionChargesReport() Then
                            Else

                            End If

                        Else

                        End If
                    Else
                        ClientScript.RegisterClientScriptBlock(Me.GetType(), "ClientScript", "alert ('Invalid Begin Date') ", True)
                    End If

 Private Sub GetDates()

        Page.ClientScript.RegisterStartupScript(Me.GetType(), "Script", "jsInputBox();", True)

    End Sub

So what I dont understand and what is happening is that the code continues after getdates() sub, before the actual javascript has done its function, which is to bring up 2 prompt boxes asking for the dates.  How do I change my code to make it react the way I want and thats to get the dates before continuing.  My issue is that I want to run a report that takes 2 variable and begin and enddate and I have to ask user for those dates before continuing.  I really dont want to create a form just to enter 2 dates.  The javascript program attached does work correctly, just not triggering when I need it to.

 function jsInputBox() {
        var mBegin = prompt("Enter Begin Date", "");
        //  return (mBegin);
        var newBegin = document.getElementById("ctl00_ContentPlaceHolder1_hfBeginDate");
        newBegin.value = mBegin;
        //var txtCont = document.getElementById('<%= Page.FindControl("ContentPlaceHolder1").FindControl("txtContent").ClientID %>');
        var mEnd = prompt("Enter End Date:", "");
        //  return (mEnd);
        var newEnd = document.getElementById("ctl00_ContentPlaceHolder1_hfEndDate");
        newEnd.value = mEnd;
       
    }




thanks
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

What you need to understand is that anything client side would happen AFTER the response goes back to the client. That is after all the processing on the server side is finished and the page is rendered to the client. So your approach wont work.
Can you not add two textboxes on the page and a button where user puts the dates and presses the button and you do the further logic in that button?
Avatar of mgmhicks
mgmhicks

ASKER

Yes, and your the expert.  You've help me many times. Is that how you handle all your prompts for reports?  I was kinda trying to avoid that.  But if that is the best way to do things, then thats what I want to do.
SOLUTION
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sorry, I wrote above code directly here, so there are some errors. See attached example.
PopUp.zip