Link to home
Start Free TrialLog in
Avatar of lapucca
lapucca

asked on

How to display an alert dialog when running code in C# at Server?

Hi, I'm using VS2012, C# and ASP.NET

When a variable is test null, what's the easiest way to pop up an alert dialog to user with a OK button?  After user click the OK button, can it return to continue the C# code at server?

Thank you.
Avatar of Walter Padrón
Walter Padrón
Flag of United States of America image

IMHO this is not possible a popup alert runs in the client and the test run in the server. You must check the variable before sending data to the server.
Avatar of lapucca
lapucca

ASKER

I would think there is a way to call up client side jquery or javascript alert messagebox from C#.  I just don't know how to do it.  In my C# code if a variable is tested null then I would like to give a pop up message to users and then I would like to reload the current page when users clicks ok on the pop up messagebox.
Have a look at this link
http://www.aspsnippets.com/Articles/Show-Alert-Message-in-ASPNet-from-Server-Side-using-C-and-VBNet.aspx

  string message = "Zero entered.";
  System.Text.StringBuilder sb = new System.Text.StringBuilder();
  sb.Append("<script type = 'text/javascript'>");
  sb.Append("window.onload=function(){");
  sb.Append("alert('");
  sb.Append(message);
  sb.Append("')};");
  sb.Append("</script>");
  ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", sb.ToString());

Open in new window

@Michael74 the code from the article launch the alert message from Page_Load event that means you will launch the popup every time  you load the page.

@lapucca you CAN NOT send the alert to the client, stop server code execution, wait for user response and then awake the server thread and continue. This is not how the client/server architecture works.

You must check the user input for null BEFORE sending the data to the server.
Or depending on the framework you are using you can send validation results back to the client.
ASKER CERTIFIED SOLUTION
Avatar of Michael Fowler
Michael Fowler
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of lapucca

ASKER

Thank you.