为什么linkbutton OnClick没有触发?

时间:2016-09-03 14:04:24

标签: asp.net onclick 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>

链接按钮的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甚至不在代码中,它只是一个虚拟按钮。我错过了什么?

3 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

您可能忘记在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

答案 2 :(得分:0)

请在链接按钮中尝试CauseValidation="false"

像:

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