Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CustomValidator message doesnt show up

Tags:

c#

asp.net

I've a CustomValidator and I defined every possible parameter of it:

<asp:CustomValidator ID="custom" runat="server" Text="*" ErrorMessage="This email address is already registered" ControlToValidate="txtEmail" OnServerValidate="isExist" Display="None" ValidationGroup="valRegister"></asp:CustomValidator>

PS: I've a RequiredFieldValidator for same textbox and I dont want to check empty value.

Here are other objects of the form:

<div class="row"><asp:Label runat="server" Text="Email" AssociatedControlID="txtEmail"></asp:Label><asp:RequiredFieldValidator runat="server" ErrorMessage="Please enter your email" Text="*" ControlToValidate="txtEmail"></asp:RequiredFieldValidator><asp:TextBox ID="txtEmail" runat="server" CssClass="inpBox"></asp:TextBox></div>

<asp:Button runat="server" Text="Register" CssClass="btn" OnClick="register_member" CausesValidation="true" ValidationGroup="valRegister" />

<asp:ValidationSummary ID="validationSummary" runat="server" ShowMessageBox="true" ShowSummary="false" ValidationGroup="valRegister" />

protected void isExist(object sender, ServerValidateEventArgs args){
if (cre.member.isExist(args.Value)){
    args.IsValid = false;
} else {
    args.IsValid = true;
}

}

When I put an email already exist in the db table * appears on the form, but the error message doesnt show up. I tried all display options for custom error but no luck.

like image 473
dvdmn Avatar asked Dec 19 '12 20:12

dvdmn


2 Answers

I took the code exactly as in your question.

Changing Display="None" to Display="Dynamic" in the asp:CustomValidator causes the asterisk to appear.

Changing ShowSummary="false" to ShowSummary="true" in the asp:ValidationSummary causes the error message to appear in the summary.

like image 88
Carson63000 Avatar answered Nov 03 '22 20:11

Carson63000


Changing the Display to "Dynamic" or anything doesn't really do anything if the server is not handling the validation manually, especially when using <asp:CustomValidator. Even a ValidationGroup with or without a ValidationSummary does nothing.

Always force a validation on the server before allowing the user to exit the form/gridview/etc.

ie

            ...your form here...
             <tr>
                <td colspan="3" style="text-align: center" valign="top">
                    <asp:Button ID="ButtonSubmit" runat="server" Text="Submit" OnClick="Submit_Click" CausesValidation="true"  />
                    <asp:Button ID="ButtonCancel" runat="server" Text="Cancel" OnClick="Cancel_Click" CausesValidation="false"  />
                </td>
            </tr>
        </table>
    </asp:Panel>

...
    protected void Submit_Click(object sender, EventArgs e)
    {
        Page.Validate();
        if (Page.IsValid)
        {
           //processing done after a successful submit here!
        }
    }

The Page.Validate() will force the validation controls to check and display your error message.

like image 41
Fandango68 Avatar answered Nov 03 '22 20:11

Fandango68