65.9K
CodeProject is changing. Read more.
Home

Displaying action results to the user in ASP.NET

starIconstarIconemptyStarIconemptyStarIconemptyStarIcon

2.00/5 (1 vote)

Jan 3, 2012

CPOL
viewsIcon

16181

How do you display the results of an action to a user and ensure it shows up no matter which page they are redirected to afterwards?

One way is to pass it via a query string. While it's doable, there are limitations such as the length of the message and having to escape/encode special characters.

Another way is to store the message in state, then have a dedicated control on each page grab the message, display it, then clear the message so it doesn't get shown again.

Below is a control designed to do exactly that.

To display a message, simply call Result.Display("[succeededmessage]"). To display an error, call Result.DisplayError("[failedmessage]");.

To have the message/error show up, simply include a reference on the page:

<cc:Result  runat="server" />

Here's the code:

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SoftwareMonkeys.SiteStarter.State;

namespace SoftwareMonkeys.SiteStarter.Web.WebControls
{
    /// <summary>
    /// Displays the result of an action.
    /// </summary>
    public class Result : Panel
    {               
        public bool IsInAsyncPostBack
        {
            get
            {
                ScriptManager scriptManager = ScriptManager.GetCurrent(Page);
                return scriptManager != null && scriptManager.IsInAsyncPostBack;
            }
        }
        /// <summary>
        /// Gets/sets a value indicating whether the result is an error.
        /// </summary>
        static public bool IsError
        {
            get
            {
                if (!StateAccess.IsInitialized || StateAccess.State == null || 
                               StateAccess.State.Session == null)
                        return false;
                else
                {
                    if (!StateAccess.State.ContainsSession("Result_IsError")
                        || StateAccess.State.GetSession("Result_IsError") == null)
                        StateAccess.State.SetSession("Result_IsError", false);
                    return (bool)StateAccess.State.GetSession("Result_IsError");
                }
            }
            set
            {
                if (StateAccess.IsInitialized)
                    StateAccess.State.SetSession("Result_IsError", value);
            }
        }
        
        /// <summary>
        /// Gets/sets the text result.
        /// </summary>
        static public string Text
        {
            get
            {
                if (!StateAccess.IsInitialized || StateAccess.State == null || 
                           StateAccess.State.Session == null)
                    return String.Empty;
                else
                {
                    if (!StateAccess.State.ContainsSession("Result_Text")
                        || StateAccess.State.GetSession("Result_Text") == null)
                        StateAccess.State.SetSession("Result_Text", String.Empty);
                    return (string)StateAccess.State.GetSession("Result_Text");
                }
            }
            set
            {
                if (StateAccess.IsInitialized)
                     StateAccess.State.SetSession("Result_Text", value);
            }
        }

        protected override void Render(HtmlTextWriter writer)
        {
                
            // Only display the control if it is marked
            // as visible and a message is available
            if (Visible && !IsInAsyncPostBack && Text != String.Empty)
            {
                // Use the correct CSS class depending
                // on whether it is showing an error
                if (IsError)
                    CssClass = "Error";
                else
                    CssClass = "Message";

                // Add the text to the control
                Controls.Add(new LiteralControl(Text));

                // Render the control
                base.Render(writer);

                // Reset the text because it has been displayed now
                Text = String.Empty;
                IsError = false;
            }
        }

        #region Static functions
        /// <summary>
        /// Displays the provided text on the next
        /// Result control that is rendered on a page.
        /// </summary>
        /// <param name="text">The text to display
        /// on the Result control.</param>
        public static void Display(string text)
        {
                if (StateAccess.IsInitialized)
                {
                    Text = text;
                    IsError = false;
                }
                else
                    throw new InvalidOperationException(
                      "Can't use the result control when " + 
                      "the state has not been initialized.");
        }

        /// <summary>
        /// Displays the provided error on the next
        /// Result control that is rendered on a page.
        /// </summary>
        /// <param name="error">The error to
        ///    display on the Result control.</param>
        public static void DisplayError(string error)
        {
                if (StateAccess.IsInitialized)
                {
                    Text = error;
                    IsError = true;
                }
                else
                    throw new InvalidOperationException(
                      "Can't use the result control when " + 
                      "the state has not been initialized.");
        }
        #endregion
    }
}

http://code.google.com/p/sitestarter/source/browse/trunk/Src/App/SoftwareMonkeys.SiteStarter.Web/WebControls/Result.cs[^]

The control uses a custom state management library (e.g., StateAccess.State.SetSession("")), but if you want to use the control yourself, you can simply change it to use HttpContext.Current.Session instead.