HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

ASP.Net, C#, and alert/display messages

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
netmessagesanddisplayalertasp

Problem

We have a hidden div in a master page. When we want to display a message, we send a function a message to display, and it turns that div from .Visible false to .Visible true, and fills the div with the message. For all intents and purposes, this works and we haven't really had much trouble with it. I just feel that there must be a more "standard" way of doing this. There are two downsides, which I'll detail at the end, but neither is site-breakingly critical.

ControlHelper.cs

public static void DisplayNotificationMessage(MasterPage master, List errormessages, string title, MessageBoxImages imgtype)
{
    if (master.FindControl("divmsgpanel") != null)
    {
        master.FindControl("divmsgpanel").Visible = true;
    }

    if (master.FindControl("divdimmer") != null)
    {
        master.FindControl("divdimmer").Visible = true;
    }

    Label titlelabel = (Label)master.FindControl("lblmsgpaneltitle");

    if (titlelabel != null)
    {
        titlelabel.Text = title;
    }

    TextBox thetxtbox = (TextBox)master.FindControl("txtboxmsgcontents");

    if (thetxtbox != null)
    {
        thetxtbox.Text = String.Empty;
        foreach (string x in errormessages)
        {
            thetxtbox.Text += x + "\n\n";
        }
    }

    Image icon = (Image)master.FindControl("imgmessageicon");

    switch (imgtype)
    {
        case MessageBoxImages.Info:
            icon.ImageUrl = "~/images/icons/ico-msginfo96x96.png";
            break;
        case MessageBoxImages.Warning:
            icon.ImageUrl = "~/images/icons/ico-msgwarning96x96.png";
            break;
        case MessageBoxImages.Error:
            icon.ImageUrl = "~/images/icons/ico-msgerror96x96.png";
            break;
        default:
            icon.ImageUrl = "~/images/icons/ico-msginfo96x96.png";
            break;
    }
}


divdimmer is what we use to make the "background" go dark, so that the error message sticks out to the user. Here is a cropped screenshot of the final produ

Solution

Not actually 100% sure if this will work for you but could you use Session variables to achieve what you are after?

public sealed class MessageInfo
    {
        private readonly string title;

        private readonly string text;

        public MessageInfo(string title, string text)
        {
            this.title = title;
            this.text = text;
        }

        public string Title
        {
            get
            {
                return this.title;
            }
        }

        public string Text
        {
            get
            {
                return this.text;
            }
        }
    }

    public sealed class MessageBox
    {
        private readonly Page parent;

        public MessageBox(Page parent)
        {
            if (parent == null)
            {
                throw new ArgumentNullException("parent");
            }

            this.parent = parent;
        }

        private MessageInfo Message
        {
            get
            {
                return this.parent.Session["MessageBox"] as MessageInfo;
            }

            set
            {
                this.parent.Session["MessageBox"] = value;
            }
        }

        public void NextMessage(string title, string message)
        {
            NextMessage(new MessageInfo(title, message));
        }

        public void NextMessage(MessageInfo msgInfo)
        {
            Message = msgInfo;
        }

        private void Clear()
        {
            NextMessage(null);
        }

        public bool CanShow()
        {
            return !string.IsNullOrEmpty(Message.Text);
        }

        public void Show()
        {
            if (!CanShow())
            {
                return; // do nothing or perhaps throw exception?
            }

            ControlHelper.DisplayNotificationMessage(this.parent.Master.Master, Message.Text, Message.Title, MessageBoxImages.Info);
        }

        public void ShowOnce()
        {
            Show();

            // clear immediately
            Clear();
        }
    }


and used in your target page like:

protected void Page_Load(object sender, EventArgs e)
{
   var msgBox = new MessageBox(this);
   if(msgBox.CanShow())
   {
      msgBox.ShowOnce();
   }
}


and set from your calling page like:

protected void Deleted(object sender, EventArgs e)
{
   new MessageBox(this).NextMessage("Success", "The event has been deleted");
}

Code Snippets

public sealed class MessageInfo
    {
        private readonly string title;

        private readonly string text;

        public MessageInfo(string title, string text)
        {
            this.title = title;
            this.text = text;
        }

        public string Title
        {
            get
            {
                return this.title;
            }
        }

        public string Text
        {
            get
            {
                return this.text;
            }
        }
    }

    public sealed class MessageBox
    {
        private readonly Page parent;

        public MessageBox(Page parent)
        {
            if (parent == null)
            {
                throw new ArgumentNullException("parent");
            }

            this.parent = parent;
        }

        private MessageInfo Message
        {
            get
            {
                return this.parent.Session["MessageBox"] as MessageInfo;
            }

            set
            {
                this.parent.Session["MessageBox"] = value;
            }
        }

        public void NextMessage(string title, string message)
        {
            NextMessage(new MessageInfo(title, message));
        }

        public void NextMessage(MessageInfo msgInfo)
        {
            Message = msgInfo;
        }

        private void Clear()
        {
            NextMessage(null);
        }

        public bool CanShow()
        {
            return !string.IsNullOrEmpty(Message.Text);
        }

        public void Show()
        {
            if (!CanShow())
            {
                return; // do nothing or perhaps throw exception?
            }

            ControlHelper.DisplayNotificationMessage(this.parent.Master.Master, Message.Text, Message.Title, MessageBoxImages.Info);
        }

        public void ShowOnce()
        {
            Show();

            // clear immediately
            Clear();
        }
    }
protected void Page_Load(object sender, EventArgs e)
{
   var msgBox = new MessageBox(this);
   if(msgBox.CanShow())
   {
      msgBox.ShowOnce();
   }
}
protected void Deleted(object sender, EventArgs e)
{
   new MessageBox(this).NextMessage("Success", "The event has been deleted");
}

Context

StackExchange Code Review Q#15464, answer score: 5

Revisions (0)

No revisions yet.