close an open tab in browser from C# - c#

is there any way to close a tab on CHROME, Firefox and IE from C#? I believe that I can accomplish this via JavaScript but I do not know how to call a JS from my C# application. By way of background, my app opens a browser, interacts with the caller then I need to close the open tab.

You probably won't be too successful trying to close a specific tab in the opened browser. You could try to call Process.CloseMainWindow on the launched process, but this may get tricky and potentially close other tabs.
You may want to consider creating a new window (WPF) or form (WinForms) in your app and use the embedded browser control.

If the page you're opening is an asp.net you that you can code... In the method which you want to close the tab, use:
Response.Write("<script type=\"text/javascript\">window.close();</script>");
Otherwise, I can't think of a way either.

Related

Disable prompts in Windows Forms Webbrowser

I have a C# application which uses a System.Windows.Forms.WebBrowser.
The problem is: i'd like the user to navigate smoothly in my application, without prompts, without javascript windows popping up, without security prompts. Even if this requires some contents to be unavailable.
I just want to have one window (always one window, if a receive a new window event, i redirect it to the single window).
How can i do this?
I tried to use this.browser.ScriptErrorsSuppressed = true but i doesnt seem to work.
For example, if i test it on a browser page which performs text validation, i still receive a popup window saying that my text is invalid.
Thank you!
I've found a solution somewhere else, since it wasn't available here.
Here it is: http://www.codeproject.com/Articles/31163/Suppressing-Hosted-WebBrowser-Control-Dialogs
Basically, you have to hook the WM_INITDIALOG message.
It works wonders here.

How to close the dialog itself from the code behind?

I create a dialog with in .aspx. This dialog is to update something and show the progress and I want it close automatically when the update is done. So can I close the dialog from the c# code without clicking the "Ok" or "Cancel" button when something is done?
I opened this dialog using a self-made dialog class of my company. I put a "neatUpload" element into this dialog so it can upload some file from the client machine to the server. What I want to achieve is close the dialog when the uploading is done. I don't know whether it is possible or not.
Thank you
You can't shutdown a client-side script from a server-side. It's not a desktop app, web apps work differently.
EDIT:
Maybe there's a way, based on your edit... I suppose you could use ajax to notify the client after the job is done and then use javascript in the dialog to close itself.
It depends on the kind of dialog you're using, if it's a window.alert, it cannot be closed other than by clicking on the 'OK' button.
You will need a JQuery dialog or such to achieve your goal.

Response.Redirect() in new window in Asp.Net

How can i do that i want to process some data on server side with c# and the new page should open in new window.
Thanks in advance
Opening a new window in almost all browsers nowadays has to be invoked from a user click event. Almost all browsers (and this covers about 99% of cases) will block a popup that is invoked from a page load event, so I suggest you reconsider your solution, probably by showing a link that would open in new window.
What I would do, is open the new window on the click event, which would open your processing page, which in turn will redirect to whatever page you want. Basically, first open the popup with click event, and then do the redirect in your new page.
You can't do that using Response.Write. As soon as the request is sent to the server, it's already decided where the requested page will be opened. So, when the server code runs, it's already too late to change where the page will be opened.
If you want to open the page in a new window, that has to be done before sending the request to the server. Instead of doing a postback, you should send a request with _blank as target. You can use a link for that:
...
You can use the window.open method in Javascript:
window.open('Page.aspx', '_blank');

Integrate C# Desktop Application With Browsers

I have developed an application(downloader) using C# now i want to integrate it to browser like i want to place a button in browsers if user press that button then my app should execute & start downloading the required file.
I'm largely guessing, but it sounds like you are talking about a "protocol handler", i.e. to handle hyperlinks to "yourapp:some-stuff-here". If so, try looking at Registering an Application to a URL Protocol on MSDN, which gives a C# example of this.
I don't think that what you want is possible, you can however place a link to the executable and tell your users to execute it via the Browser download options (The Dialog that pops up when you click on some download link that asks wether you want to open oder save the file).
It is not possible to immedatly run your GUI application on a browser button click.

How do i close all open windows from my C# program?

Hi i have a windows application where i show a webbrowser, i have a button that removes the browser (its a preview) and goes to another "view" in my application (another tab). My problem is that my users are getting advanced, they build HTML with links (and its ok) but the links may spawn new browser windows (IExplorer), and my button needs to close these windows, but how?
I have made some code to traverse all eht windows that ends with "Windows Internet Explorer", ahd it all seems to work - but how do i close them? I am trying to do it like this:
SendMessage((int)hWnd, WM_SYSCOMMAND, SC_CLOSE, 0);
It seems to work, but the browser pops up a dialog asking me if i want to close the tab of all the tabs...how to work around/solve this?
Cheers,
walking over all top level iE windows and closing them is a bad idea, unless you are guaranteed users can't launch ie and browse the Internet on their own. Otherwise, you might actually lose user data (say an email or a blog post the user has been working on in the last half an hour)
You can't easily work around that dialog without modifying the per-user IE settings. Your other option is to look for that dialog and click the yes button, but that would be fragile and is not guaranteed to continue working if the user upgrades to IE9.
You could potentially prevent opening links in new window by listening to BeforeNavigate event and allowing only navigations that are guaranteed to happen in your control. However, there are scenarios where IE might still decide to open new window.

Categories