messagebox

hey does any know how to display a message box on the sever side?
i am using update panlel

C# coding and AsyncPostBackTrigger,  PostBackTrigger

i need a class that i can call a messagebox(STRING) in c# code

i have found messagebox class on the net, but i am having problem with the post back trigger,
it works fine in the update panel with "PostBackTrigger"
but "AsyncPostBackTrigger" does not work, give me this java error:
Error: Sys.WebForms.PageRequestManagerServerErrorException: Input string was not in a correct format.




this is my messagebox class:

using System;
using System.Web.UI;
using System.Web;
using System.Text;
using System.Collections;


/// <summary>
/// ASP.NET JavaScript message box class
/// </summary>
public class MessageBox
{
    private static Hashtable m_executingPages = new Hashtable();

    private MessageBox() { }

    public static void Show(string sMessage)
    {
        // If this is the first time a page has called this method then
        if (!m_executingPages.Contains(HttpContext.Current.Handler))
        {
            // Attempt to cast HttpHandler as a Page.
            Page executingPage = HttpContext.Current.Handler as Page;

            if (executingPage != null)
            {
                // Create a Queue to hold one or more messages.
                Queue messageQueue = new Queue();

                // Add our message to the Queue
                messageQueue.Enqueue(sMessage);

                // Add our message queue to the hash table. Use our page reference
                // (IHttpHandler) as the key.
                m_executingPages.Add(HttpContext.Current.Handler, messageQueue);

                // Wire up Unload event so that we can inject some JavaScript for the alerts.
                executingPage.Unload += new EventHandler(ExecutingPage_Unload);
            }
        }
        else
        {
            // If were here then the method has allready been called from the executing Page.
            // We have allready created a message queue and stored a reference to it in our hastable.
            Queue queue = (Queue)m_executingPages[HttpContext.Current.Handler];

            // Add our message to the Queue
            queue.Enqueue(sMessage);
        }
    }


    // Our page has finished rendering so lets output the JavaScript to produce the alert's
    private static void ExecutingPage_Unload(object sender, EventArgs e)
    {
        // Get our message queue from the hashtable
        Queue queue = (Queue)m_executingPages[HttpContext.Current.Handler];

        if (queue != null)
        {
            StringBuilder sb = new StringBuilder();

            // How many messages have been registered?
            int iMsgCount = queue.Count;

            // Use StringBuilder to build up our client slide JavaScript.
            sb.Append("<script language='javascript'>");

            // Loop round registered messages
            string sMsg;
            while (iMsgCount-- > 0)
            {
                sMsg = (string)queue.Dequeue();
                sMsg = sMsg.Replace("\n", "\\n");
                sMsg = sMsg.Replace("\"", "'");
                sb.Append(@"alert( """ + sMsg + @""" );");
            }

            // Close our JS
            sb.Append(@"</script>");

            // Were done, so remove our page reference from the hashtable
            m_executingPages.Remove(HttpContext.Current.Handler);

            // Write the JavaScript to the end of the response stream.
            HttpContext.Current.Response.Write(sb.ToString());
        }
    }
}



JCWEBHOSTAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

angus_young_acdcCommented:
That error message seems just like your passing either illegal characters, or not passing a string at all.  Have you been able to see what you're actually trying to populate the message with?  It could be that you're passing an object which isn't getting successfully converted to a string.
0
Meir RivkinFull stack Software EngineerCommented:
(http://weblogs.asp.net/leftslipper/archive/2007/02/26/sys-webforms-pagerequestmanagerparsererrorexception-what-it-is-and-how-to-avoid-it.aspx)By calling Response.Write() directly from your messagebox class you are bypassing the normal rendering mechanism of ASP.NET controls. The bits you write are going straight out to the client without further processing (well, mostly...). This means that UpdatePanel can't encode the data in its special format.
0
Meir RivkinFull stack Software EngineerCommented:
you have 2 options to solve this:If you must use ⿿Response.Write⿝ you can get a similar solution by using a Label control and update it instead of using ⿿Response.Write⿿.If you use ⿿Response.Write⿝ in markup pages (ASPX or ASCX) replace them with either the ⿿=⿝ syntax (<% = MyProperty %>) or use the same solution mentioned above, adding a Label control and updating it in the code behind.(http://dotnetdebug.net/2006/12/28/syswebformspagerequestmanagerparsererrorexception/)
0
Introduction to Web Design

Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

Meir RivkinFull stack Software EngineerCommented:
another option i found is using LiteralControl but i haven't tested it...
0
JCWEBHOSTAuthor Commented:
i am lost? i little help plz?
0
Meir RivkinFull stack Software EngineerCommented:
please follow the links i've posted, they describe in details how to overcome this issue.

(http://dotnetdebug.net/2006/12/28/syswebformspagerequestmanagerparsererrorexception/)
0
JCWEBHOSTAuthor Commented:
i cant find Response.Write() in the code?
0
Meir RivkinFull stack Software EngineerCommented:
why not?
here's the link content:

Having “Response.Write” in the code behind in an event that is fired on an Async postback like Page_Load, Page_Render or the handler that handles that exact event can cause the same problem.

If you must use “Response.Write” you can get a similar solution by using a Label control and update it instead of using “Response.Write“.

If you use “Response.Write” in markup pages (ASPX or ASCX) replace them with either the “=” syntax (<% = MyProperty %>) or use the same solution mentioned above, adding a Label control and updating it in the code behind.
0
JCWEBHOSTAuthor Commented:
can ural give me the code which i need to be changed?
0
JCWEBHOSTAuthor Commented:
this is a Class:

using System;
using System.Text;
using System.Collections;
using System.Web;
using System.Web.UI;
 
public class WebMsgBox
{
  protected static Hashtable handlerPages = new Hashtable();
 
  private WebMsgBox() { }
 
  public static void Show(string Message)
  {
     if (!(handlerPages.Contains(HttpContext.Current.Handler)))
     {
        Page currentPage = (Page)HttpContext.Current.Handler;
        if (!((currentPage == null)))
        {
           Queue messageQueue = new Queue();
           messageQueue.Enqueue(Message);
           handlerPages.Add(HttpContext.Current.Handler, messageQueue);
           currentPage.Unload += new EventHandler(CurrentPageUnload);
        }
     }
     else
     {
        Queue queue = ((Queue)(handlerPages[HttpContext.Current.Handler]));
        queue.Enqueue(Message);
     }
  }
 
  private static void CurrentPageUnload(object sender, EventArgs e)
  {
     Queue queue = ((Queue)(handlerPages[HttpContext.Current.Handler]));
     if (queue != null)
     {
        StringBuilder builder = new StringBuilder();
        int iMsgCount = queue.Count;
        builder.Append("<script language='javascript'>");
        string sMsg;
        while ((iMsgCount > 0))
        {
           iMsgCount = iMsgCount - 1;
           sMsg = System.Convert.ToString(queue.Dequeue());
           sMsg = sMsg.Replace("\"", "'");
           builder.Append("alert( \"" + sMsg + "\" );");
        }
        builder.Append("</script>");
        handlerPages.Remove(HttpContext.Current.Handler);
        HttpContext.Current.Response.Write(builder.ToString());
     }
  }
}



this is how you call it, there is no "Response.Write"?
0
JCWEBHOSTAuthor Commented:
how i call the messbox : WebMsgBox.Show("How to show alert message!");
0
Meir RivkinFull stack Software EngineerCommented:
yes u have, the last line:

HttpContext.Current.Response.Write(builder.ToString());
0
JCWEBHOSTAuthor Commented:
so that must change to?

HttpContext.Current.Response.Write(builder.ToString());

please help.
0
Meir RivkinFull stack Software EngineerCommented:
whem u get the javascript error, which line it refer to?
0
JCWEBHOSTAuthor Commented:
i do not kwon
0
Meir RivkinFull stack Software EngineerCommented:
on the webpage u should see the JS error icon (yellow warning sign) on the left-bottom corner, dbl-click it and u get the JS error description with the line where the exception triggered.
0
JCWEBHOSTAuthor Commented:
ok
0
JCWEBHOSTAuthor Commented:
   protected void showMessageBox(string message)
    {
        string sScript;
        message = message.Replace("'", "\'");
        sScript = String.Format("alert('{0}');", message);
        ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", sScript, true);
    }


i have found that this works but, in my title of my message box it say: " message from this page"
how can i can it?
0
Neeraj SoniSr. ArchitectCommented:
this could be an issue of special chars in your string to be displayed.

When you pass a string to any JS function, do encode/encrypt the string otherwise chars like single quote or double quote will create problem. for example:


from serverside, you have assigned value to hidden var
    hdnName.Value = "'" + "I'am " + name + "'";     //inJS the value will be like this 'I'am Adam'

and in client side JS, you call a function:
    alert(document.getElementById("hdnName").value);

you will get an error  Input string was not in a correct format.

check if this is the case in your app also. if so, then you have to escape the chars.
0
Meir RivkinFull stack Software EngineerCommented:
0
Meir RivkinFull stack Software EngineerCommented:
here's the code.
change the title of the messagebox (replace "ClientScript" with whatever u like).
public static void MessageBox(string MyMessage)
{
//This method pops a message box onto the screen to display whatever
//text you pass into the method via the input parameter.
string ScriptString = "alert('" + MyMessage + "');";

System.Web.UI.Page page = HttpContext.Current.Handler as System.Web.UI.Page;
page.ClientScript.RegisterStartupScript(typeof(System.Web.UI.Page), "ClientScript", ScriptString, true);

}

Open in new window

0
JCWEBHOSTAuthor Commented:
 it does not seem to be working, nothing is been displayed?

  public static void showMessageBox(string MyMessage)
    {
        string ScriptString = "alert('" + MyMessage + "');";

        System.Web.UI.Page page = HttpContext.Current.Handler as System.Web.UI.Page;
        page.ClientScript.RegisterStartupScript(typeof(System.Web.UI.Page), "Household Realty", ScriptString, true);

    }
0
JCWEBHOSTAuthor Commented:
its still not changing?

why?

and it does not work for anysc postback trigger only displaying for postback with out the title been changed.
0
Meir RivkinFull stack Software EngineerCommented:
try:

public static void showMessageBox(string MyMessage)
{
System.Web.UI.Page page = HttpContext.Current.Handler as System.Web.UI.Page;
page .ClientScript.RegisterStartupScript(typeof(System.Web.UI.Page), "Household", "<script
language='JavaScript'>alert('" + MyMessage + "');", true);
}
0
JCWEBHOSTAuthor Commented:
nope nothing is been displayed now
0
JCWEBHOSTAuthor Commented:
theres no error to, why?
0
Meir RivkinFull stack Software EngineerCommented:
that should work:

protected void showMessageBox(string message)
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "Household Realty", "alert('" + message + "');", true);
}
0
JCWEBHOSTAuthor Commented:
it's working but "Household Realty", still not displaying
0
Meir RivkinFull stack Software EngineerCommented:
no title at all?
0
JCWEBHOSTAuthor Commented:
nope just says: message from web page
0
Meir RivkinFull stack Software EngineerCommented:
it seems that the title of the alert box cannot be changed.
you would have to create a custom messagebox to accomplish this.
check this post:
http://www.acejs.com/scriptsfolder/121015/121015.html
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.