Search Amazon:

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.

Sign In
  User Id 
  Password 


Submit Your Own Code and Articles




About TheScarms
About TheScarms

Ask me your programming questions

I read every email and answer all I can.

User Feedback: Be the first to add a comment! Items to Show:     

     
You must log in to post feedback.
Comment:    
 

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

Email this page


TheScarms AppSentinel lets you securely copy protect and create evaluation versions of your software

TheScarms(tm) AppSentinel lets you quickly and easily create evaluation versions of your software and stop unauthorized copying and unregistered use of your programs!

Get your free
trial copy today!


      The World's Number 1 Web Host

© Copyright 2008 TheScarms