Handling Button Events

Upated March 30, 2010
Iron Speed Designer V7.0 and later

It is often useful to call custom logic when a button is clicked in your application:

A good way to catch the relevant button events is by overriding the button click method and calling your custom logic.  Or, you can add a new button and add functionality to that button.  Individual pages may contain different Table controls, so make sure you override the button click method in the appropriate table control class.

Handling a Button Click Event for a TableControl Class

In the example below, a message is displayed to the user after deleting a row in an application built using the Orders table in the Northwind database.  Since the Delete Button event is at the table control level, the DeleteButton_Click() method is overridden in the OrdersTableControl class, located in:

...\<App Folder>\App_Code\Orders\ShowOrdersTable.Controls.cs or .vb

Add the following code to the OrdersTableControl class.

C#:

public override void OrdersDeleteButton_Click(object sender, EventArgs args)

{

     base.OrdersDeleteButton_Click (sender, args);

     BaseClasses.Utils.MiscUtils.RegisterJScriptAlert(this,"MY_KEY","DELETED");

}

Visual Basic .NET:

Public Overrides Sub OrdersDeleteButton_Click(ByVal sender As Object, ByVal args As EventArgs)

     MyBase.OrdersDeleteButton_Click(sender, args)

     BaseClasses.Utils.MiscUtils.RegisterJScriptAlert(Me, "MY_KEY", "DELETED")

End Sub

Important:  If the button is set to redirect to another page in the Property Sheet, then calling the base click method will redirect to this page and any code placed after the call to the base click method will not be executed.

Handling a Button Click Event for an Add Record Page Class

In the example below, when a user attempts to add a new Order in an Add Record page without specifying the ShipPostalCode value and clicks the “Save” button, the Order is not saved and, instead, the user is redirected back to the previous page (typically the Show Orders Table page).  Add this code customization to the AddOrder class, located in:

...\<App Folder>\Orders\AddOrders.aspx.cs or .vb

C#:

public void SaveButton_Click(object sender, EventArgs args)

{

     if (this.ShipPostalCode.Text == "")

     {

          this.RedirectBack();

     }

     SaveButton_Click_Base(sender, args);

}

Visual Basic .NET

Public Sub SaveButton_Click(ByVal sender As Object, ByVal args As EventArgs)

     If (Me.ShipPostalCode.Text = "") Then

          Me.RedirectBack

     End If

     SaveButton_Click_Base(sender, args)

End Sub

Important:  If the button is set to redirect to another page in the Property Sheet, then calling the base click method will redirect to this page and any code placed after the call to the base click method will not be executed.

In the example below, a new button called “Fill Default Value” has been added to an Add Record page.  When the button is clicked, a default value of “99999” is inserted into the ShipPostalCode text box field.

Step 1:  Use the Application Explorer to open an Add Record page.

Step 2:  In Design Mode, select the Page Buttons area.  Drag a Button control from the Toolbox and place it next to the Cancel button.

Step 3:  Select select the new button control and set these properties via the Property Sheet:

Group

Property

Setting

[Application Generation]

Button actions

Custom

Appearance

Text

Fill Default Value

Step 4:  Add the following code in the AddOrders class, located in:

...<App Folder>\Orders\AddOrders.aspx.cs or .vb

C#

public void Button_Click(object sender, EventArgs args)

{

     Button_Click_Base(sender, args);

     this.ShipPostalCode.Text = "99999";

}

Visual Basic .NET:

Public Sub Button_Click(ByVal sender As Object, ByVal args As EventArgs)

     Button_Click_Base(sender, args)

     Me.ShipPostalCode.Text = "99999"

End Sub

Step 5:  Bulid and run your application.

See Also

Part V: Customizing Application Code