Why is the RequiredFieldValidator not reacting?

时间:2016-08-31 17:01:46

标签: asp.net webforms onclick requiredfieldvalidator

I have built a simple contact form in asp.net webforms. I fill in the fields, click submit, everything runs fine and I receive an email a few moments later. So far so normal.

But leaving the fields blank throws an exception, as you would expect, string txtEmail cannot be empty or words to that effect. So I thought I'd add an "asp:RequiredFieldValidator", but now when I click submit, nothing happens. So in Chrome, I inspected the code, found the validator code which had a style set to "display: none", in essence they are not being fired and the warning is not being displayed. I filled in the fields and clicked submit, and again nothing happened. I reloaded the page, filled in the fields and clicked submit and nothing happened. It appears that the inclusion of the RequiredFieldValidator is blocking the OnClick actionon the button. If I remove them, it works fine.

Any ideas?

Edit: Some code

<div class="form-group has-feedback">
<asp:Label ID="lblEmail" runat="server" AssociatedControlID="txtEmail" Text="Email" CssClass="col-sm-3 control-label"></asp:Label>
<div class="col-sm-6">
<asp:TextBox ID="txtEmail" CssClass="form-control" runat="server" placeholder="Email" TextMode="Email"></asp:TextBox>
<span class="glyphicon glyphicon-envelope form-control-feedback"></span>
</div>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" Display="Dynamic" runat="server" ErrorMessage="Please enter your email" ControlToValidate="txtEmail" CssClass="text-danger">
</asp:RequiredFieldValidator>
</div>
<div class="row">
<div class="col-sm-offset-3 col-sm-6">
<asp:LinkButton ID="lnkSubmit" runat="server" OnClick="lnkSubmit_Click" CssClass="btn standard-hover-effect bg-red btn-lg btn-block">
<span class="text">Contact us <i class="fa fa-arrow-right"></i></span>
</asp:LinkButton>
</div>
</div>

The click event -

protected void lnkSubmit_Click(object sender, EventArgs e)
    {
        MailMessage mm = new MailMessage(txtEmail.Text, "foo@bar.com");
        mm.Subject = "Feedback from website";
        mm.Body = "Email from " + txtYourName.Text + "<br /><br />" + txtMessage.Text;
        SmtpClient smtp = new SmtpClient();
        smtp.Host = "mail.domain.net";
        smtp.Send(mm);
        //panContact.Visible = false;
        panContactThanks.Visible = true;
    }

As I say, without the required field validators, the code works fine. If I put them in, the form doesn't work.

3 个答案:

答案 0 :(得分:0)

Required Field Validator Usage like this

Asp Textbox with Validator

     <asp:TextBox ID="txttitle" CssClass="form-control" 
MaxLength="15" runat="server"></asp:TextBox>

    <asp:RequiredFieldValidator runat="server" 
ControlToValidate="txttitle" CssClass="text-danger" ErrorMessage="The Title field is required." />

答案 1 :(得分:0)

Without looking it up... are you sure "display:none" isn't an incorrect setting? My recollection is that the display type is a formatting property, where you set static/dynamic to hold space or permit flow? Maybe by having it set to display: none, you are firing the invalid event but just not seeing it...try setting it to "display:static" or "dynamic"

答案 2 :(得分:0)

最后,我决定基于

进行解决
if (string.IsNullOrEmpty(txtYourName.Text)) { //code }

不是我想要的解决方案,但它可以解决问题