tags:

views:

99

answers:

1

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.

The problem is that as soon as the validation event fires the modal popup disappears due to the post back.

<asp:Panel ID="panComp" runat="server" Height="180px" Width="400px" cssclass="ModalWindow">   
        <table width="100%">
                 <tr> 
                      <td><asp:Label Text="Name" runat="server" /></td> <td><asp:TextBox ID="txtCompName" runat="server" />
                         <asp:CustomValidator ID="CustomValidator1" OnServerValidate="btnAddComp_Click" runat="server" ErrorMessage="Competency already exists" ControlToValidate="txtCompName" />
                         <cc1:ValidatorCalloutExtender  ID="ValidatorCalloutExtender1" runat="server" TargetControlID="CustomValidator1" />                                                               
                      </td>
             </tr>   
        </table>
        <br />
        <asp:Button ID="btnAddComp" runat="server" Text="Add" />
        <asp:Button ID="btnCancel" runat="server" Text="Cancel" />
  </asp:Panel>

The event fires as it should and the modalPopupExtender OnOkScript doesn't has a value

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;
        }
    }

}
+1  A: 

Hey,

That is tricky because you need the postback to happen for the custom validator. What you can do is check if the page is valid; if an error is present, you can show the panel by calling the modal extender Show method either on the server or the client to reshow it with the error.

Otherwise, the modal popup does not retain its open state across postbacks.

HTH.

Brian
Hrm I think that is the only option I have, thanks ;)
fluf
That did the trick thanks :D
fluf