Button With No PostBack

Discussion in 'ASP .Net' started by Jeremy, Jun 27, 2004.

  1. Jeremy

    Jeremy Guest

    I have an ASPX page with a bunch of System.Web.UI.WebControls.Button
    controls on it. By default, clicking on any of these causes a Postback. I'd
    like to have it so that for a couple of these buttons, no PostBack occurs -
    and rather some client-side script is executed (with no postback
    subsequently occuring). I have wired up the client-side script to the
    Buttons in question using Attributes.Add(blah blah blah) - now I just need
    to somehow prevent the Postback from occuring. I looked to set
    AutoPostBack="false" - but that isn't apparently an option. How can I have
    Buttons that do not trigger a postback?

    Thanks.
     
    Jeremy, Jun 27, 2004
    #1
    1. Advertising

  2. Trevor Benedict R, Jun 27, 2004
    #2
    1. Advertising

  3. Jeremy

    DalePres Guest

    Like Trevor said, use an HTML button. Inspite of the web-control-centric
    view in Visual Studio.net, developers should carefully consider whether a
    standard HTML control would work as well as a server control and default to
    the HTML control...

    But with that said, the answer to your question about the button control is
    to assign a value to the CommandName property. With no CommandName value,
    the button acts like a submit button. With a CommandName value, it will act
    more like a standard button.

    Dale

    "Trevor Benedict R" <> wrote in message
    news:%...
    > Checkout the HTMLButton COntrol
    >
    > http://samples.gotdotnet.com/quickstart/aspplus/samples/webforms/ctrlref
    > /htmlctrl/HtmlButton/doc_button.aspx
    >
    > Regards
    >
    > Trevor Benedict R
    > MCSD
    >
    > *** Sent via Devdex http://www.devdex.com ***
    > Don't just participate in USENET...get rewarded for it!
     
    DalePres, Jun 27, 2004
    #3
  4. Jeremy,

    As far as I understand, your problem is that when you click on a
    Button1 (which is WebControls.Button), it calls javascript function
    doSomething() and then posts the form? and you dont want it to
    post the form just run javascript?

    If that is the case, then here is the solution.

    we know that if we do this: Button1.Attributes.Add("OnClick", "return
    false;")
    Button1 will become non-functional, and it will not do anything when
    user clicks it.

    now suppose you have this javascript code in your web form:
    <script language=javascript>
    function doCalculate()
    {
    alert('I am called');
    return false;
    }
    </script>

    now if you do this: Button1.Attributes.Add("OnClick", "return
    doCalculate();")

    doCalculate will execute and return 'false'.. so your web form will not be
    posted back. There maybe some cases when you want to return true
    (you want to post the form after some work)

    Your question is already answered tho, but there
    maybe soem cases where you want to use WebControls.Button
    (to access it easily from codebehind) .but if the button have nothing
    to do with server, then why not use HtmlButton control as my
    fellow Trevor suggested?

    --
    Hope this helps,
    Zeeshan Mustafa, MCSD

    "Jeremy" <> wrote in message
    news:e12HT$...
    > I have an ASPX page with a bunch of System.Web.UI.WebControls.Button
    > controls on it. By default, clicking on any of these causes a Postback.

    I'd
    > like to have it so that for a couple of these buttons, no PostBack

    occurs -
    > and rather some client-side script is executed (with no postback
    > subsequently occuring). I have wired up the client-side script to the
    > Buttons in question using Attributes.Add(blah blah blah) - now I just need
    > to somehow prevent the Postback from occuring. I looked to set
    > AutoPostBack="false" - but that isn't apparently an option. How can I have
    > Buttons that do not trigger a postback?
    >
    > Thanks.
    >
    >
     
    M. Zeeshan Mustafa, Jun 27, 2004
    #4
  5. M,
    Personally, I don't use the asp:Button. I tend to always use <input
    type="button"/> and then if I need to modify it on the server, I add
    runat="server". If all I need is javascript, then I don't even make it a
    server control. And for submit post-back processing, I just set the
    ServerClick event, and it posts back. That way I don't run into problems
    with multiple buttons on my form and having a inadvertant click of the enter
    button trigger a post-back.

    Best regards,
    Jeffrey Palermo

    "M. Zeeshan Mustafa" <> wrote in message
    news:%...
    > Jeremy,
    >
    > As far as I understand, your problem is that when you click on a
    > Button1 (which is WebControls.Button), it calls javascript function
    > doSomething() and then posts the form? and you dont want it to
    > post the form just run javascript?
    >
    > If that is the case, then here is the solution.
    >
    > we know that if we do this: Button1.Attributes.Add("OnClick", "return
    > false;")
    > Button1 will become non-functional, and it will not do anything when
    > user clicks it.
    >
    > now suppose you have this javascript code in your web form:
    > <script language=javascript>
    > function doCalculate()
    > {
    > alert('I am called');
    > return false;
    > }
    > </script>
    >
    > now if you do this: Button1.Attributes.Add("OnClick", "return
    > doCalculate();")
    >
    > doCalculate will execute and return 'false'.. so your web form will not be
    > posted back. There maybe some cases when you want to return true
    > (you want to post the form after some work)
    >
    > Your question is already answered tho, but there
    > maybe soem cases where you want to use WebControls.Button
    > (to access it easily from codebehind) .but if the button have nothing
    > to do with server, then why not use HtmlButton control as my
    > fellow Trevor suggested?
    >
    > --
    > Hope this helps,
    > Zeeshan Mustafa, MCSD
    >
    > "Jeremy" <> wrote in message
    > news:e12HT$...
    > > I have an ASPX page with a bunch of System.Web.UI.WebControls.Button
    > > controls on it. By default, clicking on any of these causes a Postback.

    > I'd
    > > like to have it so that for a couple of these buttons, no PostBack

    > occurs -
    > > and rather some client-side script is executed (with no postback
    > > subsequently occuring). I have wired up the client-side script to the
    > > Buttons in question using Attributes.Add(blah blah blah) - now I just

    need
    > > to somehow prevent the Postback from occuring. I looked to set
    > > AutoPostBack="false" - but that isn't apparently an option. How can I

    have
    > > Buttons that do not trigger a postback?
    > >
    > > Thanks.
    > >
    > >

    >
    >
    >
    >
     
    Jeffrey Palermo [MCP], Jun 28, 2004
    #5
  6. Jeremy

    PGScannell

    Joined:
    Oct 14, 2011
    Messages:
    1
    Button click behaviour on an ASP.NET form

    I am having a heck of a time. Clicking on a button is causing a postback. Everything i have seen so far in 3 forums doesn't address the real problem. First of all, why does clicking a button cause a postback? not every button click should be treated like a submit.

    I have a page with several buttons. there is valuable variable settings behind the scenes that HAVE to be maintained when a user clicks a button. therefore, all i want to have happen when a button is clicked is for its event handler to fire.

    Someone recommended putting a CommandName property setting. that didn't work.

    Thanks in advance,
    Paul
     
    PGScannell, Oct 14, 2011
    #6
  7. Jeremy

    gnostic

    Joined:
    May 15, 2012
    Messages:
    1
    you have a html button that you don't want it to do post back, just change the type parameter of button from "submit" to "button", cause when its type is "submit" it acts like a data submitter to the server
     
    gnostic, May 15, 2012
    #7
    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. Chris Lane
    Replies:
    4
    Views:
    11,266
    Chris Lane
    Nov 17, 2003
  2. Psych971
    Replies:
    5
    Views:
    4,651
    bruce barker
    Dec 17, 2004
  3. =?Utf-8?B?Q2hhZA==?=

    Postback not being set to True on first postback

    =?Utf-8?B?Q2hhZA==?=, Apr 4, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    516
    =?Utf-8?B?Q2hhZA==?=
    Apr 4, 2005
  4. gce
    Replies:
    2
    Views:
    1,728
  5. Mossman
    Replies:
    0
    Views:
    802
    Mossman
    Dec 12, 2005
Loading...

Share This Page