Handling aspButton Click events

  • Thread starter Thread starter AidanT
  • Start date Start date
A

AidanT

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.CommandName = "Add";
cmdAdd.CommandArgument="AddLine";

cmdAdd.Command += new CommandEventHandler ( HandleButtonPressed );
....


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


....
private void HandleButtonPressed ( object sender, CommandEventArgs 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?
 
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?
 
Back
Top