ErocM
ErocM

Reputation: 4662

WPF WebBrowser Issues

I am using the WebBrowser in WPF like this:

 <WebBrowser 
     Name="SSSBrowser" 
     Margin="8,4,8,8" 
     Grid.Row="1"            
     dp:WebBrowserUtility.BindableSource="{Binding WebAddress}"/>

And in C# I am loading it simply, for now, like this:

private string _webAddress;
public string WebAddress 
{
  get { return "http://www.somewebsite.com/updates/message/message.htm"; }
  set { _webAddress = value; }
}

What I would like to do is prevent it from displaying an error if they cannot reach the webpage, for whatever reason.

How do I keep tell if the website returned an error in code and disable the WebBrowser so that it doesn't give an error on screen to user?

Any help would be greatly appreciated!

Upvotes: 3

Views: 3024

Answers (4)

SliverNinja - MSFT
SliverNinja - MSFT

Reputation: 31651

You could try using the NavigationService from System.Windows.Controls.Frame as indicated in this MSDN forum post. The WebResponse will always be null for the WebBrowser control in WPF (as described in the post).

Upvotes: 0

JBRWilkinson
JBRWilkinson

Reputation: 4875

There doesn't seem to be an ErrorOpeningURL event for the WPF WebBrowser object, so, judging by previous experience, you could wire up to the Navigated event and check whether the URI is the IE error page (res://Error.html.. IIRC) or dig into the NavigationEventArgs for the WebResponse and check the headers.

To suppress the error, hide the WebBrowser component, perhaps by covering with a polite message - your user will want to know that the operation did not succeed, but doesn't have to see the navigation FAIL.

Upvotes: 0

hmadrigal
hmadrigal

Reputation: 1050

If I'm right Source property is not bindable because it's not a dependency property.

Check out this post:

databind the Source property of the WebBrowser in WPF

Herber

Upvotes: 0

Ta01
Ta01

Reputation: 31630

Not sure via WPF, but if you use HttpWebRequest and HttpWebResponse to programatically try to fetch your url first, the response will give you the http StatusCode, i.e. 200, 404 etc. Might be useful if you want to check for a 200 first and disable the browser preemptively. Not exactly the answer to your question, but a possibility.

Upvotes: 1

Related Questions