Search code examples
asp.netonclicklinkbutton

Why is linkbutton OnClick not firing?


This is driving me crackers, it's very basic code, just a very basic three textbox contact form and a linkbutton.

<div class="form-group has-feedback">
<asp:Label ID="lblYourName" AssociatedControlID="txtYourName" CssClass="col-sm-3 control-label" runat="server" Text="Your name"></asp:Label>
 <div class="col-sm-6">
 <asp:TextBox ID="txtYourName" TextMode="SingleLine" CssClass="form-control" runat="server" placeholder="Your name"></asp:TextBox>
 <span class="glyphicon glyphicon-user form-control-feedback"></span>
 <asp:Label ID="lblNoName" runat="server" Visible="false" Text="Please enter your name"></asp:Label>
 </div>
 </div>
 <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>
 <asp:Label ID="lblNoEmail" runat="server" Visible="false" Text="Please enter your email"></asp:Label>
 </div>
 </div>
 <div class="form-group">
 <asp:Label ID="lblMessage" AssociatedControlID="txtMessage" CssClass="col-sm-3 control-label" runat="server" Text="Your message"></asp:Label>
 <div class="col-sm-6">
 <asp:TextBox ID="txtMessage" CssClass="form-control" TextMode="MultiLine" placeholder="Your message" runat="server"></asp:TextBox>
 </div>
 </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 OnClick of the linkbutton points to this simple MailMessage method where it checks name and email boxes are filled in and then sends an email.

protected void lnkSubmit_Click(object sender, EventArgs e)
    {
        lblNoName.Visible = false;
        lblNoEmail.Visible = false;
        if (string.IsNullOrEmpty(txtYourName.Text) || string.IsNullOrEmpty(txtEmail.Text))
        {
            if (!string.IsNullOrEmpty(txtYourName.Text))
            {
                lblNoName.Visible = false;
            }
            else
            {
                lblNoName.Visible = true;
            }
            if (!string.IsNullOrEmpty(txtEmail.Text))
            {
                lblNoEmail.Visible = false;
            }
            else
            {
                lblNoEmail.Visible = true;
            }
        }
        else
        {
            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.websitelive.net";
            smtp.Send(mm);
            panContactThanks.Visible = true;
        }
    }

But when I click the submit button, nothing happens. At all. The OnClick doesn't even fire so the breakpoint in the code behind doesn't even get called. It's as if the OnClick even isn't even in the code and it's just a dummy button. What have I missed out?


Solution

  • Please try CauseValidation="false" in link button.

    Like:

    <asp:LinkButton ID="lnkSubmit" runat="server" OnClick="lnkSubmit_Click"  
        CauseValidation="false"  CssClass="btn standard-hover-effect bg-red btn-lg btn-block">