Displaying Pop-up Alert Messages
Register and show pop-up alert messages even if the current page is redirected.
- Jing Ding, Senior Systems Consultant for The Ohio State University Medical Center

June 20, 2008
Iron Speed Designer V5.X
Introduction
It is common to display confirmation pop-up messages after a task is successfully completed. Examples are after you send an email or save changes to a page. You can implement pop-ups using ScriptManager.RegisterStartupScript() or Iron Speed Designer’s shortcut method MiscUtils.RegisterJScriptAlert(). The pop-up, however, will not work if the page is redirected after completion of a task. This is because the JavaScript code is emitted to the current page, not the destination page. In this article, I show you how to implement roaming alert messages, i.e. to display pop-up alert even if the page is redirected.
Solution
The idea is fairly easy. Leave the alert message in session, and let the next page (either the current page or the redirect page) pick it up. The BaseApplicationPage class is the best place to implement the method so that all Iron Speed Designer-generated pages are roaming-ready.
Implementation
Open BaseApplicationPage.cs (.vb) in either Visual Studio or Iron Speed Designer. This file is located in:

...\<App Name>\App_Code\Shared\BaseApplicationPage.cs (.vb)

To make the method available to every new Iron Speed Designer application, put it in the code templates folder, usually:

C:\Program Files\Iron Speed\Designer vX.X.X\ProjectTemplates\vs200X\cs(vb)\App_Code\Shared

Add the following RegisterAlert() method in the BaseApplicationPage class.

C#:

public void RegisterAlert(string key, string msg, bool roaming) {
    if (!roaming) {
    MiscUtils.RegisterJScriptAlert(this, key, msg);
    } else {
        Hashtable AlertQueue = this.Session["AlertQueue"] as Hashtable;
        if (AlertQueue == null)
            this.Session["AlertQueue"] = AlertQueue = new Hashtable();
        AlertQueue.Add(key, msg);
    }
}

Visual Basic .NET:

Public Sub RegisterAlert(ByVal key As String, ByVal msg As String, ByVal roaming As Boolean)
    If Not roaming Then
        MiscUtils.RegisterJScriptAlert(Me, key, msg)
    Else
        Dim AlertQueue As Hashtable = TryCast(Me.Session("AlertQueue"), Hashtable)
        If AlertQueue = Nothing Then
            AlertQueue = New Hashtable()
            Me.Session("AlertQueue") = AlertQueue
        End If
        AlertQueue.Add(key, msg)
    End If
End Sub

Locate the Control_ClearControls_PreRender() event handler in the BaseApplicationPage class. Insert the code block “Display alert messages”.

C#:

protected void Control_ClearControls_PreRender(object sender, EventArgs e) {
    this.ClearControlsFromSession();

    // Display alert messages
    Hashtable AlertQueue = this.Session["AlertQueue"] as Hashtable;
    if (AlertQueue != null) {
        foreach (Object key in AlertQueue.Keys) {
            string msg = AlertQueue[key] as string;
            MiscUtils.RegisterJScriptAlert(this, (string)key, msg);
        }
        AlertQueue.Clear();
    }
}

Visual Basic .NET:

Protected Sub Control_ClearControls_PreRender(ByVal sender As Object, ByVal e As EventArgs)
    Me.ClearControlsFromSession()

    ' Display alert messages
    Dim AlertQueue As Hashtable = TryCast(Me.Session("AlertQueue"), Hashtable)
    If AlertQueue <> Nothing Then
        For Each key As Object In AlertQueue.Keys
            Dim msg As String = TryCast(AlertQueue(key), String)
            MiscUtils.RegisterJScriptAlert(Me, DirectCast(key, String), msg)
        Next
        AlertQueue.Clear()
    End If
End Sub

Save and rebuild. Your application is now roaming ready.

About the Author
Jing Ding has a PhD in Computer Engineering, Bioinformatics and Computational Biology, and an M.S. in Toxicology from Iowa State University. He received his B.S. in biophysics from Fundan University in Shanghai, China. He is a self-taught programmer who "played" with assembly, C and C++ in the 1990s. He took a break from programming from 1997 to 2000. When he picked it up again in 2001, he worked with Java. Jing began working with C# and .NET in 2006.


  Privacy Statement