Action when website changes - c#

how can i make action when link in WebBrowser1 changes?
I tried to do if(webbrowser1 == https://www.google.nl/intl/en/about/)but it doesn't work
I mean at startup in WebBrower1 is http://www.google.com and when i click something in this WebBrowser1 link changes, for example https://www.google.nl/intl/en/about/ and my question is, how can make an action (MessageBox.Show("you are here"); when i'm on main google page and i'm go to about page
This is "Windows Forms Application"

I guess you could do it using Navigated event.
You can also find an example and how to catch and redirection moment here: https://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser.navigated(v=vs.110).aspx
I believe this example will help you:
// Shows ModalWindow upon navigation.
private void webBrowser1_Navigated(object sender,
WebBrowserNavigatedEventArgs e)
{
MessageBox.Show("you are here:" + WebBrowserNavigatedEventArgs.Url);
}

Related

Trying to use the "launch url on c#" but doesnt work

read title
Here is the code i'm using
System.Diagnostics.Process.Start("My url, dont wanna show it xd");
But visual studio tells this
screenshot
i tried using the light, but tells
this
You need to add a method to the click event before you can call the Process.Start. For instance:
button14.Click += Button14_Click;
Now if you click the button it will open the browser.
private void Button14_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("my url");
}
Have you tried
Process.Start("chrome.exe", "http://www.YourUrl.com");
Similar question here: How to launch a Google Chrome Tab with specific URL using C#

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

C#, Windows Phone 7, Using if-else statement to check which page is displayed

Now I'm developing a Windows Phone 7 app, my app has three buttons located at the applicaton bar. Here is the event handler for these three buttons:
//app bar page navigation
private void ApplicationBarIconButton_Click1(object sender, EventArgs e)
{
if(//check if the current displayed page is mainpage)
{
//do nothing
}
else
{
NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.RelativeOrAbsolute));
}
}
private void ApplicationBarIconButton_Click2(object sender, EventArgs e)
{
NavigationService.Navigate(new Uri("/Audios.xaml", UriKind.RelativeOrAbsolute));
}
private void ApplicationBarIconButton_Click3(object sender, EventArgs e)
{
NavigationService.Navigate(new Uri("/Statistics.xaml", UriKind.RelativeOrAbsolute));
}
The button navigation works well except the first one (Button_Click1) because when I first access the main page and click the first button the app will automatically go back to app list.
So I want to use if-else statement to check which page is currently displayed and then decide whether to navigate or stay in the current page.
It looks like you are using the ApplicationBar like you would use a TabBar within iPhone (or similar in Android, Bada, etc too)
In WP7, the Metro style is typpically to use a Pivot or Panorama rather than a "Tab Bar" for this type of navigation.
If you do want to use the ApplicationBar like this:
then you can (the WP7 Marketplace will allow it) but users might feel it's not very Metro.
then you might want to consider disabling the appropriate button rather than just stubbing out the action.
then you can detect the currently shown page using CurrentSource on the NavigationService
Also, please do note that if you try to navigate from MainPage.xaml to the same url MainPage.xaml then you will see an exception - as I recall, the navigation service complains about url fragments not being supported.
Button click1 should be removed from the main page. It makes no sense to have that button there.
Other pages should use the back button to return to the main page. Otherwise you mess up your back stack.

Submission of a webpage form using WebBrowser control in C#

I have seen a lot of posts regarding this particular subject on SO as well as on the web in general and most if not all code is as seen below
private void btnSubmit_Click(object sender, RoutedEventArgs e)
{
webBrowser1.Navigate(new Uri("http://samples.msdn.microsoft.com/workshop/samples/author/dhtml/refs/onsubmit.htm"));
}
private void btnLogin_Click(object sender, RoutedEventArgs e)
{
mshtml.HTMLDocument htmlDoc = null;
htmlDoc = (mshtml.HTMLDocument) this.webBrowser1.Document;
if (webBrowser1.Document != null)
{
foreach (mshtml.HTMLFormElement form in htmlDoc.forms)
{
form.submit();
break;
}
}
}
The code has no errors whatsoever but for the life its not submitting. The sample page that I am using has simple button, what it does, it alerts the selection of the radio button and then submits the form. For some strange reason when the form is submitted via code using the WebBrowser control, the form is submitted but the alert never shows up.
I am not sure what I am doing wrong here. Any help on this would be appreciated.
Would performing a click on the button do what you need it to do? You will need to add a COM reference to the Microsoft HTML Object Library (which you may already have). For example, if you load up google into the webbrowser control, this code will place "hello world" into the search box and perform the search:
mshtml.IHTMLDocument2 doc = ((mshtml.HTMLDocumentClass)webBrowser1.Document);
((mshtml.IHTMLElement)doc.all.item("q")).setAttribute("value", "hello world");
MessageBox.Show("Clicking I'm feeling lucky button");
((mshtml.HTMLInputElement)doc.all.item("btnI")).click();
Edit: I updated the code for the components that the WPF WebBrowser control uses. Also note that this sometimes throws a script error from google, but that appears to be a timing issue based on some of the ajax calls google has on the home page.
To fix your problem you need to replace line:
form.submit();
With following code:
var children = form as IEnumerable;
var inputs = children.OfType<mshtml.HTMLInputElement>();
var submitButton = inputs.First(i => i.type == "submit");
submitButton.click();
This will show alert about user selection and submit form.
I've got a more dirty one-liner working by injecting a JavaScript to submit the form
_webBrowser.InvokeScript("eval", new object[] { "document.getElementById('formName').submit()" });
That's been working for me when interacting with a site using a lot of JavaScript and button beyond the form.

Categories

Resources