1

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');
                }
            }
        });
    }
});
eugeneK
  • 10,750
  • 19
  • 66
  • 101

1 Answers1

2

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();
}
IsmailS
  • 10,797
  • 21
  • 82
  • 134
  • first of all thanks for time and effort to help me, second of all is there possibility to avoid creation of second parameter in seError method so i won't be needed to change this function all around the project ? – eugeneK Jun 08 '10 at 13:08
  • this line gives and error ((HttpContext.Current.Handler as System.Web.UI.Page).Master as MasterPage).MessagePlaceholder = getMessage(); – eugeneK Jun 08 '10 at 13:26
  • Create `public static void setError(string errorMessage, bool redirecting)` as an overload. Keep your old `setError` method as is. That will save you changing method calls where you are redirecting. In all places when you are not redirecting call `setError("Message", false);` What's the error? Invalid Cast? – IsmailS Jun 09 '10 at 04:48
  • See http://stackoverflow.com/questions/58123/finding-system-web-ui-page-from-httpcontext. The line at which you said is raising error should work. Can you please give detailed error so that I can understand the problem. – IsmailS Jun 09 '10 at 05:23
  • Is it a runtime or compile time error? Have you defined MessagePlaceHolder as a public property in your master page? and you will have to cast it to the specific master page class which you have defined. So most probably if your master page file name is MasterPage.master then you class name should be MasterPage to which you should cast it. – IsmailS Jun 09 '10 at 07:07
  • @Ismail, i did exactly as you wrote with public property. It gives me a compile error. As far as i know i need to define VirtualMaster on each page if i use it's public properties. This must be an error, oddly enough you don't have it – eugeneK Jun 09 '10 at 07:17
  • Error 3 'System.Web.UI.MasterPage' does not contain a definition for 'MessagePlaceholder' and no extension method 'MessagePlaceholder' accepting a first argument of type 'System.Web.UI.MasterPage' could be found (are you missing a using directive or an assembly reference?) – eugeneK Jun 09 '10 at 09:40
  • I've used code behind , this was the problem. I've implemented a class that inherits from MasterPage, should do the job – eugeneK Jun 09 '10 at 11:13
  • I don't know how you resolved it but if my master page code behind looks like `public partial class BasicMaster : System.Web.UI.MasterPage` then the line at which you said is throwing error should be like this `((HttpContext.Current.Handler as System.Web.UI.Page).Master as BasicMaster).MessagePlaceholder = getMessage();` – IsmailS Jun 09 '10 at 12:00