为什么 linkbutton OnClick 没有触发?

Why is linkbutton OnClick not firing?

这让我很兴奋,这是非常基本的代码,只有一个非常基本的三个文本框联系表单和一个链接按钮。

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

链接按钮的 OnClick 指向这个简单的 MailMessage 方法,它检查姓名和邮箱是否已填写,然后发送电子邮件。

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, "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.websitelive.net";
            smtp.Send(mm);
            panContactThanks.Visible = true;
        }
    }

但是当我点击提交按钮时,没有任何反应。完全没有。 OnClick 甚至不会触发,因此甚至不会调用后面代码中的断点。就好像 OnClick 甚至不在代码中一样,它只是一个虚拟按钮。我错过了什么?

将您的 html 包裹在表单标签中

<form id="someForm" runat="server">


 <!--your html-->

</form>

您正在使用 .net 服务器控件,因此您需要将它们包装在表单中。不要忘记添加 runat="server" http://forums.asp.net/t/1463877.aspx?Does+an+aspx+must+have+a+form+tag+

可能您忘记在 Page_load

中添加 isPostBack 条件
protected void Page_Load(object sender, EventArgs e)    
{
    if(!IsPostBack)
    {
        //Control initialization here
    }
}

看看 ASP.NET 页面生命周期:https://msdn.microsoft.com/en-us/library/ms178472.aspx IsPostBack:https://msdn.microsoft.com/en-us/library/system.web.ui.page.ispostback(v=vs.110).aspx

请在 link 按钮中尝试 CauseValidation="false"

喜欢:

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