[英]OnClientClick not passing to OnClick code behind on return of true in my ASP.NET web solution
I have followed several solution suggestions for this problem but nothing is working properly. 我已针对此问题遵循了一些解决方案建议,但是没有任何工作正常。 My desired outcome is to fire off a JavaScript validation function before executing the C# code behind method upon successful validation return of true.
我希望得到的结果是在成功返回true时在执行C#代码落后方法之前触发JavaScript验证功能。 OnClick should be followed after the javascript OnClientClick operation.
在执行JavaScript OnClientClick操作之后,应遵循OnClick。
I have tried suggestions from this thread: 我尝试过此线程的建议:
Executing OnClick code behind method after returning true from OnClientClick javascript function 从OnClientClick javascript函数返回true后,在方法后面执行OnClick代码
as well as this thread: 以及此线程:
Executing OnClick code behind method after returning true from OnClientClick javascript function 从OnClientClick javascript函数返回true后,在方法后面执行OnClick代码
This is my current layout: 这是我当前的布局:
<asp:Button ID="btnAdd" runat="server" AutoPostBack="true" OnClick="btnAdd_Click" OnClientClick="btnAdd_Click(this, event);" Text="Add" UseSubmitBehavior="false"/>
function btnAdd_Click(sender, args) {
if(confirm('Do you want to do a server trip?'))
{
return true;
}
else
{
return false;
}
}
I have also tried: 我也尝试过:
<asp:Button ID="btnAdd" runat="server" AutoPostBack="true" OnClick="btnAdd_Click" OnClientClick="if (!Confirm()) return false;" Text="Add" UseSubmitBehavior="false"/>
function Confirm() {
return confirm("Are you sure?");
}
What i am seeing is JavaScript is firing off every time. 我所看到的是JavaScript每次都会触发。 Debugging JS proves that it is returning true back to my solution, so the OnClick should get hit.
调试JS证明它可以返回我的解决方案,因此OnClick应该会受到欢迎。 I have a break point in my click event on the code behind but it never gets hit.
我的click事件背后的代码有一个断点,但它从未被击中。 Is there a site setting or something preventing this?
是否有网站设置或阻止此设置的措施?
Help is much appreciated. 非常感谢您的帮助。 Thanks in advance.
提前致谢。
Try this...you really don't even need that JS function. 尝试一下...您甚至根本不需要该JS函数。
<asp:Button ID="btnAdd" runat="server" OnClientClick="return window.confirm('Are you sure?');" Text="Add" OnClick="btnAdd_Clicked"/>
protected void btnAdd_Clicked(object sender, EventArgs eventArgs)
{
string test = "12345";
}
If you debug the btnAdd_Clicked function, you'll see it hits the first line of code there. 如果调试btnAdd_Clicked函数,您将看到它到达了代码的第一行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.