title: Is there an easier way to show an alert dialog with ASP.NET?
body: <p>I'm using ASP.NET and C# to create a form.  When the user clicks submit, I validate the inputs and continue.  If for any reason the inputs are not valid, I show an alert notifying the user of the issues.  If everything checks out, I do my work with the given input.  My problem here is that when I click the submit button, the page refreshes, erasing all input.  Here's an example on what I'm doing in the code behind:</p>
  
  <pre><code>saveButton.Click += (s, e) =&gt; {
      var errors = "";
      if (string.IsNullOrWhitespace(myField.Text)) errors += "myField is empty or consists of only whitespace.\n";
      //do this for each field
      if (errors == "") { //no errors found!
          //use the input
      }
      else { //something's invalid
          //alert the user
          ShowMessage("Please review these problems before continuing:\n\n" + errors);
      }
  };
  </code></pre>
  
  <p>No exceptions are thrown, but if a purposely send invalid input, the page refreshes before it can display the popup.  Here's what I've tried for <code>ShowMessage()</code>:</p>
  
  <pre><code>private void ShowMessage(string msg) {
      ClientScript.RegisterClientScriptBlock(GetType(), DateTime.Now.ToString(), "alert(\'" + message + "\');", true);
  }
  </code></pre>
  
  <p>and</p>
  
  <pre><code>private void ShowMessage(string msg) {
       HttpContext.Current.Response.Write("&lt;script language=\"JavaScript\"&gt;alert(\"" + message + "\");&lt;/script&gt;");
  }
  </code></pre>
  
  <p>Again, each one refreshes the page before the message is shown!
  Is there an easier way to show a message dialog without refreshing the page?</p>