How to call method of web page from container

I am trying to call a method of a WebPage from a WebContainer. I thought something like

MainPage.ChangeContainer

would work. The autocomplete for MainPage only shows the controls on the page, none of the methods.
ChangeContainer is a public method on MainPage.
Xojo 2021r2

Is the webpage an implicit instance? then you can just say

myWebPage.methodToCall()

If you instantiate the webpage with something like:

var myPage as new myWebPage
myPage.show

Then you’ll either have to set a reference to a globally accessible place like the session object it:

var myPage as new myWebPage
Session.myPageInstance = myPage // myPageInstance is a property which you have to create first
myPage.show
//
Session.myPageInstance.methodToCall()

Or if the container you call from is part of the Webpage, than you can use the .Parent property:

if self.parent isa myWebPage then
   var myPage as myWebPage = myWebPage(self.parent)
   myPage.methodToCall()
end if

Always keep in mind, that you can easily create circular references here, so have an eye on that. Otherwise you produce memory leaks, which are hard to find.

If you set the reference in session.myPageInstance, then make sure you set it to nil, when the webpage closes.

Thanks Lars

1 Like

Hi Lars,
The direct approach doesn’t work. Calling myWebPage.methodToCall() does not work since the webpage is running on a different thread. It triggers a nilobjectexception.

Can you include how to get the session from the WebApp object?

Which session do you want to get? The current, wich runs the current user? Then it should simply be Session.whatevermethod.

If you want to have any session, which runs on the app, then you should have an eye on WebSessionContext first.

The method described in this thread is used, if you want to call something from within a ui control. So you’re already underneath the session and its properties.

Thanks for your reply. There are several of us trying to learn xojo and the lack of documentation, disjoint, and out of documentation is taxing. I don’t understand what you mean by Session.whatevermethod. Reading some of the documentation, gave the impression that the WebApplication object kept track of the session. How would I add a method to the global Session object? My understanding is that the current session is passed from the webpage to a property that is accessible by the WebApplication object. Can you explain this better for the newbies?

What do you try to achieve? It seems that you try a different thing than the OP asked for.

But to answer some of your questions:

  • The Session object is unique for every connected user. Sessions cannot interfere. Every value of a property is only set for the specific session, is therefore usable by the connected user, but not from within the WebApplication object.
  • The WebApp object is global, which means values of properties are equal for every user. If user 1 changes a thing here, user 2 and 3 will see that too.
  • Having methods on the session or the app object is not much different. Only for the data you save in properties/variables you have to think about the place where you store it.
  • If you want to have the root webpage of the current user saved in one place to access it, you need to save it at the session. If you set the variable to the web object, it would be overwritten when ever another user set this variable too.

I’m trying to get the context for the current webpage that allows me to update the text for a webcontrol. I have tried all of the suggestions you mentioned in above in my App.somemethod() but they all fail with some type of exception. I’m trying achieve in the webapp object:
<App.somemethod()
Check for WebSession
Get context to WebSession
Get WebPage handle
Update WebPage.WebControl.Text
/>
Do you have any complete code that demonstrates how to do this?
I have even tried to get the WebSessionContext by using:
Var sessionContext As WebSessionContext
sessionContext = New WebSessionContext(App.SessionForControl(Self))
This fails because it needs Self to be a WebControl.