Confirming asp c# action on page load - c#

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

Related

C# WebBrowser turn off jquery popups

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.

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 "";

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

ClientScript.RegisterClientScriptBlock?

In my web application when i upload a video and click the save button, if the video is uploaded i write the code to display the message video is uploaded. My code is as follows:
ClientScript.RegisterClientScriptBlock(GetType(), "sas", "<script> alert('Inserted successfully');</script>", false);
When the alert box appears is comes with a white background. I clicked on ok button in that alert box but the page is not going back to the previous page it is showing the same white space.
Can you solve the problem.? If you not understand i will explain clearly.
In local it is working fine but when i update in online it is not working.
Hai sridhar,
I found an answer for your prob
ClientScript.RegisterClientScriptBlock(GetType(), "sas", "<script> alert('Inserted successfully');</script>", true);
change false to true
or try this
ScriptManager.RegisterClientScriptBlock(ursavebuttonID, typeof(LinkButton or button), "sas", "<script> alert('Inserted successfully');</script>", true);
The method System.Web.UI.Page.RegisterClientScriptBlock has been deprecated for some time (along with the other Page.Register* methods), ever since .NET 2.0 as shown by MSDN.
Instead use the .NET 2.0 Page.ClientScript.Register* methods.
- (The ClientScript property expresses an instance of the ClientScriptManager class )
Guessing the problem
If you are saying your JavaScript alert box occurs before the page's content is visibly rendered, and therefore the page remains white (or still unrendered) when the alert box is dismissed by the user, then try using the Page.ClientScript.RegisterStartupScript(..) method instead because it runs the given client-side code when the page finishes loading - and its arguments are similar to what you're using already.
Also check for general JavaScript errors in the page - this is often seen by an error icon in the browser's status bar. Sometimes a JavaScript error will hold up or disturb unrelated elements on the page.
See if the below helps you:
I was using the following earlier:
ClientScript.RegisterClientScriptBlock(Page.GetType(), "AlertMsg", "<script language='javascript'>alert('The Web Policy need to be accepted to submit the new assessor information.');</script>");
After implementing AJAX in this page, it stopped working. After reading your blog, I changed the above to:
ScriptManager.RegisterClientScriptBlock(imgBtnSubmit, this.GetType(), "AlertMsg", "<script language='javascript'>alert('The Web Policy need to be accepted to submit the new assessor information.');</script>", false);
This is working perfectly fine.
(It’s .NET 2.0 Framework, I am using)

How do I insert text into a textbox after popping up another window to request information?

I have an asp.net web page written in C#.
Using some javascript I popup another .aspx page which has a few controls that are filled in and from which I create a small snippet of text.
When the user clicks OK on that dialog box I want to insert that piece of text into a textbox on the page that initial "popped up" the dialog/popup page.
I'm guessing that this will involve javascript which is not a strong point of mine.
How do I do this?
You will have to do something like:
parent.opener.document.getElemenyById('ParentTextBox').value = "New Text";
What you could do is create an ajax modal pop-up instead of a new window. The semantic and aesthetic value is greater not to mention the data-passing is much easier.
http://www.asp.net/ajax/ajaxcontroltoolkit/samples/modalpopup/modalpopup.aspx

Categories

Resources