简体   繁体   English

来自javascript的值未在Visual Web GUI中的应用程序中返回c#

[英]value from javascript is not returning to c# in the application in Visual Web GUI

I have written a java script function in the skin file of the visual web Gui application which returns some value too. 我已经在可视Web Gui应用程序的外观文件中编写了Java脚本函数,该函数也返回了一些值。 Now i am invoking the java script method from code behind. 现在,我从后面的代码中调用Java脚本方法。

public void XYZ( string message)
    {
        this.InvokeMethodWithId("testCall", message);
    }

And javascript function is:-- 而javascript函数是:-

function testCall(strGuid, txt) {

    alert("hai Java script fired..");
    return txt+ 'returned from JavaScript';
}

I want the value returned from JavaScript in the application. 我想要在应用程序中从JavaScript返回的值。 how can i achieve it. 我怎样才能做到这一点。 Is there in other method to invoke the methods of JavaScript? 是否有其他方法可以调用JavaScript方法?

I want something like this:-- 我想要这样的东西:-

public void Conect( string message)
        {
          string returnedvalue =  this.InvokeMethodWithId("testCall", message);
        }

Javascript is executed on the client so the return won't make it to the server. Javascript是在客户端上执行的,因此返回值不会到达服务器。

A solution could be to use AJAX to send that value to the server. 一种解决方案是使用AJAX将该值发送到服务器。 Stack Overflow is full of answers about AJAX. Stack Overflow充满了有关AJAX的答案。

Here's a good example . 这是一个很好的例子

@Amish Kumar, @阿米什·库玛(Amish Kumar),

As noted by other replies already, the client-side and server-side are not directly connected in web programming. 如其他答复所述,在Web编程中,客户端和服务器端未直接连接。 The client is always the initiator of every request, and the server-side's "purpose" is to render a response, which will then be returned to the client for processing, in Visual WebGui this is usually some UI update processing. 客户端始终是每个请求的发起者,服务器端的“目的”是呈现响应,然后将其返回给客户端进行处理,在Visual WebGui中,这通常是一些UI更新处理。 This basically means that your client script will not execute until the server-side has finished rendering the response, and the only way the client can get some message back to the server is to issue another request. 从根本上讲,这意味着您的客户端脚本将在服务器端完成呈现响应之前不执行,并且客户端可以将一些消息返回给服务器的唯一方法是发出另一个请求。

Think about how you need to use the MessageBox in Visual WebGui for instance. 例如,考虑一下您需要如何在Visual WebGui中使用MessageBox。 In order to receive the "response" from the MessageBox, you need to supply a callback handler in your server-side code, and then your server-side code will have completed creating the response, which is returned to the client. 为了从MessageBox接收“响应”,您需要在服务器端代码中提供一个回调处理程序,然后您的服务器端代码将完成创建响应,并将其返回给客户端。 The client updates its UI and on some action to the MessageBox dialog, it sends a new request to the server, which interpretes the action and invokes your callback handler. 客户端更新其UI,并在某些操作上将其更新到MessageBox对话框,它将新请求发送到服务器,服务器将解释该操作并调用您的回调处理程序。 In the callback handler you use Form.DialogResult to get the user action. 在回调处理程序中,您可以使用Form.DialogResult获取用户操作。

A very basic way to make this work in custom Visual WebGui code could be like the following code on a Form: 在自定义Visual WebGui代码中完成此工作的非常基本的方法可能类似于Form上的以下代码:

    private void button1_Click(object sender, EventArgs e)
    {
        SendClientMessage("This is a test");
    }

    public void SendClientMessage(string strMessage)
    {
        System.Text.StringBuilder sb = new StringBuilder();
        sb.AppendLine("var objEvent = mobjApp.Events_CreateEvent('{0}', 'MessageEvent');");
        sb.AppendLine("mobjApp.Events_SetEventAttribute(objEvent, 'Msg', '{1}');");
        sb.AppendLine("mobjApp.Events_RaiseEvents();");

        this.InvokeScript(string.Format(sb.ToString(), this.ID, strMessage));
    }

    protected override void FireEvent(Gizmox.WebGUI.Common.Interfaces.IEvent objEvent)
    {
        if (objEvent.Type == "MessageEvent")
            MessageBox.Show(objEvent["Msg"]);
        else
            base.FireEvent(objEvent);

    }

This code will not work unless you set your Visual WebGui applicaton for no Obscuring. 除非您将Visual WebGui应用程序设置为无遮挡,否则此代码将不起作用。 In order for this code to work on an obscured application, you would need to add the JavaScript as an obscured JavaScript resource and it would work fine. 为了使此代码在模糊的应用程序上运行,您需要将JavaScript添加为模糊的JavaScript资源,然后才能正常工作。

Palli 帕利

enter code here

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

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