0
votes

I have this scenario. This is the front end asp:button component.

<asp:Button ID="btnNewsAdd" runat="server" Text="Edit current news item" OnClientClick="return Confirm();" OnClick="btnNewsAdd_Click" UseSubmitBehavior="false"/>

This is the Javascript:

<script type="text/javascript">
function Confirm() {
   var result = window.confirm('Are you sure?');
      if (result == true)
         return true;
      else
         return false;          
}
</script>

The user should click on the "btnNewsAdd" button, the Javascript should be fired and a dialog box with yes or no options pops up. Clicking yes will return true and no will return false. If true is returned the ASP.Net method in the backend: btnNewsAdd_Click should fire. It was working up until 15 mins ago. When debugging, the JS steps over the return true line and headsinto javascript files that I did not create myself; however, the backend method does not fire.

I noticed that in the Sources section, the HTML converted to the below. As one can see the OnClick and OnClientClick automatically merged into OnClick. This was automatic and I don't know if it could be the problem or part of it.

<input type="button" name="ctl00$MainContent$btnNewsAdd" value="Edit current news item" onclick="return Confirm();__doPostBack(&#39;ctl00$MainContent$btnNewsAdd&#39;,&#39;&#39;)" id="MainContent_btnNewsAdd" />

I have literally spent a whole day trying to get around it. At a certain point it was working and it just stopped working without having changed anything. Any suggestions appreciated.

1
You may find your answer stackoverflow.com/questions/14058116/…Rex
Thanks it helped me a lot. I will post an answer with the solution.Jurgen Cuschieri

1 Answers

0
votes

As pointed by Rex, and as explained by several within this thread: See other thread

by changing the button to:

<asp:Button ID="btnNewsAdd" runat="server" Text="Edit current news item" OnClick="btnNewsAdd_Click" OnClientClick="if (!Confirm()) return false;"/>

and the Javascript to:

<script type="text/javascript">
function Confirm() {
   return confirm("Are you sure?");         
}
</script>

problem is solved.