views:

263

answers:

5

How can I block postback caused by an ASP.NET Button (or, any other) server control?

I.e. I shall push a Button but no postback will occur.

+3  A: 

do this

button.Attributes.Add("onclick", "return false;")

on page load.

Eric
+1  A: 

you could use the

OnClientClick="return false;"

property of the button

ghills
not everything has an onclient click!!!
Eric
oops I was focused on the button. Slipped my mind that he wanted it to work for any control.
ghills
A: 
<asp:button runat="server" ... OnClientClick="return false" />
Max
not every control has onclient click!!
Eric
A: 

You could use AJAX controls that do callbacks instead of postbacks for another possibility besides the Javascript "return false;" answer.

JB King
A: 

You can make the client side validation fail for the control's validation group. Depending on what you're trying to do, that might be a nicer solution.

You can piggyback on the JavaScript run for each postback as well, but showing you from the top of my head while typing on a mobile phone... maybe not. I'll update this post when I get home.

Thorarin