C# Winforms WebBrowser open links in default browser - c#

I know this has been discussed several times around here but the default behaviour for opening links
clicked in a WebBrowser control does not work for my application.
So while this works as in it opens a link clicked in IE:
private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
System.Diagnostics.Process.Start(e.Url.ToString());
e.Cancel = true;
}
I am using a dropdown list to update the html file that the webBrowser is displaying like so:
private void ddl_SelectedIndexChanged(object sender, EventArgs e)
{
webBrowser1.Url = myURI;
}
Now the problem I'm having is that with the _Navigating method above, the webBrowser does not load any subsequent changes to the URL (thanks to the e.cancel I guess) so it only displays the first html file it loads.
If I remove the _Navigating method it updates fine but then the links open up in the same webBrowser control which is what I do not want.
How can I get it to work both ways?

I hope this can help you.
If you want to open a link in a browser, you can add this simple code:
Process.Start("http://google.com");
Remember, there is a lot of information about it. Here in stack Overflow you can take a look in this post: How to open in default browser in C#
If you want to open your link in another browser, you can use this code:
System.Diagnostics.Process.Start("firefox.exe", "http://www.google.com");
Don't forget to visit this post called: How do I open alternative webbrowser (Mozilla or Firefox) and show the specific url?
And Finally, I could recommend you this stack overtflow post called: .NET C#: WebBrowser control Navigate() does not load targeted URL
I hope this information can help you a little bit.

This is an old post but I believe I may understand what the original poster wanted to do. They wanted a page to load in the webbrowser control if the user selected it from the dropdown list but any links in the loaded page should open in the user's web browser. If this is indeed the case, the original poster need a flag on the form to determine the behavior.
The original poster simply needed a flag such as linksOpenInSystemBrowser shown below.
using System;
using System.Windows.Forms;
namespace Browser_Test
{
public partial class myForm : Form
{
private bool linksOpenInSystemBrowser = false;
public myForm()
{
InitializeComponent();
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
linksOpenInSystemBrowser = false;
webBrowser1.Navigate(comboBox1.SelectedItem.ToString());
}
private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
if(!linksOpenInSystemBrowser)
{
linksOpenInSystemBrowser = true;
return;
}
System.Diagnostics.Process.Start(e.Url.ToString());
e.Cancel = true;
}
}
}

Related

Forms Web Browser control won't proceed or redirect on it's own

I have a main form that opens a new form that contains a web browser control that navigates to the entered url when it is loaded. I've tried different things I've found and this is my latest code: I have a function (GoToURL) that is triggered at the Shown event of the form which is here:
public delegate void Launch();
private void launchBrowser()
{
webBrowser1.Navigate(GlobalData.URL);
}
private void GoToURL(object sender, EventArgs e)
{
webBrowser1.Invoke(new Launch(launchBrowser));
}
I have nothing in the Document Completed function:
void browser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
}
The web browser control loads the url just fine and I can scroll around in it, but on some links or buttons the control goes white and nothing happens. It won't proceed with the desired function. A more specific example is I am logging into a site and once I click the submit button it just hangs there. A "loading" image is presented and it just sits there. I know the Document Completed function above is triggered when this happens, too.
I apologize for my inexperience with C# and this is the first time using the web browser control (forms not wpf), so I am at a loss of what to try. I suspect its a threading issue, but that's as far as I got.
You are experiencing issues within the browser itself. Please launch IE and test the same there. I suspect you'll see the same issues.
Your experience is tied to the version of Internet Explorer you have in your system, and javascript errors have been frequents in the earlier versions of IE. Upgrade to IE11 in any case.
If that's not an option, try using a browser control with a different rendering engine. I'm too lazy to find and list the options, but I've used a few and they work well.
to avoid JavaScript Errors please follow this
public frmMain()
{
//Java Script Error popup block
webBrowser1.ScriptErrorsSuppressed = true;
}
Please Check this on loading Uri using
webBrowser1.Url = new Uri(Url.Trim().ToString());
Url - your desired Url

Managing _blank links

I'm using xulrunner to show some content from a website, it works perfectly for the most, but we have included a facebook like box with stream (div+javascript), and clicking on any link in it does nothing.
We would like to open it in a new window of the default browser, it's possible?
the code for the xulrunner is pretty simple
private void Form_browser_Load(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Maximized;
geckoBrowser.Navigate(Properties.Settings.Default.redirect);
}

How to click a non ID java button in my c# browser?

I have a c# program which has a web browser my program deals with java pages i want it to click a button in a page but the button has no ID or value all i got about it is this code :
td class="submit">
<br>
<button class="fixedSizeBigButton" type="submit">
</td>
please is there any way to click that button
and for http://watin.sourceforge.net/ i need a code
thanks a lot
Your button has a class name,you can find your button using class name,see this example:
private void Form1_Load(object sender, EventArgs e)
{
webBrowser1.DocumentText = "<td class=\"submit\"><br><button class=\"fixedSizeBigButton\" type=\"submit\"></td>";
webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
}
void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
foreach (HtmlElement btn in webBrowser1.Document.GetElementsByTagName("button"))
{
if (btn.GetAttribute("ClassName") == "fixedSizeBigButton")
{
btn.InvokeMember("Click");
}
}
}
I found this link when I went looking around.
More looking around leads me to believe you need to look more into the entire Find class itself. I know there are other ways to locate elements.
I'm trying to locate the documentation that will let me construct some code, since I've never actually used this before :-) I'll update if/when I get something more.
Update
Looks like you aren't the only one having similar issues. You can apparently Chain find methods. WatiN - Find HTML Table Which seems rather useful, if you know more about the button's exact location. Using Find.ByClass along with something else, to try to define the button uniquely.

webbrowser iframes open in default browser

I've got the WebBrowser control to open links in my default browser like this:
private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
if (e.Url.ToString() != "about:blank")
{
e.Cancel = true;
System.Diagnostics.Process.Start(e.Url.ToString());
}
}
This works great but if I load a document that contains some IFrame elements those will be opened in the system browser as well (mostly embedded stuff, like Google Maps, Digg icons, etc..).
How do I keep iframes loading in the Webbrowser control and user clicked links in the system browser?
I've come to the conclusion that the .NET WebBrowser component is almost useless in this regard. I've tried to read WebBrowserNavigatingEventArgs.TargetFrameName but it will return the name attribute of the iframe element only if the HTML document has it. Otherwise it will spit out an empty "" string. Returning null on non-frame links would have been tons more useful.
So the only fix I've found for this is using the AxWebBrowser control, and specifically listening to the BeforeNavigate2 event. I haven't tested as much as I should have, but it appears that the "flags" property in DWebBrowserEvents2_BeforeNavigate2Event is set to 64 each time it's user triggered.
private void axWebBrowser1_BeforeNavigate2(object sender, AxSHDocVw.DWebBrowserEvents2_BeforeNavigate2Event e)
{
// 64 means user triggered
if ((int)e.flags == 64 && e.uRL.ToString() != "about:blank")
{
e.cancel = true;
System.Diagnostics.Process.Start(e.uRL.ToString());
}
}
MSDN docs says that flags is an IE7+ only parameter, so I have no idea what happens on IE6 machines...
Using Internet Explorer from .NET has some really valuable information regarding AxWebBrowser.

ASP.NET PopupControlExtender Problem

I am trying to create a popup which will be used to select a month/year for a textbox. I have kind of got it working but however when I try and read from the textbox when I Submit the form it returns an empty string. However visually on the page I can see the result in there when I click the Done button which can be seen in the screenshot.
http://i27.tinypic.com/2eduttx.png - is a screenshot of the popup
I have wrapped the whole textbox/popup inside a Web User Control
Here is the code of the control
Code Behind
ASP Page
and then read from the Textbox on the button click event with the following
((TextBox)puymcStartDate.FindControl("txtDate")).Text
Any suggestions of how to fix the problem?
You may need to read the form posted value rather than the value from the view state. I have the following methods in my code to handle this.
The below code just grabs the values in the request headers (on post back) and sets/updates the controls. The problem is that when using the ASP.NET Ajax controls, it doesn't register an update on the control, so the viewstate isn't modified (I think). Anyways, this works for me.
protected void btnDone_Click(object sender, EventArgs e)
{
LoadPostBackData();
// do your other stuff
}
// loads the values posted to the page via form postback to the actual controls
private void LoadPostBackData()
{
LoadPostBackDataItem(this.txtYear);
LoadPostBackDataItem(this.txtDate);
// put other items here if needed
}
// loads the values posted to the page via form postback to the actual controls
private void LoadPostBackDataItem(TextBox control)
{
string controlId = control.ClientID.Replace("_", "$");
string postedValue = Request.Params[controlId];
if (!string.IsNullOrEmpty(postedValue))
{
control.Text = postedValue;
}
else
{
control.Text = null; // string.Empty;
}
}

Categories