Detect a specific website my WebBrowser navigated to - c#

I'm using a WebBrowser Control on a WPF application that opens a website with a form for the user to fill and submit. After submitting, it redirects to another page after 10 secs, www.google.com for example.
Is there a way to detect when the WebBrowser has opened www.google.com?

You can detect when the WebBrowser has opened your site name in webbrowser_LoadCompleted event,try this:
void webbrowser_LoadCompleted(object sender, NavigationEventArgs e)
{
mshtml.HTMLDocument doc =( mshtml.HTMLDocument) s.Document;
if (doc.parentWindow.window.location.host == "www.google.com")
{
MessageBox.Show("you navigated to google");
}
}

Related

How to open links in wpf webview in default explorer

My webview display a html page which contains lots of links, if I click the link, the new page will open in the webview, is there a way to open the link in default browser rather than in webview?
You can handle the WebView.NavigationStarting event as follows:
private void navigationStartingHandler(object sender, WebViewNavigationStartingEventArgs e)
{
e.Cancel = true;
Process.Start(e.Uri.ToString());
}
Here and here are more answers about opening web pages in default browser

Opening Web Browser click in default browser

Currently I am building a windows form app using c#. I have a web browser control on my form that displays a google ad. When clicked the webpage is displayed within the little 300x300 web browser control on the form. Is there a way for me to launch the default browser when the ad is clicked instead?
Thanks.
Edit: I figured out I can do so open the default browser by using Process.Start("url here"); but the browser windows is opening upon app load.
Edit Adding Code:
Form.cs
private void AdPanelNavigating(object sender, WebBrowserNavigatingEventArgs e)
{
e.Cancel = true;
if (e.Url != null ) Process.Start(e.Url.ToString());
}
Form.Designer.cs
this.AdPanel.Navigating += new WebBrowserNavigatingEventHandler(AdPanelNavigating);
You can add Navigating event handler:
webBrowser1.Navigating += new WebBrowserNavigatingEventHandler(WebBrowser_Navigating);
void WebBrowser_Navigating(object sender, WebBrowserNavigatingEventArgs e) {
e.Cancel = true;
Process.Start(e.Url);
}
It will open default browser instead of opening this url inside webbrowser.

Detect whether user has clicked on a specific link inside a Browser Control?

I'm not using the in-built browser control (ie wrapper) but i'm using Webkit.Net instead
When i click on button1, the WebkitBrowser will navigate to the link typed on textBox1
private void Button1_Click(object sender, EventArgs e)
{
backgroundWorker1.Navigate("https://www.facebook.com/cocacola");
}
Can i detect when user click on the Like button ? For example when the user likes the page, a MessageBox appears and says that the page has been successfully liked !
Anyhelp would be highly appreciated

Response.Redirect() disables back-button

As far as I understand, Response.Redirect("http://stackoverflow.com"); tells the browser to initiate a request to another URL.
One of the results of this is that the browser "remembers" the redirection, and allows pressing "back."
However, I have a website where Response.Redirect disables the ability to press the browser's "Back" button, as if the browser had opened a new window. (Browsing history is not forgotten, unlike with Server.Transfer.)
The redirection used to work properly in the past, so I suspect the problem has something to do with the IIS server (IIS7).
I apologize in advance if this question should be moved to ServerFault.com.
UPDATES:
Here is some code:
protected void btnClickMe_Click(object sender, EventArgs e)
{
// ...
// some server-side logic
// ...
Response.Redirect("NewPage.aspx?ProductID=" + idNum);
}
Regarding "disables the ability to press the browser's 'Back' button", what I meant is that the button cannot be pressed. Same as when you open a new window. The button is gray, and clicking it has absolutely no effect.
UPDATE 2:
This has been tested with IE6 and IE8.
The problem was NOT with the Response.Redirect();.
When I was on OldPage.aspx, I entered a new URL in the address bar. Once the browser loaded the new site, it disabled the back-button.
Conclusion: There is something wrong with OldPage.aspx, not the redirection to NewPage.aspx.
I still don't know why THIS happens, but this is an entirely different question.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack) //check if the webpage is loaded for the first time.
{
ViewState["PreviousPage"] =
Request.UrlReferrer;//Saves the Previous page url in ViewState
}
}
protected void btnBack_Click(object sender, EventArgs e)
{
if (ViewState["PreviousPage"] != null) //Check if the ViewState
//contains Previous page URL
{
Response.Redirect(ViewState["PreviousPage"].ToString());//Redirect to
//Previous page by retrieving the PreviousPage Url from ViewState.
}
}

Focusing WebBrowser control in a C# application

I have a WebBrowser control hosted in a windows Form. The control is used to display hyperlinks which get created at runtime. These links point to some HTML pages and PDF documents.
The problem is that when the form hosting the browser control is loaded, the focus is on the form. When the TAB key is pressed, the focus does not shift to the first hyperlink. However, if I perform a mouse click on the control and then hit the TAB key, the tab focus is now on the first hyper link. I tried using Select() on the WebBrowser control and then I called Focus(), but it doesn't solve the problem.
Any ideas on how to set the tab focus on the first hyperlink at load? Thanks.
Cheers,
Harish
I guess it might be because the focus is set before the page is fully loaded. Try this:
private void Go(string url)
{
webBrowser1.Navigate(url);
webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
}
void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
webBrowser1.Document.Body.Focus();
}
You could also automatically select the focus on the first link directly by getting the HtmlElement of that first link.
If the above doesn't work, you might want to check other parts of your code to see if anything else is capturing the focus. Try searching for Select, Focus and ActiveControl in your code.
Use form.ShowDialog(form) instead on form.Show(), then it will work !
where form is the running instance of your windows Form
This is my solution
private void txtAdres_KeyPress(object sender, KeyPressEventArgs e)
{
int licznik = 1;
if (e.KeyChar == (char)13)
{
string adres = txtAdres.Text;
webBrowser1.Navigate(adres);
licznik = 0;
}
if (licznik == 0)
{
webBrowser1.Focus();
}
}
In a normal scenario it should be sufficient for you to set the TabIndex of the WebBrowser control to zero. This way, when the form loads the control will be focused and pressing TAB will iterate through the links.
Note that you should also change the TabIndex of the other controls on the form.
If this does not solve your problem, you need to add more detail on the complexity of the form hosting the control.

Categories

Resources