title: server side validation on ajax modal popup extender
body: <p>I have a program where i use a modal popup that allows the user to add new items to a database, this is working fine but i would like to add some validation. For instance when an exception is thrown due to a duplicate entry. I have looked through a couple of examples on how to use the asp.net custom Validation control. </p>
  
  <p>The problem is that as soon as the validation event fires the modal popup disappears due to the post back.  </p>
  
  <pre><code>&lt;asp:Panel ID="panComp" runat="server" Height="180px" Width="400px" cssclass="ModalWindow"&gt;   
          &lt;table width="100%"&gt;
                   &lt;tr&gt; 
                        &lt;td&gt;&lt;asp:Label Text="Name" runat="server" /&gt;&lt;/td&gt; &lt;td&gt;&lt;asp:TextBox ID="txtCompName" runat="server" /&gt;
                           &lt;asp:CustomValidator ID="CustomValidator1" OnServerValidate="btnAddComp_Click" runat="server" ErrorMessage="Competency already exists" ControlToValidate="txtCompName" /&gt;
                           &lt;cc1:ValidatorCalloutExtender  ID="ValidatorCalloutExtender1" runat="server" TargetControlID="CustomValidator1" /&gt;                                                               
                        &lt;/td&gt;
               &lt;/tr&gt;   
          &lt;/table&gt;
          &lt;br /&gt;
          &lt;asp:Button ID="btnAddComp" runat="server" Text="Add" /&gt;
          &lt;asp:Button ID="btnCancel" runat="server" Text="Cancel" /&gt;
    &lt;/asp:Panel&gt;
  </code></pre>
  
  <p>The event fires as it should and the modalPopupExtender OnOkScript doesn't has a value</p>
  
  <pre><code>protected void btnAddComp_Click(object source, ServerValidateEventArgs args)
  {
      if (!String.IsNullOrEmpty(txtCompName.Text))
      {
          try
          {
              _ass.AddCompetency(txtCompName.Text);                
              args.IsValid = true;
          }
          catch (Exception)
          {
              args.IsValid = false;
          }
      }
  
  }
  </code></pre>