Handling aspButton Click events

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • AidanT

    Handling aspButton Click events

    I am writing my first Web application, and have a question regarding
    asp Buttons of Submit and Command Type.

    I have a WebForm with a Command Button on it that I am creating in the
    Page_Load :

    ....

    // ADD Button
    Button cmdAdd = new Button();
    cmdAdd.Text = "Add";
    cmdAdd.CommandN ame = "Add";
    cmdAdd.CommandA rgument="AddLin e";

    cmdAdd.Command += new CommandEventHan dler ( HandleButtonPre ssed );
    ....


    When the button is pressed I want it to be handled in a delegate like
    this :


    ....
    private void HandleButtonPre ssed ( object sender, CommandEventArg s e)
    {
    // handle stuff here - I add a row to a Table
    }
    ....


    However, the first time I press the button this method is called as I
    want, and the next time the Button seems to do a Submit without
    calling my event handler. Is it possible to combine the two, so that I
    press the button, perform my action in the Command Event Handler and
    refresh my WebForm with the changes in my action?
  • Scott Allen

    #2
    Re: Handling aspButton Click events

    Hi Aidan:

    For your first application you should probably take a look at
    declaring controls in the ASPX with markup like:

    <asp:Button runat="server" id="cmdAdd"/>

    You can add controls to the page with code as you are trying but there
    are additional complications in that approach. Do you have a tuturial
    to work through?

    --
    Scott


    On 28 Oct 2004 07:51:55 -0700, atwomey@yahoo.c om (AidanT) wrote:
    [color=blue]
    >I am writing my first Web application, and have a question regarding
    >asp Buttons of Submit and Command Type.
    >
    >I have a WebForm with a Command Button on it that I am creating in the
    >Page_Load :
    >
    >...
    >
    >// ADD Button
    >Button cmdAdd = new Button();
    >cmdAdd.Text = "Add";
    >cmdAdd.Command Name = "Add";
    >cmdAdd.Command Argument="AddLi ne";
    >
    >cmdAdd.Comma nd += new CommandEventHan dler ( HandleButtonPre ssed );
    >...
    >
    >
    >When the button is pressed I want it to be handled in a delegate like
    >this :
    >
    >
    >...
    >private void HandleButtonPre ssed ( object sender, CommandEventArg s e)
    >{
    > // handle stuff here - I add a row to a Table
    >}
    >...
    >
    >
    >However, the first time I press the button this method is called as I
    >want, and the next time the Button seems to do a Submit without
    >calling my event handler. Is it possible to combine the two, so that I
    >press the button, perform my action in the Command Event Handler and
    >refresh my WebForm with the changes in my action?[/color]

    Comment

    Working...