简体   繁体   English

如何将值从共享函数VB.Net传递给Javascript

[英]how to passing value from shared function VB.Net to Javascript

I have shared function on code behind as follow: 我在后面的代码上共享函数如下:

Public Shared idEvaluator As Integer

<WebMethod()>
<ScriptMethod()>
Public Shared Function Evaluate(ByVal user As Evaluator)
    Try
        Using connection As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("BestEmployeeConnectionString").ConnectionString)
            connection.Open()
            Dim sql As String = "INSERT INTO PENILAI(IDEMPLOYEE,IDPERIOD) VALUES (@IDEMPLOYEE,@IDPERIOD);SELECT @IDEVALUATOR=SCOPE_IDENTITY()"
            Dim cmd As SqlCommand = New SqlCommand(sql, connection)
            cmd.Parameters.Add("@IDEMPLOYEE", SqlDbType.Int).Value = user.IDEMPLOYEE
            cmd.Parameters.Add("@IDPERIOD", SqlDbType.Int).Value = user.IDPERIOD
            cmd.Parameters.Add("@IDEVALUATOR", SqlDbType.VarChar, 100)
            cmd.Parameters("@IDEVALUATOR").Direction = ParameterDirection.Output
            cmd.CommandType = CommandType.Text
            cmd.ExecuteNonQuery()

            IDEVALUATOR= Convert.ToInt32(cmd.Parameters("@IDEVALUATOR").Value)   

                Return IDEVALUATOR

            End Using
        Catch __unusedException1__ As Exception
        Throw
    End Try
End Function

I just want to send value form IDEVALUATOR to javascript as follow: 我只想将值IDEVALUATOR发送到javascript,如下所示:

 $.ajax({ type: "POST", url: "Survey1.aspx/Evaluate", data: '{user: ' + JSON.stringify(user) + '}', contentType: "application/json; charset=utf-8", dataType: "json", success: function (response) { var t = '<%= IDEVALUATOR %>'; alert(t) } }); 

when run alerts always produce a value of 0.but when debugging on shared function then the idevaluator shows the correct value. 当运行警报时总是产生值0.但是当对共享函数进行调试时,idevaluator会显示正确的值。 so, how can I get the value from a shared function at code behind in javascript? 那么,如何从javascript中的代码后面的共享函数中获取值? thank you for your advice 感谢您的意见

Try just using response: 尝试使用响应:

$.ajax({
                type: "POST",
                url: "Survey1.aspx/Evaluate",
                data: '{user: ' + JSON.stringify(user) + '}',
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (response) {
                    var t = response;
                    alert(t)
                }
            });

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

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