开发者

How to create a pop-up message box to display error message in .net c# web-applications

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 开发者_Go百科catch to pop-up the error message box to the user when the save process is failed?


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


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

or

 Response.Write(
     @"<SCRIPT LANGUAGE=""JavaScript"">alert('" + errorText + "')</SCRIPT>");


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);
}


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. :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜