Web Message Box

time to read 4 min | 760 words

This is probably a really bad idea, but here it is:

public class WebMsgBox

{

      public static void Show(string msg)

      {

            if(HttpContext.Current==null && HttpContext.Current.Handler is Page)

                  return;

            Page currentPage = (Page) HttpContext.Current.Handler;

            currentPage.ClientScript

                  .RegisterStartupScript(currentPage.GetType(),"msgBox",

                      string.Format("alert('{0}');", msg.Replace("'","\\'")),

                      true);

      }

}

It can be called from anywhere in the code, not just from a page.