Getting page object values

For questions about Gambas web tools.
Post Reply
Posts: 841
Joined: Thursday 23rd July 2020 5:20pm

Getting page object values

Post by BruceSteers » Monday 18th July 2022 10:11am

Anyone know how to communicate and get return values from the pages objects?

For example my form has a WebAudio object.
I want to read/write the DOM property .currentTime

So from my gambas cgi web app i need to do something like this...

Dim iSeconds As Integer = WebForm1.GetValue("WebAudio1", "currentTime")

Only there is no GetValue command. :(

Posts: 841
Joined: Thursday 23rd July 2020 5:20pm

Re: Getting page object values

Post by BruceSteers » Monday 25th July 2022 9:03pm

So after some play i realize the following..
(Note: these are initial findings that i could be wrong about)

There must be 2 parts to a gambas web page, the gambas part and a .js file with your functions in it (if you want fancy functions).

When the gambas application runs on the server it basically converts itself into a bunch of javascript functions.

All the gambas type WebButton1_Click() events and the like become javascript functions from the lib.js file of gb.web.gui.
This ALL happens on load and all the client browser receives is the output html/javascript code.
Any other functionality needed after page load HAS to happen in your .js file. (make a file like myfunctions.js and place it in project dir)
this requires working out how Javascript works too (luckily i have some experience)
Forget that quote, i was wrong (see later post)

Examining the code of the lib.js file from gb.web.gui and the code of the controls is a great way to learn what's happening and doing the same.

So I CAN get object values and work with them but only if i do it in my own custom.js file.

The bad thing is that errors in the js file are not so easy to find. using firefox or chrome browsers dev tools can help a lot.
Last edited by BruceSteers on Saturday 30th July 2022 6:09pm, edited 1 time in total.

Posts: 841
Joined: Thursday 23rd July 2020 5:20pm

Re: Getting page object values

Post by BruceSteers » Wednesday 27th July 2022 7:39pm

Okay so for my particular task here i found the best thing was to make my own event.

Within my WebAudio.class I used something like the following...

Event TimeUpdate(Position As Float)

Private $hObserver As Observer
Private $fPosition As Float

Public Sub _new()

  $hObserver = New Observer(Me) As "This"


Public Sub This_TimeUpdate(Pos As Float)
  $fPosition = Pos

Public Sub _Render()
  Print "<audio"; Me._GetClassId(); " src=\""; Html(Me._GetLink($sAudio)); "\"";
  If $bLoop Then Print " loop=\"1\"";
  Print Me._GetEventJS("ontimeupdate", "TimeUpdate", "[$_(" & JS(Me.Name) & ").currentTime]")
  Print "></audio>";

Now in my WebForm i can use the following event...

Public Sub WebAudio1_TimeUpdate(Optional Position As Float)

  $fCurrentPosition = Position
  lblTime.Text = TimeToString(Position)


Doing this seems to work really well for various things.
i have a readable time position now in the WebAudio and also in the webform.

Hoping that'll help someone...


Posts: 841
Joined: Thursday 23rd July 2020 5:20pm

Re: Getting page object values [solved]

Post by BruceSteers » Thursday 28th July 2022 10:24am

Okay now with some very helpful advice from Benoit I have the following answer to my issue...

firstly a problem with using an event handler the way i was...
Benoit Minisini wrote: Note that you have a WebControl '_GetUpdateJS()' method that generates
for you the HTML code that calls 'gw.update' when a specific DOM event
is triggered.
So scrap that idea for that purpose as it was causing all my objects to refresh/flicker.
Benoit Minisini wrote: You must send the 'Position' property from the browser to the server by

gw.update(<id>, '<property>', <value>);

Where <id> is the id of the control, <property> the name of the
property, and <value> its value.

Then you handle that message on the server in the '_UpdateProperty'
hidden method of the control.

So this method works briliantly...

Event Position

Property Position As Float Use $fPosition

Public Sub _Render()

  Print "<audio"; Me._GetClassId(); " src=\""; Html(Me._GetLink($sAudio)); "\"";

  Print " ontimeupdate=\"gw.update(" & JS(Me.Name) & ",'position', $_(" & JS(Me.Name) & ").currentTime, null)\"";

  Print "></audio>";


Public Sub _UpdateProperty(sProp As String, vValue As Variant)

  If sProp = "position"
    $fPosition = vValue
    Raise Position


No more flickering.
the ontimeupdate event auto-sets $fPosition and raises the Position event.

One more quote from Benoit as it's important need-to-know stuff...
Benoit Minisini wrote: When a property of a control changes, you usually must refresh it by
calling the _Refresh() method.

You can avoid a full refresh of the control to speed up things by using
custom javascript code instead. Look at the SetText() method of the
WebTextBox control to see an example.

Another point: you must not catch an event on the browser side if you
don't implement the event handler on the server side.

To detect that, use the 'Object.CanRaise()' method. As usual, look at
the '_Render' method of WebTextBox for an example.
Many thanks to Benoit for his help and hopefully this can help you too.


Post Reply