elI am working on a Wpf application. it contains a webbrowser where the user authenticates via Facebook. The problem is that the user is capable of clicking on links (for example: Forgot your password?) the standaard browser then open... what i want to do is to disable/block all the external links. so users can only authenticate and not navigate through the webbrowser control. I hoop you guys can help me out.
Update 1
Like suggested i can check the source of the webbrowser. So i can allow the wanted pages. but the problem are the links. they open on IE. i dont want to open them, but to block them at all
Description image
private void webBrowserFacebook_Navigating_1(object sender, NavigatingCancelEventArgs e)
{
string huidigeLink = Convert.ToString(webBrowserFacebook.Source);
MessageBox.Show(huidigeLink);
// check for allowed pages
}
Update 2
I was able to find a solution: http://social.technet.microsoft.com/wiki/contents/articles/22943.preventing-external-links-from-opening-in-new-window-in-wpf-web-browser.aspx
Very slef explanatory.. thank you guys for the help!
void Window1_Loaded(object sender, RoutedEventArgs e)
{
browser = new WebBrowser();
browser.Navigate(new Uri("http://www.google.com"));
browser.Navigating += new NavigatingCancelEventHandler(browser_Navigating);
browser.Navigated += new NavigatedEventHandler(browser_Navigated);
}
void browser_Navigating(object sender, NavigatingCancelEventArgs e)
{
//Your checks should happen here..
Console.WriteLine("Loading Webpage !!");
}
void browser_Navigated(object sender, NavigationEventArgs e)
{
Console.WriteLine("Webpage Loaded !!");
}
You can register for WebBrowser.Navigating Event.
Navigating event handlers are passed an instance of the NavigatingCancelEventArgs class. You can cancel the navigation by setting the Cancel property of the NavigatingCancelEventArgs object to true.
Or you can invoke script or browser instance to stop loading if URL navigating doesn't matches.
yourWebBrowser.InvokeScript("eval", "document.execCommand('Stop');");
Related
Using Visual Studio community 2017, winforms, C# and MySql DB.
I have a tab control page that displays the playlists created from the DB.
When I delete a playlists, it's removed from the DB, but is still visible in the tab page.
I created a button to try all refresh, update scenarios I could think of to no avail.
PlayListLoad() is my function that creates and populates my playlists.
Thanks for your help.
private void RefreshBtn_Click(object sender, EventArgs e)
{
PlayListLoad();
tabPage3.Update();
tabPage3.Refresh();
}
Found the answer through a lot of searches and try/error.
I just needed to clear the controls before updating.
private void RefreshBtn_Click(object sender, EventArgs e)
{
tabPage3.Controls.Clear();
PlayListLoad();
tabPage3.Update();
tabPage3.Refresh();
}
i have. view and viewmodel in same project.i was opennig a new page on button click on code behind like this
private void button_click(object sender, routedeventargs e)
{
var dashboardwindow = new dashboard();
this.navigationservice.navigate(new uri("../view/dashboard.xaml", urikind.relative));
}
now i have added user authentication on this buton click using MVVM.so how can i open this page from view model.?
if i used window instead of page then i can open new window simply like this
Dashboard dl = new Dashboard();
dl.Show();
thats workd.but i have taken pages because i need bowser type navigation in my application so.. please help .Thanks
I have a Web browser control in my project. Works great! However if I loose connection to the internet then open the project, IE opens and shows the standard cannot display webpage.
I'd prefer the Web browser control in my project show this message and not pop up a IE browser window when the internet connection is lost.
Thanks!
You can do this by importing System.Net.NetworkInformation namespace. NetworkChange Class exposes a event called NetworkAvailabilityChanged which is responsible to notify the application on connection status change. Please find the below snippet. Please mark the answer if useful.
public partial class MainWindow : Window
{
public bool IsAvailable { get; set; }
public MainWindow()
{
InitializeComponent();
NetworkChange.NetworkAvailabilityChanged += NetworkChange_NetworkAvailabilityChanged;
}
void NetworkChange_NetworkAvailabilityChanged(object sender, NetworkAvailabilityEventArgs e)
{
IsAvailable = e.IsAvailable;
}
private void BrowseButton_Click(object sender, RoutedEventArgs e)
{
if (IsAvailable)
{
WebBrowser1.Navigate(TextBox1.Text);
}
else
{
MessageBox.Show("Your Popup Message");
}
}
}
I don't see IE open on my machine, but I do see the normal IE error messages displaying within the WebBrowser control.
I believe you could detect that the webpage wasn't loaded properly by handling the WebBrowser's Navigated event, and looking at the document's url property. Here is some XAML:
<WebBrowser Source="http://www.google.com" Navigated="WebBrowser_Navigated" />
And a bit of code (I don't do VB, sorry):
private void WebBrowser_Navigated(object sender, NavigationEventArgs e) {
var browser = sender as WebBrowser;
if (browser != null) {
var doc = browser.Document as HTMLDocument;
if (doc != null)
MessageBox.Show(doc.url);
}
}
On my machine, when the navigation failed, I got this URL:
res:ieframe.dll/navcancl.html#http://www.google.com
While I don't think we could count on the URL being exactly this all the time, I bet you could inspect it and determine that it's NOT the URL you were looking for. In fact, the "http:" is now "res:". When you see this happen (and don't expect it) you could make the browser point to a local source to display a message.
I have a Silverlight application which has a RadHtmlPlaceholder which points to ssrs to display reports like so:
<telerik:RadHtmlPlaceholder SourceUrl="http://serverName/ReportServer/Pages/ReportViewer.aspx?/Northwind/Employees&rs:Command=render" />
This works fine but when I have a report that allows you to drill down to display a child report, there is no way of getting back to the parent report without having to load the whole lot again. There doesn't seem to be an option to turn on the navigate back button toolbar option and I've seen other ways of implementing a back button by using javascript to set the window location back one in the history, but obviously this won't work in a Silverlight application. Is there anyway to implement a navigate back button?
Take a look at this thread over in the Telerik forums: http://www.telerik.com/community/forums/silverlight/htmlplaceholder/html-place-holder-back-forward-refresh.aspx
Basically you need to get a handle on the IFrame from the presenter and inject some JavaScript. The history object also has a length property you can use to evaluate if your buttons should be enabled.
public MainPage()
{
InitializeComponent();
// Get the IFrame from the HtmlPresenter
HtmlElement iframe = (HtmlElement)htmlPlaceholder.HtmlPresenter.Children[0];
// Set an ID to the IFrame so that can be used later when calling the javascript
iframe.SetAttribute("id", "myIFrame");
}
private void Refresh_Click(object sender, RoutedEventArgs e)
{
// Code to be executed
string code = "document.getElementById('myIFrame').contentWindow.location.reload(true);";
HtmlPage.Window.Eval(code);
}
private void Back_Click(object sender, RoutedEventArgs e)
{
// Code to be executed
string code = "document.getElementById('myIFrame').contentWindow.history.back();";
HtmlPage.Window.Eval(code);
}
private void Forward_Click(object sender, RoutedEventArgs e)
{
// Code to be executed
string code = "document.getElementById('myIFrame').contentWindow.history.forward();";
HtmlPage.Window.Eval(code);
}
}
I Have attached a silver light user control (ex: FirstUserControl.xaml) into my silverlight navigation page project. If I wanna run the application, i need to show the added user control as startup page. How can I set the user control as defualt page in silverlight?
Can any one please give me the solution for this?
You can set it in your App.cs Application_Startup method:
private void Application_Startup(object sender, StartupEventArgs e)
{
// Specify the main application UI
this.RootVisual = new FirstUserControl();
}