开发者

Error messages in ASP.NET with jQuery UI

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.

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

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

CODE EXAMPLES

this is Error class

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("<div id=\"{0}\" title=\"{1}\">{2}</div>", 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);
}

i set message like this prior to redirect or return

   Errors.setError(my error is");

i get error on bottom of my masterpage like this

<%= Errors.getMessage() %>
开发者_运维知识库

and this is JS

$(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');
                }
            }
        });
    }
});


There is a possibility that <%= Errors.getMessage() %> executes before you call Errors.setError(my error is") in case when you are not redirecting.

Hope below answer helps.

Create a property in your master page code behind

public string MessagePlaceholder
{
  get { return messagePlaceholder.InnerHtml; }
  set { messagePlaceholder.InnerHtml = value; }
}

Replace <%= Errors.getMessage() %> with a div place holder like below

<div id="messagePlaceholder" runat="server"></div>

And here is your setError method

public static void setError(string errorMessage, bool redirecting)
{
  HttpContext.Current.Session["errorMessage"] = setMessage("error", errorMessage);
  if (!redirecting)
  {
    ((HttpContext.Current.Handler as System.Web.UI.Page).Master as YourMasterPageType).MessagePlaceholder = getMessage();
  }
}

EDIT Sorry I forgot this

In Page_Load event of your master page

if(!IsPostBack)
{
   messagePlaceholder.InnerHtml = Errors.getMessage();
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜