Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 332
  • Last Modified:

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.
0
lapucca
Asked:
lapucca
  • 3
  • 2
  • 2
1 Solution
 
Walter PadrónCommented:
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
 
Michael FowlerSolutions ConsultantCommented:
0
 
lapuccaAuthor Commented:
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
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 
Michael FowlerSolutions ConsultantCommented:
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
 
Walter PadrónCommented:
@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
 
Michael FowlerSolutions ConsultantCommented:
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
 
lapuccaAuthor Commented:
Thank you.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now