简体   繁体   中英

What do I need to do to make this button run the code in the code behind?

On the .aspx page is basically this code in general

I'm doing a contact us form for a project but the button does not execute the code in the (.cs) code behind...

I tried using the textbook material to diagnose (nothing said about a faulty button), even looking up some articles here. maybe i missed the article

I tried replacing the button, didn't work

But here is the code snippet of the form

<div class="main-form">
    <h3 class="contactus-title">Leave US A Message</h3>
    <div class="row">
        <form>
            <div class="main-form">
                <h3 class="contactus-title">Leave Message</h3>
                <div class="row">

                    <div class="col-sm-6">
                        &nbsp;
                        <asp:TextBox ID="txtName" runat="server" placeholder="Name" required=""></asp:TextBox>
                    </div>
                    <div class="col-sm-6 ">
                        &nbsp;
                        <asp:TextBox ID="txtEmail" runat="server" placeholder="Email"></asp:TextBox>
                        <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" required="" ErrorMessage="Please input a valid email address." ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" ControlToValidate="txtEmail"></asp:RegularExpressionValidator>
                    </div>
                    <div class="col-sm-6 ">
                        <asp:TextBox ID="txtPhone" runat="server" placeholder="Phone (Optional)"></asp:TextBox>
                        <asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server" ErrorMessage="Please input a valid Singaporean Phone Number." ValidationExpression="[6|8|9]\d{7}|\+65[6|8|9]\d{7}|\+65\s[6|8|9]\d{7}" ControlToValidate="txtPhone"></asp:RegularExpressionValidator>
                    </div>
                    <div class="col-sm-6 ">
                        <asp:TextBox ID="txtSub" runat="server" placeholder="Subject" required=""></asp:TextBox>
                    </div>
                    <div class="col-xs-12 ">
                        <asp:TextBox ID="txtMessage" runat="server" Rows="3" TextMode="MultiLine" placeholder="Message" required=""></asp:TextBox>
                    </div>
                    <div class="col-xs-12  text-center">
                        <asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" />
                        <br />
                        <asp:Label ID="lblResult" runat="server"></asp:Label>
                    </div>
                </div>
            </div>
        </form>
    </div>
</div>

Code for the btnSubmit_Click method (based on textbook code given)

protected void btnSubmit_Click(object sender, EventArgs e)
{
    int result = 0;

    _Contact Feedback = new _Contact(txtName.Text,txtEmail.Text,txtPhone.Text,txtSub.Text,txtMessage.Text);
    result = Feedback.Feedback();

    if (result > 0)
    {
        Response.Write("<script>alert('Insert successful');</script>");
    }
    else 
    {
        Response.Write("<script>alert('Insert NOT successful');</script>");
    }
}

What is expected is that it is to execute the code behind. Actual result is nothing happens no matter how many times I click the button. No error messages were displayed whatsoever because the code behind did not execute.

You have a tag within your div structure.

<div class="main-form">
<h3 class="contactus-title">Leave US A Message</h3>
<div class="row">
    **<form>**
        <div class="main-form">

You can have only one form for a page. It should work if you remove the tags (opening and closing). That's an educated guess as you haven't posted the full aspx page code.

Make your form tag as

form runat="server" may be this help . Also take care of closing this form tag

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM