I wants to display an OK/CANCEL message box. I have written like this.
if (MessageBox.Show("Are you sure you wants to Save the details ? ", "Validate", MessageBoxButtons.OKCancel) == DialogResult.OK)
{
// do something if "OK "
}
it works locally well.. but on IIS shows an error " Showing a modal dialog box or form when the application is not running in UserInteractive mode is not a valid operation. Specify the ServiceNotification or DefaultDesktopOnly style to display a notification from a service application."
please help.
To get the effect I believe you are wanting, you'll want to use the Javascript confirm() function. It is typically used like this:
<asp:Button runat="server" OnClientClick="return confirm('Are you sure you want to save the details?')" id="btnSubmit" OnClick="btnSubmit_Click" />
This would, upon clicking the button, display a confirmation box that would stop the server OnClick event from firing if the user clicks No, Cancel, etc.
OnClientClick will render as an onclick event on the <input> tag. The rest is determined by the browser's handling of Javascript. See What's the effect of adding 'return false' to a click event listener? for more details on what the return value of the code in OnClick (OnClientClick) does.
Here's an example of the return values of confirm() using Chrome. For the first execution I had clicked Cancel.
Related
I'm developing an asp.net application and I need to call a confirm dialog box on the code behind. And after that, I need to get the user's click (if he clicked at OK or Cancel).
If he clicked the OK button of the dialog, I must continue to running the code where I stopped before call the dialog.
To solve this problem I thought in put at the aspx an input of type button, and set it's style to visibility:hide. So, when the users click on the OK button the programm will call this hidden button which calls a method that will continue the job stopped.
I'll post my code, I expect it might help.
The code below is on my code behind, and it calls the confirm dialog.
System.Web.UI.
ScriptManager.RegisterClientScriptBlock(this, GetType(), "__Mensagem", "if (confirm('" + CustomMessage + "')){document.getElementById(\'<%=btnSave.ClientID%>\').click();}", true);
The code below is on my aspx, it's the "hidden" button.
<input style="visibility:hidden;" id="btnSave" runat="server" onclick="btnSave_Click"/>
I don't know why isn't working. The error that I receive after click on the button OK of the confirm dialog box is the following: Erro em tempo de execução do Microsoft JScript: 'document.getElementByID(...)' é nulo ou não é um objeto
I'm from Brazil so the error is in portuguese, a translation to english it's something like this:
"A runtime error occurred on Microsoft JScript 'document.getElementByID(...)' is null or is not an object"
I give a look at the html code of the page and I notice that the button isn't there.
Maybe it's because I'm using an UpdatePanel, but when I removed it (just for tests, I must use the Update Panel), the same error is showed, and in this time the button were on the page's html code.
A stab in the dark.
In your code-behind have you set btnSave.visible = false;? If so, this would prevent the button from being rendered to the HTML.
Also, is there any reason you're not using the ASP:Button control, rather than a mix of HTML with runat="server"?
Finally, it may help to have type='button' on your <input....
UPDATE
The problem is you're trying to apply an inline tag
document.getElementById(\'<%=btnSave.ClientID%>\')
in your code be <%=btnSave.ClientID%> does not exist yet.
your code will break the moment you put your button inside another server control, as the clientId will become different.
Change the code to this:
document.getElementById('" + btnSave.ClientID + "')
and it will work, regardless of where you have the button.
Once your page is rendered in browser, right click on it, and say "view Source". check the HTML and try to find your hidden button, if you find it, use its id in your statement getElementById(\'<%=btnSave.ClientID%>\') . If you dont find your button, then probably you have set its visibility to false from your code-behind somewhere, remove that, follow the above process again and you should be ok.
I solved the problem replacing the part
document.getElementById(\'<%=btnSave.ClientID%>\').click();
by
$(\"#ctl00_body_btnSave\").click();
I also replace the input element by an asp:Button.
Give a look how my code is on my code behind now:
System.Web.UI.
ScriptManager.RegisterClientScriptBlock(this, GetType(), "__Mensagem", "if (confirm('" + CustomMessage+ "')){$(\"#" + btnSave.ClientID + "\").click();}", true);
And on my aspx:
<asp:Button id="btnSave" Width="0px" Text="" runat="server" onclick="btnSave_Click"/>
The use of Widht="0px" and Text="" to hidden the button isn't recommended, but I used because it won't cause me problems. It's recommended the use of a CssClass which make it hide.
So, if the user confirm the operation, the even btnSave_Click is called.
Thanks for all the answers.
Here is my code :
<script type="text/javascript">
$(document).ready(function () {
$(window).bind('beforeunload', function () {
alert("unload");
if (closeIt())
$("#<%=Button1.ClientID %>").trigger('click');
});
function closeIt() {
var ans = confirm("save current layout ?");
if (ans) return true;
}
});
</script>
<asp:Button ID="Button1" runat="server" OnClick="btnSaveState_Click" style="display:none;" />
the new problem is that the confirm message is displayed twice on firefox and non of chrome
All the beforeunload handler is really supposed to do is return a string, which the browser then displays in an "are-you-sure-you-want-to-leave" dialog. If the user clicks OK, whatever navigation was about to happen occurs; if they click Cancel, it doesn't. Take a look here for more detail.
The usual thing to do here would be to display a message (by returning a string, not calling confirm yourself) along this lines of "You're about to lose the changes you've made to the current layout; are you sure you want to leave?" and then let the user themselves click Cancel and then Save, or OK if they don't care.
You're having issues, I expect, because you're trying to perform a postback in the handler, and the postback itself would cause an unload. I wouldn't be surprised if the browser deliberately stops this kind of behaviour in the handler, because malicious sites could use it to stop you leaving.
As per your update: only some browsers even allow beforeunoad behaviour. By the looks of it, Firefox does, so you get two dialogs - your confirm and then the browser default one. And by the looks of it, Chrome doesn't, so your event never gets called. (Or maybe Chrome just ignores the event if it does something unexpected, like post back.)
Try this:
<asp:Button ID="Button1" runat="server" Text="Button" CausesValidation="False" onclick="btnSaveState_Click" onclientclick="return confirm('Are you sure you want to delete?')" />
Alternatively you can look at the ASP.Net Ajax <ajaxToolkit:ConfirmButtonExtender>
I want to display a alert message when a user clicks on button.
I have a button "Signed" on click of this button I want to display a alert message saying "Are you sure you want to continue?" and two buttons in this alert message box "Yes" and "No".
On click of yes button I want a update function to be executed and on click of "No" only Alert should get closed and no changes in the current page.
Please anyone can guide me how to go on with this?
You can use the OnClientClick on the button for this.
Add a return confirm(); to it. It will create a javascript confirm dialog, BEFORE it fires the click event.
If the user presses no, it will not trigger the OnClick event.
OnClientClick="return confirm('Are you sure you want to continue?');"
So, added to your button markup, it would look like this:
<asp:Button ID="Button5" runat="server" Text="Signed" Visible="False" onclick="Button5_Click" OnClientClick="return confirm('Are you sure you want to continue?');" />
jquery should do for you.
$("#ButtonId").click({
return confirm("Are you sure you want to continue");
})
I have to show a yes no popup messagebox for a function>
This is what i do for an alert popup>
Page.ClientScript.RegisterStartupScript(this.GetType(), "Alert", "<script>alert('File Updated');</script>");
This is what i want to do in the code behind:
if (ID != 0)
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "Confirm", "<script>confirm('are you sure?');</script>");
if (yes)
{
perform function
}
else
{
return;
}
}
The confirm is not working,,, any suggestions on how to do this
thanks
Edit Portion:
Navigate to a page
Add values to a textbox
Click "Save" Button to add value to database
Ask the user if he is sure he want to do it 'are you sure'?,, the confirm pop up
Now the above confirm box will only happen if the ID is != 0 or else there is no need for a popup.
if he says yes then add to database and show alert popup that values have been enterd in the DB.
if NO then just dont add to Db and just return.
so i get the confirm box like this.. but how can i get what is selected
string scriptString = "<script language='JavaScript'> ";
scriptString += "confirm ('Are you sure you want to Close this period.')";
scriptString += "</script>";
Response.Write(scriptString);
Is there a button you are clicking on to trigger the action? If so, you should add the client events to your web control like so:
<asp:ImageButton runat="server" ID="DeleteUrlImageButton" ImageUrl="~/Images/icon_remove.gif"
OnClick="DeleteUrlImageButton_Clicked"
OnClientClick="return confirm('Are you sure you want to delete?');" />
If the user selects yes, the postback happens as usual. If they select no, the even is cancelled and no postback occurs. This, IMO, is the way to handle it because it prevents any extra server activity if they select no.
Add a linkbutton.
In the OnClientClick add
javascript:return confirm('Are you sure')
This will not launch the postback if they click no. It will launch the postback if they click yes.
Then in then code behind (OnClick) of the button do your server side processing:
(Will only be executed if they click yes)
if (ID != 0)
{
Perform function
}
See the problem here is that, without posting back, you can't get the value of the confirm box. JavaScript is run client-side and until a postback occurs (either via ajax or the regular way), it can't "talk" to your C# file.
What you'll have to do is add a confirm box in JavaScript which, if Yes is clicked, will post back to your Asp.net page and run code either through Ajax or (example) form.submit().
It appears that what you're trying to do is (in a simplified scenario):
Have the user navigate to Page.aspx
Check the value of ID (lets assume
it's a querystring parameter)
If the value of ID is non-zero, prompt
the user to confirm
If they confirm do "something"
The mistake you're making is attempting to handle 2, 3 and 4 alltogether in the code-behind. The script that you emit (by calling RegisterStartupScript) doesn't get executed until the entire page has been rendered back to the user, at which point the code for steps 3 and 4 to check the value of ID and do something will already have been "skipped over"
What you need to do is decide how to separate the work between client-site and server-side as what you're attempting to do just won't work. Without knowing how your page(s) work and where the ID value comes from I can't give a speciic example, but, something like:
Have your page check ID to see if it hits your criteria, if it does, emit the javascript, but with some additional javascript that checks the response to the prompt and causes the page to re-submit but with confirmed=yes added on the querystring
Have your page check the querystring parameter "confirmed" to see if it's yes. If it is, THEN do the work
You can't do it this way. RegisterStartupScript just registers the script to be included when the page is finally rendered. After it is rendered (to HTML) then the html is sent to the browser. So when the user finally sees that popup, your code has long since finished.
EDIT:
See the answer by Mike C.: you need to move that confirm to just before the submit.
Page.ClientScript.RegisterStartupScript will register that script on the client. As far as I can see you are executing the if statement on the server. Could you please specify what confirm is not working mean? Is the alert box displaying but no effect should yes/no is pressed? If so, move the if ... else statement on the client. Anyway, I suggest that you replace RegisterStartupScriptBlock with this code:
ClientScript.RegisterClientScriptBlock
(this.GetType(), "confirm", "alert('do')", true);
I need some jquery code in my application I have a button on the page and I want to handle it by jquery when the user click on it to show a confirmation dialog for example Are you sure? yes|no buttons but this is an asp.net button inside the updatepanel and in the other side I have some server side code to delete a record from database but my question is how I can handle both of them? server side and jquery inorder to when "yes" button clicked it runs server side and delete recorde from database and if the button is no it stop running ?
you need to use confirm javascript function
<asp:button id="fooBtn" runat="server" OnClientClick="return confirm("Are you sure?") />
Here is a link to confirm function.