1

Does anyone know how can I create a pop-up message box in server side, so that it will display the error message in the pop-up message box when save process is failed?

Example:

   protected void btnSave_Click(object sender, EventArgs e)
   {
       try
       {
           using (TransactionScope scope = new TransactionScope())
           {
               //save process

               scope.Complete();
               Response.Redirect(url);
           }
       }
       catch (TransactionAbortedException ex)
       {
          //pop-up message box to show error message
       }
       catch (ApplicationException ex)
       {
          //pop-up message box to show error message
       }
   }

How can I able to create a pop-up message box within the catch to pop-up the error message box to the user when the save process is failed?

3
  • You want the popup to appear on the server or the client? Commented May 23, 2011 at 1:58
  • 1
    @Kiranu - please tell me that was a trick question - you cannot get a message box to pop up on the server! Commented May 23, 2011 at 2:09
  • @slugster - I know... but it wasn't clear if what he wanted was something that was possible (a popup in the client) or impossible (a form created by a windows service) Commented May 23, 2011 at 3:19

4 Answers 4

2
ClientScript.RegisterStartupScript(
    this.GetType(), "myalert", "alert('" + errorText + "');", true);

or

 Response.Write(
     @"<SCRIPT LANGUAGE=""JavaScript"">alert('" + errorText + "')</SCRIPT>");
Sign up to request clarification or add additional context in comments.

5 Comments

@Alex Aza; I applied the code you have giving before the try{}catch() .. it's doesn't seem any message box been pop-up
@Jin Yong - weird, I tested this on .NET 2, 3.5 and 4 - worked for me.
Maybe javascript is turned off from the browser. Try turning it on.
@Jin - I just realized, you cannot test it like this, you can't apply it before try catch because you redirect to run Response.Redirect(url); in a normal scenario, which redirects to another page. To test properly, add one my line and do return just after that line.
@Jin Yong - out of curiosity, what was the solution that worked for you? Did you use 'Page.RegisterStartupScript'?
1

Try using either the Page.RegisterStartupScript or ClientScript.RegisterStartupScript methods.

2 Comments

Page.RegisterStartupScript is deprecated.
Hi Drew, a good way to get accepted answers is to elaborate on the concepts in the question and then provide a specific working example. Alternatives are always good as well. A complete answer is likely to generate more Stack Overflow points for you over time. Hope this helps.
0

Here's one way:

/// ---- ShowAlert --------------------------------
///     
/// <summary>
/// popup a message box at the client    
/// </summary>
/// <param name="page">A Page Object</param>
/// <param name="message">The Message to show</param>

public static void ShowAlert(Page page, String message)
{
    String Output;
    Output = String.Format("alert('{0}');",message);
    page.ClientScript.RegisterStartupScript(page.GetType(), "Key", Output, true);
}

3 Comments

How this is different from my answer? :)
@Alex - When I posted my answer, yours was not yet visible.
that's weird. Our answers are 15 minutes apart. So now when you see this, you can remove the duplicate answer, or change it to be different, right?
0

While the alert() scripts mentioned are OK, they come across pretty amateurish. I would recommend two options.

  1. Have a label on your page which is normally hidden with font color red, and bold. Then just make it visible and set the text on error.

  2. If you want a dialog behavior, use what everyone is showing you, but instead of popping up an alert, pick something nice from jQuery UI with a nice error icon and even a help link or something in the dialog. Here's an example: http://jqueryui.com/demos/dialog/#modal-message

Of course, if you want your app to seem more reliable, don't show them any error. :)

Comments

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.