0

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);
        }
Amish Kumar
  • 259
  • 1
  • 3
  • 20

2 Answers2

0

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.

Community
  • 1
  • 1
Francis P
  • 13,377
  • 3
  • 27
  • 51
0

@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
Palli
  • 191
  • 2
  • My main purpose is not to display alert message. i have to do some calculation in the JavaScript and get the returned value on the page for further calculation. If u can describe in detail or if you have demo source code,please provide me.. Thanks – Amish Kumar Sep 28 '12 at 12:40
  • The sample code I posted was just to demonstrate how it is wired up, and you can expand it as you want, with additional information sent from the client etc. To demonstrate, [here](http://visualwebgui-samples.s3.amazonaws.com/public/KB/Custom%20Controls/RoundTripSample.zip) is a link to a full but simple sample: . It is a C# VS2008 sample, but if you need different version VS, this should be no problem. The sample creates a custom form with a JavaScript resource. You need to remember to register the custom form in web.config. – Palli Sep 30 '12 at 01:08
  • I am getting error "mobjApp is undefined" when running ur code. and the link u provided is not working in visual studio. But this can resolve my issue if this code will work. Thanks – Amish Kumar Oct 08 '12 at 13:01