Creating a Button that is type="button" and does not call __doPostBack

  • Thread starter Thread starter Nathan Sokalski
  • Start date Start date
N

Nathan Sokalski

I want to create a Button that does not do a postback (I simply want to use
it for JavaScript). Some of the ways I know of to do this are:

1. Include "return false;" in the OnClientClick property. However, if
UseSubmitBehavior is set to True, it is type="submit", and if
UseSubmitBehavior is set to False, onclick still includes __doPostBack even
though it doesn't get called
2. Use an HtmlButton control. However, I would like to be able to use a
Button control.

Are there any ways to create a Button that does not do a postback? Thanks.
 
Are there any ways to create a Button that does not do a postback?

Use an HtmlControl instead of a server control and make sure you do not
set runat="server" is your best option. I am not sure why you do not
want to do this, as it is the best option when you do not want a
postback.

If absolutely MUST neuter an ASP.NET server control button, then do
something like:

<asp:Button ID="ButtonName" runat="server" Text="Click for JavaScript"
OnClientClick="return functionName()" />

You then have to ensure the function returns false:

<script type="text/javascript" language=javascript>

function functionName() {
alert('Look ma, no server script!');
return false;
}

</script>


The return false effectively neuters the button.

Another option is to subclass the button server control and neuter it.
It will end up with nearly identical code to the HtmlControl, however.
But it will retain some of the "goodness" of an button server control.

Peace and Grace,

--
Gregory A. Beamer (MVP)

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

*******************************************
| Think outside the box! |
*******************************************
 
Back
Top