简体   繁体   English

从OnClientClick javascript函数返回true后,在方法后面执行OnClick代码

[英]Executing OnClick code behind method after returning true from OnClientClick javascript function

I have this scenario. 我有这种情况。 This is the front end asp:button component. 这是前端的asp:button组件。

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

This is the Javascript: 这是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. 用户应单击“ btnNewsAdd”按钮,应触发Javascript,并弹出一个对话框,显示是或否选项。 Clicking yes will return true and no will return false. 单击是将返回true,而否将返回false。 If true is returned the ASP.Net method in the backend: btnNewsAdd_Click should fire. 如果返回true,则后端中的ASP.Net方法将被触发:btnNewsAdd_Click。 It was working up until 15 mins ago. 一直工作到15分钟前。 When debugging, the JS steps over the return true line and headsinto javascript files that I did not create myself; 调试时,JS越过return true行并将headsin移到我自己创建的javascript文件中; however, the backend method does not fire. 但是,后端方法不会触发。

I noticed that in the Sources section, the HTML converted to the below. 我注意到在“源”部分中,HTML转换为以下内容。 As one can see the OnClick and OnClientClick automatically merged into OnClick. 可以看到OnClick和OnClientClick自动合并到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. 任何建议表示赞赏。

As pointed by Rex, and as explained by several within this thread: See other thread 正如Rex所指出的,以及该线程中的一些解释: 请参阅其他线程

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: 和Javascript可以:

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

problem is solved. 问题解决了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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