Error messages in ASP.NET with jQuery UI
<p>I've been using my own Error reporting module which was combination of simple c# and jQueryUI Dialog. Problem is that once error or success occurs i do write it's value to session. It does work pretty good on pages with Responce.Redirect on error but not on pages where i catch an error and then return to same form.</p>

<p>My question is why does session which added pre-postback fails to load in pages where i have return statement on some condition.</p>

<p>And if there another way to save errors and success message except in session ? Maybe global variables or something like that ...</p>

<p><strong>CODE EXAMPLES</strong></p>

<p>this is Error class</p>

<pre><code>public static string getMessage()
{
    HttpContext c = HttpContext.Current;
    string messageType = "";
    if (c.Session["errorMessage"] != null)
    {
        messageType = "errorMessage";
    }
    else if (c.Session["successMessage"] != null)
    {
        messageType = "successMessage";
    }

    if (!string.IsNullOrEmpty(messageType))
    {
        string[] messageBody = c.Session[messageType].ToString().Split('|');
        StringBuilder userMessageSb = new StringBuilder();
        userMessageSb.Append(string.Format("&lt;div id=\"{0}\" title=\"{1}\"&gt;{2}&lt;/div&gt;", messageType, messageBody[0], messageBody[1]));

        // fix so message will not re-appear
        c.Session.Remove(messageType);

        messageType = userMessageSb.ToString();
    }
    return messageType;
}

public static void setSuccess(string successMessage)
{
    HttpContext.Current.Session["successMessage"] = setMessage("success", successMessage);
}

public static void setError(string errorMessage)
{
    HttpContext.Current.Session["errorMessage"] = setMessage("error", errorMessage);
}

private static string setMessage(string messageTitle, string messageBody)
{
    return string.Format("{0}|{1}", messageTitle, messageBody);
}
</code></pre>

<p>i set message like this prior to redirect or return</p>

<pre><code>   Errors.setError(my error is");
</code></pre>

<p>i get error on bottom of my masterpage like this</p>

<pre><code>&lt;%= Errors.getMessage() %&gt;
</code></pre>

<p>and this is JS</p>

<pre><code>$(function () {
    $("#errorMessage").dialog("destroy");
    $("#successMessage").dialog("destroy");

    if ($("#errorMessage").length != 0) {
        $("#errorMessage").dialog({
            modal: true,
            height: 300,
            width: 400,
            buttons: {
                Ok: function () {
                    $(this).dialog('close');
                }
            }
        });
    }
    if ($("#successMessage").length != 0) {
        $("#successMessage").dialog({
            modal: true,
            height: 300,
            width: 400,
            buttons: {
                Ok: function () {
                    $(this).dialog('close');
                }
            }
        });
    }
});
</code></pre>
