Click here to Skip to main content
Click here to Skip to main content

Tagged as

Displaying action results to the user in ASP.NET

, 3 Jan 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

SoftwareMonkeys
SoftwareMonkeys
Australia Australia
Founder of www.softwaremonkeys.net

Comments and Discussions

 
GeneralHow to implement this in MVC application using jQueryUI ? PinmemberAndrusM14-Jan-12 9:21 
GeneralRe: Hmmm good question. I'll have to look into that. PinmemberSoftwareMonkeys19-Jan-12 23:13 
GeneralReason for my vote of 2 good Pinmemberalwabry10-Jan-12 5:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.1411028.1 | Last Updated 3 Jan 2012
Article Copyright 2012 by SoftwareMonkeys
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid