Home Ask Login Register

Developers Planet

Your answer is one click away!

Chris Geo February 2016

aspx button prevent postback onclick (c#)

The code below shows some HTML with a <asp:Button> element.

<asp:Button ID="CalculateG481" runat="server" Text="Calculate" OnClick="CalculateG481_Click" />

This calls the function CalculateG481_Click but then posts back.

I have tried using OnClientClick="return false" but this simply prevents the button from executing CalculateG481_Click

I have also tried OnClick="CalculateG481_Click; return false;" as well to no avail.

EDIT: CalculateG481_Click is a C# Codebehind function.


alec February 2016

Try setting OnClientClick to:

"return CalculateG481_Click();"

Win February 2016

The way you are doing is really strange - you cannot call Server-Side Button Event from Client-Side JavaScript.

You can either use regular input html tag with pure Client-Side calculation or Server Button Control with Ajax based on your requirement.

Easiest way is to use Ajax UpdatePanel, and place everything inside it.

Post Status

Asked in February 2016
Viewed 3,717 times
Voted 4
Answered 2 times


Leave an answer

Quote of the day: live life