Handling Button Events


"Call custom logic when a button is clicked in your application to display a message after deleting a record, send an email confirmation after adding a record or deleting a record, or handle a custom command."
- Anh Trinh, Software Engineer, Iron Speed, Inc.

November 15, 2006
Iron Speed Designer V4.0

Handling Button Events

It is often useful to call custom logic when a button is clicked in your application:
  • Display a message after deleting a record.
  • Send an email confirmation after adding a record or deleting a record.
  • Handle a custom command.
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 the user deletes 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.

.NET Framework 1.1:

...\<App Folder>\Orders\ShowOrdersTablePage.Controls.cs or .vb

.NET Framework 2.0:

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

Add the following code in 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 Page Properties dialog’s Bindings tab, 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 AddOrderPage class, located in:

.NET Framework 1.1 and 2.0:

...\<App Folder>\Orders\AddOrdersPage.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 Page Properties dialog’s Bindings tab, 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: Add a button from Iron Speed Designer toolbox, and place it next to the Cancel button.

Step 2: Double-click the new button control to display the Properties dialog. In the Display tab, enter “Fill Default Value” for the Button Text, and select “Custom” for the Button Command.

Step 3: Add the following code in the AddOrdersPage class, located in:

.NET Framework 1.1 and 2.0:

...<App Folder>\Orders\AddOrdersPage.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

About the Author

Anh Trinh
Software Engineer, Iron Speed, Inc.

Anh is a software engineer at Iron Speed, Inc. He enjoys developing applications with Iron Speed Designer and Microsoft .NET technology.



  Privacy Statement