Use RegisterJScriptAlert to Display Messages

The RegisterJScriptAlert method can be used to display a message in user defined methods, BaseClasses methods, or in events.
- Pooja Daga, Technical Support Engineer, Iron Speed, Inc.

April 5, 2006
Iron Speed Designer V3.2

Procedure
In Microsoft Windows applications you can call MessageBox to display a message to the user. Unfortunately, there is no equivalent in web applications when you are executing server-side code. For example, if you write a validation routine, you cannot simply call MessageBox to display an error message to the user.

If you want to give an error message to the user, you can use the standard <Validator>.ErrorMessage property to set the value that will be displayed to the user. In some cases, you might want to have your own custom function. So how can we do this?

In Microsoft .NET applications, data validation occurs in a postback. When the page is first loaded, the data is displayed, and when you press the Submit button, a postback is triggered and your server-side code is called to validate the data. This postback will eventually either re-display this page or redirect to another page.

If you want to display an error message, you need to do two things. First, register the alert message, and second, cancel any redirection. Registering the alert message ensures that when the page is re-displayed as part of the postback, any registered messages will be displayed to the user. If you do not cancel the redirection to another page, your registered alert message will never be displayed.

Use the RegisterJScirptAlert method to register the alert message:

BaseClasses.Utils.MiscUtils.RegisterJScriptAlert(Me, "MyAlertName", "My Alert Text")

The RegisterJScriptAlert method can be used to display a message in user defined methods, BaseClasses methods, or in events.

In the code below, override the OnApplicationEvent() method in the TableControl class and handle the DeleteRecord event within it. If the UnitPrice of a selected record for the Order_Details table is greater than $10, the RegisterJScriptAlert method is used to display a "Cannot delete record" message.

For .NET Framework 1.1, add the code below in ShowOrder_DetailsTablePage.aspx.cs, located in

...\<App Folder>\Order_Details\ShowOrder_DetailsTablePage.aspx.cs

For .NET Framework 2.0, add the code below in ShowOrder_DetailsTablePage.Controls.cs, located in

...\<App
Folder>\App_Code\Order_Details\ShowOrder_DetailsTablePage.Controls.cs

C#:

public override void OnApplicationEvent(BaseClasses. ApplicationEventArgs args)
{
    if(args.EventType == BaseClasses. ApplicationEventArgs.EventTypes.DeleteRecord)
    {
      Order_DetailsRecord selectedRecord = this.GetSelectedRecord(true);
      decimal myUnitPrice = selectedRecord.UnitPrice;
      if(myUnitPrice >10)
      {
        BaseClasses.Utils.MiscUtils.RegisterJScriptAlert(this,"CANNOT_ DISPLAY_MESSAGE","Cannot delete record");
        return;
      }
    }
    base.OnApplicationEvent(args);
}

Visual Basic .NET

Public Overrides Sub OnApplicationEvent(ByVal args As BaseClasses.ApplicationEventArgs
 
    If (args.EventType = BaseClasses. ApplicationEventArgs.EventTypes.DeleteRecord) Then
        Dim selectedRecord As Order_DetailsRecord = Me.GetSelectedRecord(True)
        Dim myUnitPrice As Decimal = selectedRecord.UnitPrice
        If (myUnitPrice > 10) Then
            BaseClasses.Utils.MiscUtils.RegisterJScriptAlert(Me, " CANNOT_DELETE_MESSAGE", "Cannot delete the record")
        Else
            'Do not stop the deletion of record and call the base OnApplicationEvent which will delete the record.
            MyBase.OnApplicationEvent(args)
        End If
    Else
        MyBase.OnApplicationEvent(args)
    End If
End Sub

About the Author
Pooja Daga
Technical Support Engineer, Iron Speed, Inc.

Pooja has experience developing Web applications using .NET technology. She started her career with PCS, a leading software services company headquartered in India and has been with Iron Speed since 2005.

Pooja holds a M.S. degree in Computer Application and a B.S. degree in Electrical engineering from Maharaja Sayajirao University in India.



  Privacy Statement