0

In an asp.net web application, is it possible to write a function, in a class on the server, that calls a client side javascript function which throws a prompt box then waits for the user's response?

I have a function that successfully calls the client javascript and shows the prompt box but the function doesn't wait for the response from the user, the code behind always keeps going. Is there a way to make it wait for the response?

Code Behind Function:

Protected Friend Shared Sub ShowDepthQuestion(sLaborId As String)
            Try
                Dim sQuestion As String = mDatabase.GetSingleValueString(" SELECT question FROM laboroverride_hdr WHERE hdr_id = " + mDatabase.AddQuotes(sLaborId), Nothing)                   
                Dim cs As ClientScriptManager = mFrom.ClientScript

                If Not mFrom.ClientScript.IsClientScriptBlockRegistered("devTeam") Then
                    cs.RegisterStartupScript(mFrom.GetType(), "devTeam", "<script language='javascript'>AskDepthQuestion(" + mDatabase.AddQuotes(sQuestion) + "); </script>")
                End If


                ' TODO need code to get the response from the user here

            Catch ex As Exception
                WebFunctions.UnhandledPageError(mFrom, ex, "Depth.Retrieve.Functions.ShowDepthQuestion")
                'Return ""
            End Try
        End Sub

Client Javascript Function:

    <script type="text/javascript">
    function AskDepthQuestion(question, context) {
        var ans = window.prompt(question, "0");
        while (!IsNumeric(ans)) {
            if (IsNumeric(ans)) {
                return ans;
            } else if (ans == null) {
                return ans;
            } else {
                ans = window.prompt("Answer must be numeric!" + "\n" + question, context);
            }
        }
    }

    function IsNumeric(n) {
        return !isNaN(parseFloat(n)) && isFinite(n);
    }

</script>

Any help on this is greatly appreciated. Thanks

1 Answer 1

1

You might have to use AJAX. Have your server code call the popup and have the popup call another method in the server side to process when the user submits. AJAX can do the client call to the server side. so your initial method would just bring up the popup and AJAX would call another method on the server to handle the submit.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks! I have made some changes to the code but I seem to be unable to hit the function in the code behind. Instead of returning the ans I call CallServer passing in the answer CallServer(ans);
function CallServer(sAns) { $.ajax({ type: "POST", url: "frmCrewList.aspx.vb/TimeOutDepth", data: "{}", contentType: "application/json; charset=utf-8", dataType: "json", success: function (msg) { if (msg.d == "Success") return true; else return false; } }); } The function called is in the code behind and declared as <WebMethod()> _ Public Sub TimeOutDepth()
Make sure the webmethod is declared static.
I declared it as Shared with no luck. Still not hitting the code behind.
get rid of the .vb from the url property, try url: "frmCrewList.aspx/TimeOutDepth"

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.