简体   繁体   English

服务器端函数调用客户端Javascript函数以获取结果

[英]Server Side Function Calling Client Side Javascript Function For Results

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? 在asp.net Web应用程序中,是否可以在服务器上的类中编写一个函数,该函数调用客户端javascript函数,该函数抛出一个提示框,然后等待用户的响应?

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. 我有一个可以成功调用客户端javascript并显示提示框的函数,但是该函数不等待用户的响应,其背后的代码始终保持不变。 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: 客户端Javascript功能:

    <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 谢谢

You might have to use AJAX. 您可能必须使用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. AJAX可以向服务器端进行客户端调用。 so your initial method would just bring up the popup and AJAX would call another method on the server to handle the submit. 因此您的初始方法只会弹出弹出窗口,而AJAX会在服务器上调用另一个方法来处理提交。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM