Solved

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

Posted on 2014-12-22
7
299 Views
Last Modified: 2014-12-26
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.
0
Comment
Question by:lapucca
  • 3
  • 2
  • 2
7 Comments
 
LVL 10

Expert Comment

by:Walter Padrón
ID: 40514086
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.
0
 
LVL 23

Expert Comment

by:Michael74
ID: 40514097
0
 

Author Comment

by:lapucca
ID: 40514126
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.
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 23

Expert Comment

by:Michael74
ID: 40514282
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

0
 
LVL 10

Expert Comment

by:Walter Padrón
ID: 40515075
@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.
0
 
LVL 23

Accepted Solution

by:
Michael74 earned 500 total points
ID: 40515831
The example shows how an alert can be displayed from an event in this case the page load event. Just change this to the button click event
0
 

Author Closing Comment

by:lapucca
ID: 40519197
Thank you.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question