user3561923
user3561923

Reputation:

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

Upvotes: 0

Views: 4311

Answers (1)

mason
mason

Reputation: 32694

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

Upvotes: 3

Related Questions