C# WebBrowser turn off jquery popups - c#

I have an application that uses WebBrowser control to navigate from page to page, on some pages I get a leaving popup asking me if that's what I want to do.. this stops the whole further execution until I press "Leave" or "Stay".. How can I disable them?
What I've tried so far were these actions:
a) setting window.onbeforeunload = null;
b) setting alert, confirm, prompt to an empty function
c) settin suppressErrorMessages to true
but even so, I still get the nasty message in the end.
I mostly relied on this answer:
How to update DOM content inside WebBrowser Control in C#?
But so far without a success.
The alerts seem to be jQuery alerts because they have custom texts (instead of OK Cancel, they have Stay Leave)..
Any help hugely appreciated!!

The webbrowser control uses IE internally and IE has a prompt if you've filled out a form asking you if you really want to leave the page (thereby losing the content you've filled out) perhaps that's what you're seeing?
i'm just shooting from the hip here but you could try clearing all inputs before navigating.

Related

Confirming asp c# action on page load

I've been searching for an answer to this but haven't found what I'm looking for yet.
When I load into the page there is a check run one the server side. Depending on the output of this (bool), I wish to display a "yes no" confirm box to execute another piece of server side code.
I have found ways to do this easily enough on a button click but I'm trying to avoid adding a hidden button and simulating a click.
MessageBox.Show isn't an option in this case as I get the following error:
Showing a modal dialog box or form when the application is not running
in UserInteractive mode is not a valid operation
Is there any way to achieve this without simulating a button click?
Cheers,
Spitfire2k6
In web app (including the one created with ASP.NET) you can use Javascript confirmation dialog box: window.confirm("Request to Confirm Text");, and process User's response like in the following sample code snippet:
var _response = confirm("Please Confirm");
if (_response == true) {//Do Action1}
else {//Do Action1}
Pertinent to your case, you can use for e.g. page <body onload> event. Hope this may help.
I realised I was going about this all wrong.
The check is now done on page load, depending on the result I'm showing an asp panel with 2 asp buttons as a confirm box.
Thanks for all guidance.
Cheers,
Spitfire2k6

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.

javascript viewstate problem

I have a situation that I am stuck with, and hoping someone can help. I am building a .NET/C# web application in which I have a tabbed panel layout, and when the user clicks on each of the tabs the display panel is updated using javascript to hide and show some divs. None of these clicks cause postback, it is all client-side, so I can't use viewstate or session.
What I want to do is somehow remember which panel was last visible when the page is refreshed, yet without posting back to the server I am unsure how to do this. I have tried a hidden field but obviously its value is reset every time because the form is never submitted. I do know that I can achieve this using cookies but its a little annoying to implement for such a (seemingly) trivial operation ... but maybe this is the only way?
Does anyone have any more elegant solution to this problem?
Using a function like this to show and hide tabs):
function makeCurrent(tab) {
if (tab.title == 'Manage orders') {
document.getElementById('panelOrders').style.display = "block";
// Hide others
document.getElementById('panelAccounts').style.display = "none";
document.getElementById('panelProducts').style.display = "none";
document.getElementById('panelSettings').style.display = "none";
// Remember last viewed panel
document.getElementById('hdnCurrentlyViewing').value = "orders";
}
The panels are just divs with style.display controlling their visibility. Not sure if its useful to post HTML code because its fairly self explanatory ...?
You can make this happen without a postback is to make an AJAX call from Javascript where you tell your server what the current panel is as you switch it.
I prefer using a framework like JQuery or Prototype to help make these AJAX calls myself.
I think Hidden field will be the best option. have you tried ASP:HiddenField? It can be accessed across postbacks.
But if you still have some reservations with postbacks and hiddenfield you can also use cookies from JS http://techpatterns.com/downloads/javascript_cookies.php this is helper lib for cookies manipulation within JS.
Regards.

Issues with displaying messagebox in asp.net

I have the following message box in c# on my asp.net page inside the btnSubmit_Click event.
It tends to popup sometimes and not popup sometimes. Any reasons as to why it is not consistent?
ClientScript.RegisterStartupScript(
GetType(),
"alert",
"alert('An email has been sent to Customer Service');",
true);
I guess that this will depend on the text you are putting inside the alert. In the example you provided the text is hardcoded but I suppose that in your real application this text is dynamic and might contain characters that break javascript such as '. Try using FireBug to see if there are some javascript errors when it doesn't work.
Have You checked, if the alert('An email has been sent to Customer Service'); line is in the HTML Source after you clicked the Button and the message did NOT appear?
If it isn't in the HTML, check:
with the Debugger if your
codeblock is hit
are you maybe redirecting the response?
try these popups instead
type java directly in the visualstudio GUI
On a button go to the "OnClientClick" property (its not into events*) over there type:
return confirm('are you sure?')
it will put a dialog with cancel ok buttons transparent over current page if cancel is pressed no postback will ocure. However if you want only ok button type:
alert ('i told you so')
The events like onclick work server side they execute your code, while OnClientClick runs in the browser side. the come most close to a basic dialog
as this codes is so small it should work unless they have really strange browser clients

webBrowser control cannot find htmlElement after Ajax webpage update or in frame

Using webBrowser control in a winForm. but when the webpage is updated by Ajax or in a frame, I cannot use
webBrowser1.document.getElementById, etc. to find that htmlElement. The element also won't show in the View->Source code in IE.
The untimate purpose is to find that htmlElement and simulate a click or other function like
invokeMember("staff").
The WebBrowser's Document object does indeed represent a live view of the DOM so there may be some other reason that you're unable to find it. DOM updates will not however be represented in View -> Source. You should use IE8's developer tools which will show you a live view of the DOM and maybe you'll see something like an incorrect/duplicate ID or something.
I'm guessing you have already solved this problem on your own, but if you haven't, refer to my question here: WebBrowser Control and GetElement by ID
Essentially, if you do something to the WebBrowser control (ie, add some member to the DOM) it will do so asynchronously. That is, it does it on another thread, that way it avoids locking your calling thread when the WebBrowser is doing work. The problem is that if you programatically modify something with a command, you will have to wait till that command actually finishes loading its changes till you can work with the result of it.
Check my question there for a code example of what I was doing. I hope someone can find my previous trials useful.

Categories

Resources