Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling a confirm dialog box on code behind and get the chosen option

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.

like image 232
Fernando Avatar asked Nov 22 '12 19:11

Fernando


People also ask

How to call Confirm box from code behind in asp net?

1) Form Design <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> <script type = "text/javascript"> function Confirm() { var confirm_value = document. createElement("INPUT"); confirm_value. type = "hidden"; confirm_value.name = "confirm_value"; if (confirm("Do you want to save data?")) { confirm_value.

What is the purpose of the confirm dialogue box?

A confirm box is often used if you want the user to verify or accept something. A confirm box takes the focus away from the current window, and forces the user to read the message.

What is Confirm() in HTML?

confirm() instructs the browser to display a dialog with an optional message, and to wait until the user either confirms or cancels the dialog.


1 Answers

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.

like image 72
Darren Wainwright Avatar answered Nov 10 '22 07:11

Darren Wainwright