6

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.

Fernando
  • 104
  • 1
  • 9

3 Answers3

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 become different.

Change the code to this:

     document.getElementById('" + btnSave.ClientID + "')

and it will work, regardless of where you have the button.

Darren Wainwright
  • 30,247
  • 21
  • 76
  • 127
  • Hi Darren, Thanks for the answer. I checked many times and there isn't a single line where I reffer to this button on my code behind. I tried to use an asp:Button before try the input, both didn't work and return the same error. I forgot to put the attribute type="button" on my tag when I copy and paste it here, I had removed it few minutes before to try the type="submit", which also didn't work. I'm trying other shots and if I solve the problem I'll post here. Thanks again. – Fernando Nov 23 '12 at 13:55
  • Can i clarify, you say that this button does not get rendered when you view source? – Darren Wainwright Nov 23 '12 at 14:10
  • Hi man, I solve the problem. I appreciate your help. I'll post the solution – Fernando Nov 23 '12 at 16:29
  • See update - i figured the issue, also show that you will have a problem in the future.. – Darren Wainwright Nov 23 '12 at 17:16
0

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.

patil.rahulk
  • 574
  • 1
  • 3
  • 13
0

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.

Fernando
  • 104
  • 1
  • 9
  • You will run into the same problem the moment you move your button into another control, such as a placeholder, as the ClientID will become different. I have just come up with the actual solution, see my answer for update – Darren Wainwright Nov 23 '12 at 17:12
  • You're right. I update the solution above. Still working and now it's safer if the button is moved inside to another control. Thanks for your advice. – Fernando Nov 23 '12 at 17:36
  • You could use `ClientIDMode="Static"` (see [documentation](http://msdn.microsoft.com/en-us/library/system.web.ui.control.clientidmode.aspx)). – nrodic Nov 23 '12 at 17:58
  • @nrodic - depends on .net version – Darren Wainwright Nov 23 '12 at 17:59
  • 1
    @nrodic, unfortunately this is a kind of "ancient" system, and it's framework version is 3.5, so I can't use a static clientId – Fernando Nov 23 '12 at 18:03