Issue a modal JavaScript dialog from an ASP.NET web page

Suppose in your ASP.NET codebehind you update a database record when the user clicks a Save button. You may want to write the record to the database, issue a modal "Save Successful" message and redirect the user to a different web page. How do you do it? What happens if the user hit the browser's Back button? You don't want the page to redisplay the dialog. Here is one possible way to do it.

In the web page containing the Save button, save the data to the database then, if successful, set a session variable denoting that the dialog should be shown, then redirect the user to the next page. I typically don't like using the Session Object but I will here. Look at the following VB code:

    Private Sub btnSave_Click(...)
        Dim blnRedirect As Boolean = False

        Try
            ...
            '
            ' Update the database.
            '
            blnRedirect = DAL.UpdateDatabase(...)
            '
            ' If successful, set the flag. Otherwise stay on 
            ' current page to provide error information.
            '
            If blnRedirect Then
                Session.Add("ShowDialog", "True")
            End If

        Catch Ex As Exception
            ...
        End Try
        '
        ' This cannot occur within the Try-Catch block.
        '
        If blnRedirect Then Response.Redirect("NextPage.aspx")

    End Sub

Now, in the Page_Load event of the target page we need to conditionally display the modal dialog box. We can do this if the request is not a PostBack.

    Private Sub Page_Load(...)
        Dim blnRedirect As Boolean = False

        If Not Page.IsPostBack Then
            ShowDialog(Page)
        End If

    End Sub

For the dialog, you can use a bit of JavaScript to display an Alert. The JavaScript gets fired when the target page loads by hooking it to the page's OnLoad event.

    Private Sub ShowDialog(ByVal page As System.Web.UI.Page)
        '  
        ' Conditionally show the dialog based on the Session variable.
        '  
        If Not page.Session.Item("ShowDialog") Is Nothing Then
            Dim strValue As String = page.Session.Item("ShowDialog").ToString()

            If strValue.ToUpper() = "TRUE" Then
                '
                ' Create the JavaScript to send to the browser
                ' to display the Alert dialog.
                '
                Dim sb As StringBuilder = New StringBuilder()
                sb.Append("")
                '
                ' Attach the JavaScript to the page's OnLoad event.
                '
                page.RegisterStartupScript("OnLoad", sb.ToString())
            End If
            '  
            ' Remove the session variable flag.
            '  
            page.Session.Remove(strKey)
        End If

    End Sub

But if the user goes to another page then uses the Browser's Back button to return to the page that issued the Alert, it will show the dialog again. To prevent that, you need to tell the Browser not to cache the page. This can be done by adding the SetCacheability property as the first line of the HTML in the .ASPX page that shows the Alert.

    <%@ OutputCache Location="None" %>

You can look up the Response.Cache.SetCacheability property in MSDN for more info on caching web pages in ASP.NET.


About TheScarms
About TheScarms


Sample code
version info

If you use this code, please mention "www.TheScarms.com"

Email this page


© Copyright 2016 TheScarms
Goto top of page