Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Displaying action results to the user in ASP.NET

0.00/5 (No votes)
3 Jan 2012 1  
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:


C#
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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here