WPF WebBrowser Issues - c#

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!

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.

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

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.

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).

Related

Navigating with Gecko Web Browser in C#

I'm using Geckofx 33.0.9.0 in my C# application, and I'm having a problem with navigating.
Generally it works well, I enter a URL and it takes me to a page. The problem is that if I enter a URL for a page that doesn't exist, it gives me a MessageBox titled 'Alert'.
1) This is really annoying. At least in my Opinion
2) I would really like to set it up, so if I navigate to a page that doesn't exist, it creates a google search from my url, like in most proper browsers.
I tried looking but Gecko isn'T really well documented, or at least I couldn't find it (Though if someone has documentation for it, that would be great!) and I couldn't find any other way of navigating other then the .Navigate('String Url/Uri') method.
What can I do to circumvent this Alert box? Is there a way?
I'm creating the GeckoWebBrowser Control in code btw.
I will of course post code if required.
to supress the alert message box you can try the following
Xpcom.Initialize(Paths.XulRunner);
GeckoPreferences.User["browser.xul.error_pages.enabled"] = false;
GeckoPreferences.User["browser.download.manager.showAlertOnComplete"] = false;
GeckoPreferences.User["security.warn_viewing_mixed"] = false;
GeckoPreferences.User["privacy.popups.showBrowserMessage"] = false;
As for the second idea, well, that shouldn't be very hard - you just need to check in the DocumentCompleted or NavigationFinished event for a page status - if it's not correctly loaded, then take the address string bit and redirect to google url + your string.
Hope this helps!

WebBrowser.url not working some times in winform

I set url value of a WebBrowser object on an event. Sometimes setting up of this value is not getting reflected in webBrowser.
The code I used is
webBrowser.Url= new Uri("www.google.com")
I also tried webBrowser.Navigate() but same behavior. Any ideas why it could be happening and what to do to overcome this?
Although very old question, for people visiting this, please check AllowNavigation property set on the WebBrowser instance.
As per MSDN documentation -
This property does not prevent you from loading an initial page by
setting the Url, DocumentText or DocumentStream property, but will
prevent all subsequent navigation.
You can find more details here.

Click Gmail Refresh button

I am writing a simple personal app that has a browser control and I want it to automatically "Refresh" gmail to check it more often than it does by default. There are monkey scripts that do this but I'm trying to add my personal style to it.
Anyhow, I've looked around and found everything but what I can do in csharp using the browser control.
I found this:
// Link the ID from the web form to the Button var
theButton = webBrowser_Gmail.Document.GetElementById("Refresh");
// Now do the actual click.
theButton.InvokeMember("click");
But it comes back with null in 'theButton' so it doesn't invoke anything.
Anyone have any suggestions?
It's been awhile since I've used JavaScript, but given the other answers and comments that there is no real ID associated with the element, could you do something like the following:
Search all Div's with an attribute of Role == 'Button' and an InnerHtml == 'Refresh'.
Once the correct InnerHtml is found, get the Element.
Invoke the click on the found Element.
Again, this may be blowing smoke, but thought I'd throw it out there.
edit: Just realized you are doing this with C# and a browser control; however, the concept would still be the same.
The best suggestion I could give you at this point involves an existing API that is used for .NET web browser based automation:
http://watin.org/
Since the div tag with the desired button really only seems to identify itself with the class name, you could use the Find.BySelector(“”) code included with the most recent version of watin.

NullReferenceException on Page_Load

I'm trying to edit a textbox's text in a Page_Load form on an ASP.NET page, but when I try to edit TextBox1.Text it throws a NullReferenceException because the TextBox has not yet been instantiated. Specifically what I'm trying to implement is this: http://www.codeproject.com/KB/user-controls/popupcalendarcontrol.aspx but it is written for an older version of ASP.NET and does not work for me. Is there a way to instantiate the controls at the start of Page_Load? or another event that I can catch on load? With a normal windows form I would call InitializeComponent() in the constructor.
There are absolutely different events you can attach to but it sounds like the page isn't loading the controls properly because they should be available by that point. You can take a look at the ASP.NET Page Life Cycle for more information.
Does the TextBox sit within a bound control, such as a FormView, GridView, DataList, etc? If so, then the control won't exist until after the databinding happens. Once that event happens, you can do something like
DirectCast(myDataList.SelectedRow.FindControl("myTextBox"),
TextBox).Text
I know that I am a bit behind in getting to this question BUT I have found something unusual that I can NOT find documented anywhere. It would appear that IF your page is "under" a Master Page, and IF you refer to ANYTHING on said master page, the controls to your current page are null - EVERYONE of them. I found this out by referencing "Master.Environment" - a public string - during my InitializeCulture method and having the FIRST control on my current (Default.aspx) page be null during Page_Load() - WHATEVER the first control was.
I know I am late but if this can help ANYBODY, I want the word out.

Get the links id for the current cursor position in the webrowser control

We are using the WebBrowser control in c# winforms and need to be able to get information about the Url the cursor is positioned on.
So we have a web page in design mode, which has multiple urls, when the cursor is over one I would like to call a method which would return the id of the link.
Thanks
You can use the IHTMLCaret to get the cursor position from there using IMarkupPointer you can get the element in the current scope.
The webBrowser control has a Document property which has a Links collection. Each Link is an HTMLElement which has events you can tap into. Again, I'm not sure what you mean "cursor" because in the web world, unless if you're in a textbox, there really isn't a "cursor" (which is what I meant to ask in my comment) but you can tap into the MouseOver event and other stuff like that.
Example:
foreach (HtmlElement element in this.webBrowser1.Document.Links)
{
element.MouseOver += (o, ex) =>
{
Console.WriteLine(ex.ToElement.GetAttribute("HREF"));
};
}
This will print out the actual URL that the mouse is over.
You can have a look at this article - Hosting a web browser component in a C# winform - which explains several ways to perform that. or go directly to this one - Hosting a webpage inside a Windows Form - Basically what you need to do is handle the Click of the DOM object inside the COM WebBrowser of IE. You achieve this by handling the Js events inside your C# code.
I remember this kind of customization must be done using the AxSHDocVw.AxWebBrowser COM object instead of the System.Windows.Forms.WebBrowser Class from the newer versions of the .Net Framework.
I could send you more data about this, I did it some project, just give me time to find it ;). In the mean time try with those links.
By!

Categories

Resources