Simon
Simon

Reputation: 1333

Button click event not firing when inside form tags

I have a contact form on a website that sends the form to the website owner via e-mail.

Clicking the button btnSubmit runs the vb code behind to send the e-mail, which was working fine until I styled the form using Bootstrap and then the button click event stops firing.

If i take the button outside the parent form tags it works again, but i want it to be inside so it will trigger the HTML5 validation on the required fields.

The code not working:

    <div class="form-group">
        <label for="txtName">Name:</label>
        <asp:TextBox ID="txtName" runat="server" type="text" CssClass="form-control" required />
    </div>

    <div class="form-group">
        <label for="txtEmail">Email address:</label>
        <asp:TextBox ID="txtEmail" runat="server" type="email" CssClass="form-control" required />
    </div>

    <div class="form-group">
        <label for="ddlProduct">Product</label>
        <asp:DropDownList ID="ddlProduct" runat="server" CssClass="form-control" AppendDataBoundItems="True" DataSourceID="SQSItemNames" DataTextField="itemName" DataValueField="itemName">
            <asp:ListItem></asp:ListItem>
        </asp:DropDownList>
    </div>

    <div class="form-group">
        <label for="txtMessage">Message</label>
        <asp:TextBox ID="txtMessage" runat="server" type="text" CssClass="form-control" TextMode="MultiLine" Rows="5" required />
    </div>

    <asp:Button ID="btnSubmit" runat="server" Text="Send" CssClass="btn btn-primary pull-right" />

</form>

Edit: Not sure if this makes a difference but the page in question references a master page.

Upvotes: 1

Views: 1576

Answers (3)

Web Develop Wolf
Web Develop Wolf

Reputation: 6316

You should only ever have one set of form tags - so an open and a close.

If you have a form inside a form that will knock out your functionality.

If you still want to use bootstrap styling you can replace the form tags with div tags.

Upvotes: 1

Andr&#233; Secco
Andr&#233; Secco

Reputation: 71

your code is not calling your event in the code behind, lacking reference the event in his button, for example:

<asp:Button ID="btnSubmit" runat="server" Text="Send" CssClass="btn btn-primary pull-right" OnClick="btnSubmit_Click" />

Upvotes: 2

Joseph Morgan
Joseph Morgan

Reputation: 161

I use Razor, not web forms, so this might be a little off in the syntax, but try using

<asp:input type="submit" ... />

That way you are explicitly telling the button that it is to submit the form.

Upvotes: 0

Related Questions