user4135018
user4135018

Reputation:

Why is the RequiredFieldValidator not reacting?

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, "[email protected]");
        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.

Upvotes: 0

Views: 174

Answers (3)

user4135018
user4135018

Reputation:

In the end I settled for a work around based on

if (string.IsNullOrEmpty(txtYourName.Text)) { //code }

Not the solution I wanted, but it does the trick

Upvotes: 0

PaulBinCT2
PaulBinCT2

Reputation: 221

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"

Upvotes: 0

Tummala Krishna Kishore
Tummala Krishna Kishore

Reputation: 8271

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." />

Upvotes: 0

Related Questions