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

Discussion in 'ASP .Net Web Controls' started by Nathan Sokalski, Jan 11, 2010.

  1. 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.
    --
    Nathan Sokalski

    http://www.nathansokalski.com/
    Nathan Sokalski, Jan 11, 2010
    #1
    1. Advertising

  2. "Nathan Sokalski" <> wrote in
    news::

    > 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! |
    *******************************************
    Gregory A. Beamer, Jan 11, 2010
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?QUpQ?=

    __doPostBack manual call

    =?Utf-8?B?QUpQ?=, Aug 20, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    1,030
    =?Utf-8?B?QUpQ?=
    Aug 20, 2004
  2. SteveSu
    Replies:
    2
    Views:
    17,036
    SteveSu
    Nov 8, 2005
  3. =?Utf-8?B?Q2xhcw==?=
    Replies:
    0
    Views:
    1,490
    =?Utf-8?B?Q2xhcw==?=
    Nov 10, 2005
  4. Arch
    Replies:
    2
    Views:
    813
    leonidmk
    Aug 12, 2012
  5. Nathan Sokalski
    Replies:
    1
    Views:
    888
    Gregory A. Beamer
    Jan 11, 2010
Loading...

Share This Page