简体   繁体   中英

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. Now i am invoking the java script method from code behind.

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

And javascript function is:--

function testCall(strGuid, txt) {

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

I want the value returned from JavaScript in the application. how can i achieve it. Is there in other method to invoke the methods of 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.

A solution could be to use AJAX to send that value to the server. Stack Overflow is full of answers about AJAX.

Here's a good example .

@Amish Kumar,

As noted by other replies already, the client-side and server-side are not directly connected in web programming. 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. 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. 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. 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. In the callback handler you use Form.DialogResult to get the user action.

A very basic way to make this work in custom Visual WebGui code could be like the following code on a 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. 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.

Palli

enter code here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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