Wednesday, September 12, 2007

Fooling around with TWebbrowser #2

In the last episode of this serie (9 january 2006) we talked about the fact that we could use the TWebbrowser component to display simple webpages within our Delphi application.
Recently this project extended and was used to hold a complex ASP.NET webapplication, which gave some additional problems with the ASP.NET Session object. The problem is that this webapplication popups several windows (Internet Explorer windows) which looses the session of the webapplication.
As a result the user must login on every browser, which is a showstopper of course.

The cause of this is that the webserver sees our application as a different process compared to Internet Explorer. (ASP.NET sessions are process dependent) Different process, different session. The solution of this was rather simple, although a little hard to find.

How to retain the browser session within your application
The trick is to get the popup to run into the same process as our application, so basically to run it in our application.
TWebbrowser has a NewWindow2 event for this. In this event you can specify the application to be used throught the ppDisp (IDispatch) parameter. (our application, in this event)

Suppose you have a second form, Form2, containing another TWebbrowser, you could 'redirect' the new window like this:

procedure TForm1.WebBrowser1NewWindow2(ASender: TObject;
var ppDisp: IDispatch; var Cancel: WordBool);
ppDisp := Form2.WebBrowser1.Application;

Quite simple, isn't it?
To let our form act in the window-name resolution it is wise to set the RegisterAsBrowser property to true.

More information can be found on knAlertz:
How to maintain the ASP.NET session state
How to use the WebBrowser control NewWindow2 event in Visual Basic .NET


Anonymous said...

Thank you, this hint is very useful .. and can be used not only with asp pages ... :)

Anonymous said...

Have you tried to use TEmbeddedWB (enhanced TWebBrowser) ? There is also demo with tabbrowsing included.

Anonymous said...

Thanks a lot! I summed it up in just one single line of code, which creates a new tab and assign's that tab's browser:

ppDisp:= frmBrowser.NewPage.Web.Application;

Anonymous said...

Thank you!!!

Use an image as your UIBarButtonItem

Using an image as your UIBarButtonItem in your navigationcontroller bar can only be achieved by using a common UIButton as the BarButtonItem...