Overview of Article
Continuing with our earlier post on adding custom controls from the System.Windows.Controls namespace, the following article describes how to add a WebBrowser control onto a LightSwitch Screen.
This will allow us to display HTML or load web pages onto a LightSwitch Screen.
Screens
This example is not based upon any data. Therefore, create a new screen using the ‘New Data Screen’ template and select ‘Screen Data (None)’.![]()
Fig 1 Create a screen based on no data
- In order for us to add a WebBrowser control onto the screen, we need to bind the control it to a notional piece of data. Therefore, click the ‘Add Data Item’ button and add a new local property of type string. Untick the ‘Is Required’ checkbox and name the control ‘prpWebPage’.

Fig 2 Add a local property called prpWebPage - Drag the control onto the screen. Change the control from a TextBox into a Custom Control. On the properties pane, click the ‘change’ hyperlink and select ‘System.Windows.Controls.WebBrowser’.
- In order to load a web page, we can use the ‘Navigate’ method on the WebBrowser control. However, we can only call this when the control is ready so we need to handle the ControlAvailable event. Click on the ‘Write Code’ button and select the ‘Activated’ event. Now write the following code:
Private Sub WebPage_Activated() AddHandler Me.FindControl("prpWebPage").ControlAvailable, AddressOf webControlAvailable End Sub Private Sub webControlAvailable(sender As Object, e As ControlAvailableEventArgs) CType(e.Control, System.Windows.Controls.WebBrowser).Navigate(New Uri("http://news.bbc.co.uk")) End Sub
Here’s the C# version….
private void WebPage_Activated() { this.FindControl("prpWebPage").ControlAvailable += webControlAvailable; } private void webControlAvailable(object sender, ControlAvailableEventArgs e) { ((System.Windows.Controls.WebBrowser)e.Control).Navigate(new Uri("http://news.bbc.co.uk")); }
Obviously, change the URL to the URL of your choice
- The properties of the control are now optionally modified to hide the label and to set the sizing to ‘stretch’. The alignment on the parent group can also be set to ‘top’ and ‘left’. If we now run the solution, here’s what appears.

Fig 4 Illustration of a loaded web page in LightSwitch - If we want to display some HTML rather than load a web page, we can call the NavigateToString method on the WebBrowser Control. Here’s some sample code.
Private Sub webControlAvailable(sender As Object, e As ControlAvailableEventArgs) Dim strHtml As String = "<h1>Hello World!</h1>" CType(e.Control, System.Windows.Controls.WebBrowser).NavigateToString(strHtml) End Sub
Here’s the same in C#
private void webControlAvailable(object sender, ControlAvailableEventArgs e) { string strHtml = "<h1>Hello World!</h1>"; ((System.Windows.Controls.WebBrowser)e.Control).NavigateToString(strHtml); }
- Here’s what appears when we now run the application

Fig 5 Illustration of displaying custom HTML in the WebBroswer Control
Conclusion
This post illustrates how to load web pages and HTML content onto a LightSwitch screen.
There are many useful scenarios in which this can be used. For example, we can use this technique to display HTML data from a database. The technique could also be used to connect to a SQL Server reporting server in order to display reports inside LightSwitch.



Pingback: Microsoft LightSwitch Printing Html « Tejana
Tejana says:
This page contains the webBrowser control, for details see Tim Leung’s Blog. Thanks Tim.
Hi Tim,
This is a great example, as are many of your posts, however I tried using it on a Web Client deployment vs. a Desktop deployment, and I received the error. “HTML is enabled only in Out-of-Browser mode.”
Is there some other way to host HTML content inside a LightSwitch application in a Non-Desktop deployment?
Rick –
I am faced with exactly the same issue with the “HTML is enabled only in Out-of-Browser mode” in a Web Client LS application.
Did you find a solution to your issue?
Any advice would be appreciated
Emma
Hi Rick,
Many thanks for your comment.
You’re correct that the example does not work in Web Client mode.
Unfortunately, I don’t think there’s an easy way to work around this.
From what I understand, SilverLight 5 will support the the WebBrowser control in ‘in broswer’ mode so I’m hoping that a future release of LightSwitch will provide this functionality
On another topic, one issue that Yann has kindly pointed out is that if the WebBrowser control is used to display HTML content with other controls added onto the screen, it isn’t possible to remove the vertical scrollbars against the HTML content. Therefore, the cosmetic appearance in this scenario isn’t ideal.
Tim
Pingback: 10th August « PRJ300 – Jimmy H
Mine is an example of a beginner being assigned an advanced task: I am embedding a web browser in my C# LightSwitch Contact Manager. I am following your example, and assume that on step 3, I am supposed to apply the WebPage_Activated code to the screen (WebTest). I get the following error:
“Type ‘LightSwitchApplication.WebTest’ already defines a member called ‘WebTest_Activated’ with the same parameter types.”
At this point, I am not sure if I applied the code in the correct place, or how to correct this problem. My project is currently using the delivered Microsoft LightSwitch Extension, and no others. Any guidance would be appreciated. Thanks.
Hi Maurice,
Yep – the _Activated code is added to the screen so what you’re doing should be correct.
Can you check that you don’t have the WebTest_Activated method defined twice in your code called?
Also, it might be worth checking that you haven’t created any other properties or methods on your screen called WebTest.
Tim
Thanks for replying so quickly.
As a test, I started from scratch with a blank project, no objects or entities at all, and I got the same error.
I went back to your instructions and saw that my prpWebPage control was not positioned in the screen hierarchy as your example. I had the control as a child of the Row Layout Group. Your example has it as a sibling. I rearranged mine and it worked, but with a blank row at the top of the screen. I moved the control back into the row and now it works exactly as I want.
I look forward to the day where I have gathered enough knowledge to understand why that worked! Thank you again for your advice (and excellent instructions) Have a good weekend.
@Maurice – Glad you got it working! Enjoy the rest of the weekend too.
Great tip on using HTML in your app… Thanks and keep up teh great work!
Hi Tim, thanks for the tip here. I have managed to show the webbrowser on LS, however it hide/blocks the other controls/pop-ups. Example a dropdown menu gets blocked by the webbrowser. Someone else is also facing the same problem, http://screencast.com/t/RmeLdqRm. Do you have an idea of where can I start?
how to navigate under hyperlink button click event in light switch extension shell
Thanks very much for posting this – it saved me a lot of time!
You’re a genius
Gave me an awesome idea
Pingback: MyBizz Portal: The “smallest” LightSwitch application you have ever seen (LightSwitch Star Contest entry). « Jan Van der Haegen's blog
Pingback: Windows Azure and Cloud Computing Posts for 1/23/2012+ - Windows Azure Blog