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

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.
 
G

Gregory A. Beamer

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! |
*******************************************
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,990
Messages
2,570,211
Members
46,796
Latest member
SteveBreed

Latest Threads

Top