reference to control

i’ve got a web app with controls on a webpage. How can i reference them in the event? Simply e.g my webpage.label1.text=“blabla” returns NIL.


I don’t do Xojo web but if it is anything like Desktop, you can’t reference a control (or window) until it exists, and nothing yet exists in App.Open

ok, but the same code in an event (desktop app) works
Window1.Label1.text=“test” in sets the labeltext?

In event, I don’t think that you can unless your pages have implicit instance ON and even so, pages belong to a session.

Regardless, it seems (to me anyway) a bit strange that you would want to do this. Can you expand a bit on the need that you are trying to address and the process flow of the app at this point? Perhaps different strategies would work better to achieve the goal.

because you have Implicit Instance on, and Desktop at least will create the window (out of sequence if necessary under those conditions)

thanks, Dave. @Louis: this of course was just an example, i just want to know how to reference webpages in sessions so that i can change a control an that page e.g from a subclass.

instead of working with pages, create webcontainers that you will instanciate in any given page upon request. one of my larger web apps has only 2 pages: one login page and one blank page where webcontainers are called as required. The login process opens the second page, and calls the menu webcontainer. From the menu webcontainer, other webcontainers are then called as required. The app has a few dozens webcontainers, multiple webdialogs. In each case, an instance of the base object is used.

ok, for learning purposes…

i want to set a weblabel which is place on webpage1 to the contents of a a webrequest from app.HandleSpecialURL.

So in app.HandleSpecialURL event:

[quote]Select Case Request.Path
Case “test”
// modify my webpage1.weblabel
Dim s As String
s = Request.QueryString
webpage1.Label1.text=s //<- this is returning NIL
Return True
Case Else
// Don’t handle any other requests
Return False
End Select[/quote]

i guess i need to add the current session somewhere but what am i doing wrong?

Sorry I didn’t see this thread earlier…

You can’t access a control on a webpage from App.Open as no Sessions will have been created yet and this no page instances (or control instances) will have been created. In a desktop app, there is always one user… in a web app there’s initially zero.

thanks greg!

and what about my last question… is it possible with app.handleSpecialURL? can i modify controls on webpages from that event?

[quote=411624:@Marco Winster]ok, for learning purposes…

i want to set a weblabel which is place on webpage1 to the contents of a a webrequest from app.HandleSpecialURL.

i guess i need to add the current session somewhere but what am i doing wrong?[/quote]
App.HandleUrl handles a raw HTTP request with no knowledge of what a session is, but what you could do is place the sessionID into the request so you can get at it.

Here are two examples of ways to get data to a particular session. These are by no way the only ways.

  1. Store a dictionary somewhere globally in your project. I prefer a using a module, let’s say it’s called Globals.RequestData. When an http request comes in, you’d want some code like this:

dim d as dictionary = Globals.RequestData.Lookup(sessionID, Nil) d.value(“labeldata”) = newLabelData Globals.RequestData(sessionID) = d
Basically you’re actually storing a dictionary of dictionaries so that you can store an arbitrary amount of info on a per session basis. Next you use a timer on the WebPage which periodically checks for a new value and displays it using its action event.

dim d as dictionary = Globals.requestdata.lookup(Session.Identifier, Nil) If d = nil then return If d.lookup(“labeldata”, “”) <> Label1.textthen Label1.text = d.value(“labeldata”) End if

Im typing on my phone. Please be patient. There’s more coming…

  1. Another way to do this is by using a class called a WebSessionContext, but doing so requires a little explanation first.

At any given time, there can be one or more “Sessions” in existence in your app, only one of which has code that is currently “running” at any given time. The trick is that the Xojo Framework code doesn’t really know anything about your sessions and how they are structured so it basically uses the same system that you would to figure out which session is currently running code so commands are sent to the correct Session controls & Browser.

As I mentioned above, when dealing with an event that comes in at the App level by default there’s no information about which session it belongs to because there not be any. If you follow by advice to include the Session.Identifier that you are targeting in the request somehow, whether in the URL or as an additional parameter in the request, you can create what’s called a WebSessionContext object, which is a temporary link between your code and the Framework’s mechanism for figuring out which session is “current”.

dim context as new WebSessionContext(App.SessionWithIdentifier(identifier))

You don’t actually do anything with this object, except keep it around until the event ends.

More information can be found here: Page Not Found — Xojo documentation

This concept is great for doing quick calls into the framework, but breaks down if you have a lot of loops because loop boundaries sometimes cause thread context switches and you could suddenly be in another Session accession those controls instead.

Be very careful if you decide to go this route and make sure you test with more than two users actively accessing the web app.

Thanks again, Greg, for this detailed explanation! Unfortunately, i cant’ get it to work. Maybe i am missing something important how sessions are working. I really didn’t thought it was so difficult (for me) to simply access controls on webpages (with app.HandleSpecialURL)
I will try to get this working… you pointed me to the right direction :wink:

sorry for asking again…

so, now i am sending from a client to my webapp a call"

in my webapp there is the handlesepcialURL event :

[code]Select Case Request.Path
Case “test”
Dim Params() As String
Params = Split(Request.QueryString, “&”)
Dim SessionId As String
SessionId = Params(0).Replace(“SessionId=”, “”)
Dim context as new WebSessionContext(App.SessionWithIdentifier(sessionID))

how to reference from here to webpage?

Return True
Case Else
Return False
End Select[/code]

now context has the actual session, and then? How can i set the label on webpage1 to the value of e.g params(1), the labeltext?

anyone please?

You could simply say

if Session.CurrentPage IsA WebPage1 then WebPage1.label1.text = newvalue End if

…still returning a nilObjectException on if Session.CurrentPage IsA WebPage1

i only get this working by setting a variable in app.handlesepcialURL to the actual parameter and then use a timer on the webpage1 to set the labeltext to this variable. But isn’t there a solution without timer?

I just tried this by putting the following code in the HandleSpecialURL event:

Dim wsc As New WebSessionContext(app.SessionAtIndex(0)) If Session.CurrentPage IsA WebPage1 Then WebPage1.label1.Text = str(ticks) End If

WebPage1 has a single Label named Label1

Keep in mind, you MUST have a session running for any of this code to work.

Thank you, Greg! My problem was, that i always though i could pass the session ID with a self defined value (e.g 123) and that in combination with my “App.SessionWithIdentifier” MUST thrown always Nil when referring to objects…