Block an asp:Button OnClick event in C# from the OnClientClick event in JavaScript?

醉酒当歌 提交于 2019-12-04 15:56:29
Tim Schmelter

To block OnClick from executing, just return false from OnClientClick:

<asp:Button OnClick="Button_Click" ID="Action1" OnClientClick="return SaveValues();"
    Text="Action1" CssClass="specialButton" runat="server" />

Assuming that SaveValues will also return a boolean whether or not the condition was met.

yes it is possible

function SaveValues()
{
if(//check your codition here)
{
return true;
}
else
{
return false;
}
}

<asp:Button OnClick="Button_Click" ID="Action1" OnClientClick="javascript:return SaveValues();" Text="Action1" CssClass="specialButton" runat="server" />

Returning true or false, according to the success of the JavaScript, should prevent or enable the button to submit. So, instead of just tacking it on at the end of OnClientClick, where you'd have to conditionalise it and immediately make your call look much more ugly, let the method indicate its operational status.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!