Click here to Skip to main content
15,886,777 members
Articles / Web Development / ASP.NET

AJAX Enabled MessageBox

Rate me:
Please Sign up or sign in to vote.
4.73/5 (56 votes)
14 Sep 2009CPOL2 min read 199.8K   7.8K   108  
MessageBox usercontrol that is AJAX ready and has confirmation support
using System;
using System.Collections;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace YaBu.MessageBox
{
    public partial class uscMsgBox : System.Web.UI.UserControl
    {
        public enum enmAnswer
        {
            OK = 0,
            Cancel = 1
        }
        public enum enmMessageType
        {
            Error = 0,
            Success = 1,
            Attention = 2,
            Info = 3
        }

        public class MsgBoxEventArgs : System.EventArgs
        {
            public enmAnswer Answer;
            public string Args;

            public MsgBoxEventArgs(enmAnswer answer, string args)
            {
                Answer = answer;
                Args = args;
            }
        }

        public delegate void MsgBoxEventHandler(object sender, MsgBoxEventArgs e);
        public event MsgBoxEventHandler MsgBoxAnswered;

        public string Args
        {
            get
            {
                if (ViewState["Args"] == null)
                    ViewState["Args"] = "";

                return (Convert.ToString(ViewState["Args"]));
            }
            set
            {
                ViewState["Args"] = value;
            }
        }

        public class Message
        {
            public Message(string messageText, enmMessageType messageType)
            {
                _messageText = messageText;
                _messageType = messageType;
            }

            private enmMessageType _messageType = enmMessageType.Info;
            private string _messageText = "";

            public enmMessageType MessageType
            {
                get { return _messageType; }
                set { _messageType = value; }
            }

            public string MessageText
            {
                get { return _messageText; }
                set { _messageText = value; }
            }

        }

        protected int MessageNumber
        {
            get
            {
                return Messages.Count;
            }
        }

        private List<Message> Messages = new List<Message>();

        public void AddMessage(string msgText, enmMessageType type)
        {
            Messages.Add(new Message(msgText, type));

            Args = "";
            btnPostCancel.Visible = false;
            btnPostOK.Visible = false;
            btnOK.Visible = true;
        }

        public void AddMessage(string msgText, enmMessageType type, bool postPage, bool showCancelButton, string args)
        {
            Messages.Add(new Message(msgText, type));

            if (!string.IsNullOrEmpty(args))
                Args = args;

            btnPostCancel.Visible = showCancelButton;
            btnPostOK.Visible = postPage;
            btnOK.Visible = !postPage;
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);

            if (btnOK.Visible == false)
                mpeMsg.OkControlID = "btnD2";
            else
                mpeMsg.OkControlID = "btnOK";

            if (Messages.Count > 0)
            {
                btnOK.Focus();
                grvMsg.DataSource = Messages;
                grvMsg.DataBind();

                mpeMsg.Show();
                udpMsj.Update();
            }
            else
            {
                grvMsg.DataBind();
                udpMsj.Update();
            }
            if (this.Parent.GetType() == typeof(UpdatePanel))
            {
                UpdatePanel containerUpdatepanel = this.Parent as UpdatePanel;

                containerUpdatepanel.Update();
            }
        }

        protected void btnPostOK_Click(object sender, EventArgs e)
        {            
            if (MsgBoxAnswered != null)
            {
                MsgBoxAnswered(this, new MsgBoxEventArgs(enmAnswer.OK, Args));
                Args = "";
            }
        }

        protected void btnPostCancel_Click(object sender, EventArgs e)
        {
            if (MsgBoxAnswered != null)
            {
                MsgBoxAnswered(this, new MsgBoxEventArgs(enmAnswer.Cancel, Args));
                Args = "";
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) NetCad
Turkey Turkey
Yavuz is a software developer since 2001. He has been working as senior software developer in Netigma Project at NetCad since 2012.



He has experience in mostly Microsoft technologies such as ASP.Net, SQL Server, C#, VB.Net, WPF, WCF etc. and especially interested in security and performance issues.

Comments and Discussions