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');
                }
            }
        });
    }
});
1
  • Can you put some code which you have written? It will help understand better. Commented Jun 8, 2010 at 11:04

1 Answer 1

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();
}
Sign up to request clarification or add additional context in comments.

9 Comments

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 ?
this line gives and error ((HttpContext.Current.Handler as System.Web.UI.Page).Master as MasterPage).MessagePlaceholder = getMessage();
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?
See stackoverflow.com/questions/58123/…. The line at which you said is raising error should work. Can you please give detailed error so that I can understand the problem.
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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.