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