c# - Calling a confirm dialog box on code behind and get the chosen option - Stack Overflow

I'm developing an asp application and I need to call a confirm dialog box on the code behind. And

I'm developing an asp 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.

I'm developing an asp 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.

Share Improve this question asked Nov 22, 2012 at 19:57 FernandoFernando 1041 silver badge9 bronze badges 0
Add a ment  | 

3 Answers 3

Reset to default 1

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 bee 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 remended, but I used because it won't cause me problems. It's remended 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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745361801a4624395.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信