简体   繁体   中英

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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM