从OnClientClick javascript函数返回true后,在方法后面执行OnClick代码 [英] Executing OnClick code behind method after returning true from OnClientClick javascript function
问题描述
我有这种情况.这是前端的asp:button组件.
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"/>
这是Javascript:
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>
用户应单击"btnNewsAdd"按钮,将触发Javascript,并弹出一个对话框,显示是"或否"选项.单击是将返回true,而否将返回false.如果返回true,则后端中的ASP.Net方法将被触发:btnNewsAdd_Click.一直工作到15分钟前.调试时,JS越过return true行并将headsin移到我自己没有创建的javascript文件中;但是,后端方法不会触发.
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.
我注意到在源"部分中,HTML转换为以下内容.可以看到OnClick和OnClientClick自动合并到OnClick中.这是自动的,我不知道这可能是问题还是其中的一部分.
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('ctl00$MainContent$btnNewsAdd','')" 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
将按钮更改为:
<asp:Button ID="btnNewsAdd" runat="server" Text="Edit current news item" OnClick="btnNewsAdd_Click" OnClientClick="if (!Confirm()) return false;"/>
和Javascript:
and the Javascript to:
<script type="text/javascript">
function Confirm() {
return confirm("Are you sure?");
}
</script>
问题解决了.
这篇关于从OnClientClick javascript函数返回true后,在方法后面执行OnClick代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!