Newbie : Adding an onclick event to server control, prevents postback.???

Discussion in 'ASP .Net' started by Dr Deadpan, Nov 20, 2003.

  1. Dr Deadpan

    Dr Deadpan Guest

    Hi,

    I have a .net page that basically has two buttons on it. Each
    button when
    rendered by the server basically seem to have a type of "SUBMIT". What
    i want is to be able to disable one of the buttons when a user clicks
    on it and have the server process the operation and then renable it
    went it's done.

    I added the following piece of code to my Page_Load event.

    MyButton.Attributes.Add("onclick", "this.disabled=true;");

    This worked fine except that my event handler in the code-behind,
    ExportButton_Click is not fired and so nothign happens except for the
    button being disabled. Can someone give me detailed information on how
    this can be done as I'm relatively new to C# , Javascript and .Net.

    I was reading about the RegisterOnSubmit statement and such but it
    did not
    seem to work. What am I missing? What is an easy way to accomplish
    this?

    Code snippets/ links much appreaciated.

    Thanks
     
    Dr Deadpan, Nov 20, 2003
    #1
    1. Advertising

  2. yes this is a problem.
    one way around this is to hide the button instead.
    if looks a bit stupid but you could have a disabled button which is hidden
    until you click and hide the real button.
    a bit mickey mouse but what can you do?

    if you find a better way, please post it.
    a

    "Dr Deadpan" <> wrote in message
    news:...
    > Hi,
    >
    > I have a .net page that basically has two buttons on it. Each
    > button when
    > rendered by the server basically seem to have a type of "SUBMIT". What
    > i want is to be able to disable one of the buttons when a user clicks
    > on it and have the server process the operation and then renable it
    > went it's done.
    >
    > I added the following piece of code to my Page_Load event.
    >
    > MyButton.Attributes.Add("onclick", "this.disabled=true;");
    >
    > This worked fine except that my event handler in the code-behind,
    > ExportButton_Click is not fired and so nothign happens except for the
    > button being disabled. Can someone give me detailed information on how
    > this can be done as I'm relatively new to C# , Javascript and .Net.
    >
    > I was reading about the RegisterOnSubmit statement and such but it
    > did not
    > seem to work. What am I missing? What is an easy way to accomplish
    > this?
    >
    > Code snippets/ links much appreaciated.
    >
    > Thanks
     
    Andrew de la Harpe, Nov 21, 2003
    #2
    1. Advertising

  3. Dr Deadpan

    Rory Guest

    Instead of adding an "onClick" attribute to the button,
    why not just disable it from the buttons click event?
    ie. myButton.Enabled = False

    I guess what you are trying to achieve is to prevent the
    user from clicking the button more than once.
    You should also make sure you have wired up your event
    handlers for the button.

    eg. protected WithEvents btnMyButton As Button

    protected sub btnMyButton_Click() Handles btnMyButton.Click
    end sub

    This is VB code, but you need to make sure you declare
    your button using 'WithEvent', and wire up the proper
    event (using the 'Handles' statement) to the method that
    will be handling the event.

    It might also be a good idea to set the AutoEventWireup
    directive in your .aspx page to False, otherwise your
    event will fire twice.

    Hope this helps

    >-----Original Message-----
    >Hi,
    >
    > I have a .net page that basically has two buttons on

    it. Each
    >button when
    >rendered by the server basically seem to have a type

    of "SUBMIT". What
    >i want is to be able to disable one of the buttons when a

    user clicks
    >on it and have the server process the operation and then

    renable it
    >went it's done.
    >
    > I added the following piece of code to my Page_Load

    event.
    >
    >MyButton.Attributes.Add("onclick", "this.disabled=true;");
    >
    > This worked fine except that my event handler in the

    code-behind,
    >ExportButton_Click is not fired and so nothign happens

    except for the
    >button being disabled. Can someone give me detailed

    information on how
    >this can be done as I'm relatively new to C# , Javascript

    and .Net.
    >
    > I was reading about the RegisterOnSubmit statement and

    such but it
    >did not
    >seem to work. What am I missing? What is an easy way to

    accomplish
    >this?
    >
    > Code snippets/ links much appreaciated.
    >
    >Thanks
    >.
    >
     
    Rory, Nov 21, 2003
    #3
  4. Dr Deadpan

    Dr Deadpan Guest

    "Rory" <> wrote in message news:<069e01c3aff6$1699ce20$>...
    > Instead of adding an "onClick" attribute to the button,
    > why not just disable it from the buttons click event?
    > ie. myButton.Enabled = False
    >


    I cannot do this as it won't work. What the above will do, is disable
    the button once it gets bakc to the client. All the while while the
    server is actually running the code in the event handler, the button
    will remain Enabled and will become disabled once the code behind is
    done - definitely not what I wnat. I read a post earlier on about
    doing it an easier way somewhere.
    Please, all you guru's , keep the comments coming..

    DrD
     
    Dr Deadpan, Nov 21, 2003
    #4
    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?QmFobWFu?=

    onclick postback event has disappeared

    =?Utf-8?B?QmFobWFu?=, Mar 2, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    596
    =?Utf-8?B?QmFobWFu?=
    Mar 3, 2006
  2. Replies:
    0
    Views:
    287
  3. Ed Anady
    Replies:
    0
    Views:
    267
    Ed Anady
    Oct 27, 2006
  4. Replies:
    1
    Views:
    379
    bruce barker
    May 17, 2007
  5. Frank
    Replies:
    2
    Views:
    425
    Victor Garcia Aprea [MVP]
    Nov 26, 2003
Loading...

Share This Page