Skip to content

How can I send a message from Javascript to C#? #377

Closed

Description

@itg-assistant

Antonio asked 2016-11-11 17:39:40 UTC

How can I send a message from Javascript to C# or VB?

For example, ...

In the client side I have a background process that I need to send any data to my current App.

In VWG I have a javascript function called 'SendMessage'

Public Sub SendMessage(ByVal str As String)
Dim strScript As String = String.Format("document.getElementById('TRG_{0}').contentWindow.SendMessageToComponent('{0}', '{1}');", Me.ID, str)
VWGClientContext.Current.Invoke(Me, "eval", New Object() {strScript})
End Sub

If I use this javascript  ..... SendMessage('wwwwwwwwwwwwwwwwwwwwwwwwwww');'

I receive in VB Funtion:

And in my code inside the App I have a Function that receive this data...

Public Sub ReceivePushData(d As String)

'here the d string is my data = 'wwwwwwwwwwwwwwwwwwwwwwwwwww'

End Sub

More simple... I need to comunicate something from client in javascript to my App in Vb.net or C#

Activity

  1. itg-assistant commented on Jun 12, 2026

    @itg-assistant
    Author

    Antonio answered 2016-11-11 18:25:57 UTC

    I have it... THANKS!

    https://wisej.com/docs/html/JavaScriptObjectModel.htm

  2. itg-assistant commented on Jun 12, 2026

    @itg-assistant
    Author

    Luca answered 2016-11-11 18:32:08 UTC

    Fantastic, a developer that reads the documents. :)

    You can either fire a message to a control or call a remote method.

    Best,

    Luca

  3. itg-assistant commented on Jun 12, 2026

    @itg-assistant
    Author

    Cristian Zerbinati answered 2018-03-18 12:55:03 UTC

    Beacause i'm old in desktop programming but I'm very newbie about javascript I have not total understand...

    example: if with javascript I change a button label with:

    this.setLabel("FOO");

    how I comunicate this to my vb app?

    thanks

    Cristian

  4. itg-assistant commented on Jun 12, 2026

    @itg-assistant
    Author

    Cristian Zerbinati answered 2018-03-18 19:54:46 UTC

    ok solved, I have do this override

    Protected Overrides Sub OnWebRender( ByVal config As Object)
    MyBase.OnWebRender(CObj(config))
    config.webMethods = {"test"}
    End Sub

    create the test sub

    <Wisej.Core.WebMethod>
    Public Sub test(testo As String)
    AlertBox.Show(testo)
    End Sub

    and call with this

    Eval("this.test(this.Button1.getLabel())")

    bye

    Cristian

  5. itg-assistant commented on Jun 12, 2026

    @itg-assistant
    Author

    Luca answered 2018-03-20 16:39:48 UTC

    Web methods are exposed automatically on top level controls (Desktop, Page, Form) and in the main application static class (Program). The methods on controls are instance methods, while the web methods in Program as static and accessible using the "App." object in javascript.

    The override you mention is correct and registers web methods on singe controls.

    However, the code you are showing seems to be circular. Eval(“this.test(this.Button1.getLabel())”). It's returning the label to the server by calling back a server methods when the server is calling the client. It seems that you want to read the label property from Button1 because it was changed from the client without a call to the server?

    There are a number of properties in Wisej widgets that are automatically wired back - when they change on the client they also change on the server. Specifically: size, location, value, ... depending on the widget. The label is not since it's not a value that typically changes outside of the server control.

    However, you can subscribe to the "changeLabel" event like this:

    Override OnWebRender (as you did) and add this:

    config.wiredEvents.Add("changeLabel(Text)");

    Override OnWebEvent and add a case:

    case "changeLabel":

    this.Text = e.Parameters.Text; break;

    Now every time the label changes wisej will fire an event to the server.

    You can also fire your own events using:

    this.fireEvent("myEventName");

    config.wiredEvents.Add("myEventName");

    or

    this.fireDataEvent("myEventWithData"), {name:'test', something: 12});

    config.wiredEvents.Add("myEventNameWithData(DataMember)");

    The data is assigned to the named property in parenthesis, whatever you name it (only one). Then on the server you will find it in e.Parameters.DataMember (or whatever you named it). It's a dynamic property. So in the case above you will have:

    e.Parameters.DataMember.name; // it's "test"

    e.Parameters.DataMember.something; // it's 12

    HTH. And this is all internal stuff and how all Wisej controls are built. Using a highly optimized JSON serializer, diffing engine, and dynamic objects all wired through events and callbacks.

  6. itg-assistant commented on Jun 12, 2026

    @itg-assistant
    Author

    Cristian Zerbinati answered 2018-03-20 19:14:48 UTC

    thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    GeneralQuestions, how-tos and everything else.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions