On Load you load a Url.
How do you detect and fire an event when the user navigates away from a page? (example click on web page button/link)
When new navigation begins, i.e. Navigating.
Actually the same Navigating occurs when you type a url, click a link, etc.
Load occurs on the form, or user control, when it loads. WebBrowser control has no such event.
Related
I need to simulate a button click on my ascx page. The user firstly clicks on a button on the service home page which links to a certain service and a new window opens for that related service. If they are not logged in it directs them to the login page. A querystring is sent with it to keep note of what service they had originally clicked. When the user then logs in they are redirected back to the services page and the querystring of what they clicked on before is sent also.
I have up to this point working fine. The problem is that when I'm redirected back to the online services page I need to simulate an onclick event which will open the new window. I cant click on an the onclick method for the button as there is none, everything is done dynamically. Any ideas?
I would have thought the easiest way to achieve what it sounds like you're trying to do is in the code behind for your Online Services Page check to see whether the query string contains the url for the service they clicked on before you sent them to the login page, if it does then add some javascript to run on start up which launches the pop-up (using the ClientScriptManager.RegisterStartupScript method: http://msdn.microsoft.com/en-us/library/system.web.ui.clientscriptmanager.registerstartupscript.aspx).
If you absolutely have to fire the click event you can also do that from your javascript by finding the button you want to click by it's ClientId (using getElementById) and then calling the javascript click() method on it. Here is an example: http://www.w3schools.com/jsref/met_html_click.asp
I've got a webbrowser in a c# form and when the user click on a link the page opens in the ie10 browser.
All I want is to intercept this event and open the new page in another webbrowser (extendendwebbrowser really).
The fact is that i don't want to know what the user click in the page, but i'd like to intercept all the requests "open new page" from my webbrowser and redirect them to my extendedwebbrowserform and create a new tab with that link.
Thanks for help.
There are at least 2 ways to do that:
Extend the WebBrowser control to intercept the NewWindow2 event, then cancel original request and use that url to open it in a new window. Similar code can be found here or in code project Extended .NET 2.0 WebBrowser Control.
Implement INewWindowManager, and use EvaluateNewWindow to do the same.
I am using the web browser control in C# project to load html pages.
I can navigate back / forward or refresh the page only after right clicking the web page.
How do I add these navigation controls to the web browser itself, so that user can navigate without right clicking.
You can add buttons on your form and wire up their click events to the appropriate actions you need.
//if wb is your WebBrowser instance
wb.GoBack();
wb.Refresh();
wb.GoForward();
Having a C# WebBrowser control inside my WinForms application, and being aware of the Navigating event, I never came up with a nice and elegant solution to the following:
If a user actively navigates to another URL, I want to allow it.
If the page redirects "on its own" to another URL, I want to cancel it.
For case 1 there are some cases I can think of:
User clicks an a tag and the href attribute is evaluated to load another URL.
User clicks on an element with an onclick javascript event handler which calls a function that uses window.location to load another URL.
For case 2 I can imagine of:
The loaded page contains an iframe tag that loads an URL inside the IFrame. This fires the Navigating event.
There is some JavaScript timer that is started on page load and when it fires, it uses window.location to load another URL.
The loaded page contains a meta refresh header tag to load another URL after some seconds.
So my question is:
How to detect inside the Navigating event (or any other mechanism) whether a redirect is triggered explicitly by the user or implicitly by the page?
Some more information
The WebBrowser is being used inside a windows based CMS backend application.
I therefore have full control over the content loaded inside the WebBrowser control.
Meaning that I can manipulate the complete HTML string before being sent to the browser, if required.
If it is more applicable, I also would love to get JavaScript-only solutions which I could inject into the HTML being loaded.
(Please note that I do believe this is not a duplicate of this SO posting)
My take on this is capture user clicks on the web browser control. Have it set a flag that indicates that the user clicked on the web browser. If the flag is true, then allow redirection, if it isn't true don't allow it. Make sure to reset the flag after n number of seconds if no (or after) redirection is made.
It seems you are trying to achieve anti-ads/popup/redirect pattern.
From web browser perspective.. clicking <a href="some.url"> is not different from javascript window.location = "some.url"; or 302 redirect response. there are no explicit signals, no such convenience methods.
The WebBrowser control is just a proxy to IE component. You can't intercept browser's engine or even disable/enable javascript as it's part of internet security option.
You have to create special logic to prevent every possible cases of redirection.
eg.
verify HTML string then restrict some javascript pattern, header or iframe with Regex.Replace before render.
var scriptEx = new Regex("<script (.*?)</script>");
var iframeEx = new Regex("<iframe (.*?)</iframe>");
or intercept Navigating URL and cancel unsafe url, etc.
After a user submits a form, how do you detect that the form has been posted/submitted and the browser has navigated to the logged in page?
Handle the DocumentCompleted event and check e.Url.