简体   繁体   中英

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.

The function that i use on the OnClick property on the button takes data out of the database and does something with it. 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!

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. That's because OnClick runs on the server, and the only way it can execute is via a postback.

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 .

<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" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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