[英]How to disable postback on an asp button and still run OnClick method?
Yes, it is possible to simply add a return false to the OnClientClick, but that's not doing a very good job since the function on the OnClick just stops working. 是的,可以简单地向OnClientClick添加return false,但这做得不好,因为OnClick上的功能刚刚停止工作。
The function that i use on the OnClick property on the button takes data out of the database and does something with it. 我在按钮上的OnClick属性上使用的功能将数据从数据库中取出并对其进行处理。 while i have that return false there, nothing is happening.
虽然我在那儿得到的回报是假的,但没有任何反应。 When it's not there, the page keeps refreshing and nothing is done.
如果不存在该页面,则页面会不断刷新,并且什么也没做。
Please, if anyone got a way to block the postback without blocking the method on the OnClick property that'll be great! 请,如果有人能阻止回发而又不阻止OnClick属性上的方法,那就太好了!
HTML 的HTML
<asp:Button Text="Edit Ceredntials" OnClientClick="ShowWindow('#EditDetails', 2500)" OnClick="GetUserData" runat="server" />
You can't prevent a postback and still run the OnClick
method. 您不能阻止回发,但仍可以运行
OnClick
方法。 That's because OnClick
runs on the server, and the only way it can execute is via a postback. 这是因为
OnClick
在服务器上运行,并且其执行的唯一方法是通过回发。
If what you're really trying to accomplish is preventing the page from losing state and refreshing, you should be using AJAX instead and a compatible server side framework, like Web API . 如果您真正要完成的工作是防止页面丢失和刷新,则应该改用AJAX和兼容的服务器端框架,例如Web API 。
<script type="text/javascript">
function loadUserDetails(){
$.ajax({
url: 'api/Users/2500',
type: 'GET'
})
.success(function(userDetails){
$("#FirstName").val(userDetails.FirstName);
ShowWindow('#EditDetails', 2500);
});
}
</script>
<asp:Button runat="server" OnClientClick='loadUserDetails()' Text="Edit Credentials" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.