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
{
public class Result : Panel
{
public bool IsInAsyncPostBack
{
get
{
ScriptManager scriptManager = ScriptManager.GetCurrent(Page);
return scriptManager != null && scriptManager.IsInAsyncPostBack;
}
}
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);
}
}
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)
{
if (Visible && !IsInAsyncPostBack && Text != String.Empty)
{
if (IsError)
CssClass = "Error";
else
CssClass = "Message";
Controls.Add(new LiteralControl(Text));
base.Render(writer);
Text = String.Empty;
IsError = false;
}
}
#region Static functions
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.");
}
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.