Navigating with Gecko Web Browser in C# - c#

I'm using Geckofx 33.0.9.0 in my C# application, and I'm having a problem with navigating.
Generally it works well, I enter a URL and it takes me to a page. The problem is that if I enter a URL for a page that doesn't exist, it gives me a MessageBox titled 'Alert'.
1) This is really annoying. At least in my Opinion
2) I would really like to set it up, so if I navigate to a page that doesn't exist, it creates a google search from my url, like in most proper browsers.
I tried looking but Gecko isn'T really well documented, or at least I couldn't find it (Though if someone has documentation for it, that would be great!) and I couldn't find any other way of navigating other then the .Navigate('String Url/Uri') method.
What can I do to circumvent this Alert box? Is there a way?
I'm creating the GeckoWebBrowser Control in code btw.
I will of course post code if required.

to supress the alert message box you can try the following
Xpcom.Initialize(Paths.XulRunner);
GeckoPreferences.User["browser.xul.error_pages.enabled"] = false;
GeckoPreferences.User["browser.download.manager.showAlertOnComplete"] = false;
GeckoPreferences.User["security.warn_viewing_mixed"] = false;
GeckoPreferences.User["privacy.popups.showBrowserMessage"] = false;
As for the second idea, well, that shouldn't be very hard - you just need to check in the DocumentCompleted or NavigationFinished event for a page status - if it's not correctly loaded, then take the address string bit and redirect to google url + your string.
Hope this helps!

Related

Validation of viewstate MAC failed with server.transfer

I've done some googling on this and i know a question similar to this has been asked several times, but so far I'm not having any luck.
I've got the home page on our site also set up to be the 404/410 error page. When I do a server.transfer to this page the search form on the page no longer works. Response.redirect to this page and it works fine.
So in my code the page doing the server transfer has
Server.Transfer("/default.aspx?status=410", false);
When i then use the search form on default.aspx after getting there from the transfer, i get the error in the title.
I also see that the action value on the form has a value, whereas normally it should be blank. I saw another post suggesting setting this value manually in my content page, but I would have to hardcode the ID for the form and I'm not sure if that would cause issues on other pages since this form is used by all the pages on the site since this master page is the main master page for the whole site. Plus even when i did hard code the ID and then used Page.Master.Findcontrol to get the form and set Action = "" it still had a value set for action instead of an empty string.
I had thought as long as I didn't use Server.Transfer("", true) I shouldn't get this error. And i do have a machineKey entry in my web.config
Anyone have any suggestions on how to resolve this?
Thanks
Server.Transfer()
and Response.Redirect()
both are performing same functionality like help to navigate user from one page to another, but internal process happening inside system little bit different. To get more idea about that please visit
http://www.dotnet-tricks.com/Tutorial/csharp/c4SE281212-Difference-between-Response.Redirect-and-Server.Transfer.html
I think i found a solution, although I'd appreciate some feedback to explain something.
On the page that's being transferred to (default.aspx) I tried using Form.Action = "" and when i loaded the page that resulted in the action being something like action="/../../../default.aspx"
Just now i tried it again but I used Form.Action = "/", which did set the action to "/" and that seems to work.
So i guess my question is why did trying to set the action to "" result in that relative url?
And is this the correct way to handle this problem?
Thanks

How to make the 'Are you sure you want to navigate away from this page' warning message in browser?

I have a web page that contains a textbox and a submit button. When the user edits the text in the textbox and clicks another link (not the submit button) how do I display the 'Are you sure you want to navigate away from this page' popup message?
I have researched this on the net and found a few javascript examples. Is this the only way you can do this? If not, what is the best way to do it?
This is one of the multiple ways to achieve the same thing
function goodbye(e) {
if(!e) e = window.event;
//e.cancelBubble is supported by IE - this will kill the bubbling process.
e.cancelBubble = true;
e.returnValue = 'You sure you want to leave?'; //This is displayed on the dialog
//e.stopPropagation works in Firefox.
if (e.stopPropagation) {
e.stopPropagation();
e.preventDefault();
}
}
window.onbeforeunload=goodbye;
got it from here open js
Only the unload() event will work on JS. You can't manage it on the server.
Check out the answer to this other question on SO, it is very similar to your question
How to show the "Are you sure you want to navigate away from this page?" when changes committed?
Simple solution
window.onbeforeunload = confirmExit;
function confirmExit() {
return "Are you sure you want to leave this page?";
}
4guysFromRolla.com - Prompting a user to Save when Leaving a Page
You cannot use the onbeforeunload window method as it gets triggered by multiple ways like back and forth browser navigation links, refreshing the page, closing of the page, clicking on the links.
What i feel you have to bind the link tag for which you want display the navigation away message and then use the function for the status message display
window.addEvent('domready',function(){
$$('a').addEvent('click', function(e) {
//leaving(); function u wrote for displaying message
});
});
function leaving(e) {
if(!e)
e = window.event;
// return code for the displaying message
}
If you want to do this in a way that guarantees it will work on almost all browsers, use the JQuery library. The following describes the unload event.
http://www.w3schools.com/jquery/event_unload.asp
It's exactly for purposes like yours.
Just to elaborate a little, you would have to download the jquery js library and reference it in your project/page, but you'll probably want to do that eventually anyway.
If you want to control this from the server side, you can dynamically emit the jquery call in the OnPreRender.
Look into Jquery's .beforeunload property. Here is an example:
$(window).bind('beforeunload', function(){ return 'Click OK to exit'; });
Please note, beforeunload canot prevent a page from unloading or redirect it to another page for obvious reasons; it would be too easy to abuse. Also if you just want to run a function before unloading, try the following:
$(window).unload(function(){ alert('Bye.'); });
Finally, don't forget to referrence jQuery in your head tag by using:
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
The above gets you the latest version from the internet and saves you the trouble to download it, and of course you can do so optionally, but I am just trying to get your thing to work asap.
Oh, I also found an example for you. Click here to see a page that calls a function before it closes. Hope this helps bud.
I was able to get this to work with Andrei G's answer. I would add on that to get it to work in Chrome, add this to the end of his goodbye function:
return "";

Try to get for WatiN for links/browsers

I've recently started to test and use WatiN, and i came across some of problems/errors that i cant really solve.
First ill describe what i'm doing with my tests ..
I'll go and get all the links in a specific div in a list ... so i have a link lists ... which im gonna loop thru ...
In the loop im gonna use a link.clicknowait() function, to open that link (it opens in a new ie tab) .. then i sleep thread for like few seconds and i close that browser by attaching that link (url) to a new browser and after that the browser is closed as im using it always with 'using (..)' statement.
So the first problem is that when the browser gets all the links and start to click them one of the links might lead not the right page, meaning like it can lead to a page that is saying page doenst exist anymore, so after that i cant close that page anymore ... how to solve this and attack ie to that not existing page?
It hasnt got fixed url ... anything that you can recommend?
Is there a method something like "Try attach to ...." because else i get an error if it tries to attack a browser to a link which doesnt exist.
Also is there anyway to check if the link in a links list is still the right one, cuz i also had an exeption few times that it couldnt click on that link because it was empty in the page ...
Or can i check something like link.trytoclick() and if an error and just ignores that link and goes further ???
And the last question after clicking alot of links and opening and closing browser i got an error outofmemoryexception .. how can it be, i've used 'using statement' which always closed browser when it was out of it ...
Thanks in advance for the help.
You can use the href to open the link in the same window of your watin session, after you verify the link you can go back to original page.
Some thing like this:
string href = browser.Link("link_id").GetAttributeValue("href"); //or your link from the collection
browser.GoTo(href);
//Perform you check
browser.Back();
To check if the link exists use:
session.browser.Back();

Click Gmail Refresh button

I am writing a simple personal app that has a browser control and I want it to automatically "Refresh" gmail to check it more often than it does by default. There are monkey scripts that do this but I'm trying to add my personal style to it.
Anyhow, I've looked around and found everything but what I can do in csharp using the browser control.
I found this:
// Link the ID from the web form to the Button var
theButton = webBrowser_Gmail.Document.GetElementById("Refresh");
// Now do the actual click.
theButton.InvokeMember("click");
But it comes back with null in 'theButton' so it doesn't invoke anything.
Anyone have any suggestions?
It's been awhile since I've used JavaScript, but given the other answers and comments that there is no real ID associated with the element, could you do something like the following:
Search all Div's with an attribute of Role == 'Button' and an InnerHtml == 'Refresh'.
Once the correct InnerHtml is found, get the Element.
Invoke the click on the found Element.
Again, this may be blowing smoke, but thought I'd throw it out there.
edit: Just realized you are doing this with C# and a browser control; however, the concept would still be the same.
The best suggestion I could give you at this point involves an existing API that is used for .NET web browser based automation:
http://watin.org/
Since the div tag with the desired button really only seems to identify itself with the class name, you could use the Find.BySelector(“”) code included with the most recent version of watin.

asp:CreateUserWizard Redirecting After Complete

I have an asp registration page using a custom asp:CreateUserWizard.
Once the registration is completed successfully (RegisterUser_CreatedUser for example) I want to redirect the user to another page, be it a welcome screen, etc... (using Response.Redirect(URL); I guess), but I also want to, some how, popup a new window with the login page.
Is it possible to popup a screen from an external url using this method, or is there another way I should go about it?
I did try creating a custom button which calls this js function for registration:
function redirectAfterRegister() {
Page_ClientValidate();
if (Page_IsValid) {
window.open('/Account/Login.aspx?UserCreated=True');
$('#CreateUserButton').click();
}
return false;
}
This popup works because its called off a click, but the problem with this is the popup is always called even if the creation of the user was unsuccessful - which is wrong.
Any help is highly appreciated.
The problem is that popups only work when a user actually clicks in external sites. This prevents spammers from popping up ads all the time. Once another function is called after the click it is considered unfriendly and therefore to allowed externally.
I think it best to let the user know the registration was successful and give them navigation options from there. If anything, at least its user friendly that way, without confusion.
The asp:CompleteWizardStep can be used to redirect after successful registration, and provide extra navigation where needed.
Good Luck, and let me know if you find an alternate solution.
Why not use the CreateUserWizard.ContinueDestinationPageUrl property to go to your welcome page. You can then place your javascript to open a new window in the onload event of the Body element.

Categories

Resources