简体   繁体   中英

Bootstrap Button_Click Event Not Firing

I have a form designed with a button which should call a stored procedure. For now, I just want the button to display text in a textbox (just to make sure the button is working). However, I cannot get it to fire the button_click event.

Here is what I have:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
<form id="form1" runat="server">
<div class="container-fluid">
    <section class="container">
        <div class="container-page">                
            <div class="col-md-6">
                <h3 class="dark-grey">Registration</h3>

                <div class="form-group col-lg-12">
                    <label>Username</label>
                    <input type="" name="" class="form-control" id="" value="">
                </div>

                <div class="form-group col-lg-6">
                    <label>Password</label>
                    <input type="password" name="" class="form-control" id="" value="">
                </div>

                <div class="form-group col-lg-6">
                    <label>Repeat Password</label>
                    <input type="password" name="" class="form-control" id="" value="">
                </div>

                <div class="form-group col-lg-6">
                    <label>Email Address</label>
                    <input type="" name="" class="form-control" id="" value="">
                </div>

                <div class="form-group col-lg-6">
                    <label>Repeat Email Address</label>
                    <input type="" name="" class="form-control" id="" value="">
                </div>          


            <div class="col-md-6">
                <h3 class="dark-grey">Terms and Conditions</h3>
                <p>
                    By clicking on "Register" you agree to the Terms and Conditions.
                </p>

                <button type="submit" class="btn btn-primary" onclick="btnSumbit_Click">Register</button>
                <br />
                <br />
                <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            </div>
        </div>
    </section>
</div>
</form>
</body>
</html>

My c# code is:

protected void btnSumbit_Click(object sender, EventArgs e)
{
    TextBox1.Text = "Registered";
}

Any idea why this will not work?

My guess is that you are getting a normal <button> mixed up with an <asp:Button> .

Your current implementation wants to run a javascript function name btnSubmit_Click and does not have any connection to your C# code-behind.

...
<asp:Button type="submit" CssClass="btn btn-primary" OnClick="btnSumbit_Click" runat="server">Register</asp:Button>
...

I haven't been able to test if it works myself.

如果您需要使用html按钮而不是ASP Button控件(例如,如果您需要按钮中的文本以外的其他内容),您仍然可以这样做(使用runat = server和OnServerClick):

<button runat="server" type="button" id="updateButton" onserverclick="UpdateEmail_Click" class="btn btn-success btn-block" title="<%$ Resources:UserMessages, UpdateCaps %>"><asp:Localize ID="Localize5" runat="server" Text="<%$ Resources:UserMessages, UpdateCaps %>"></asp:Localize></button>

If you're using a standard HTML button, you need to:

  1. Add runat="server" to your button.
  2. Set your button to use onserverclick instead of onclick .

http://msdn.microsoft.com/en-us/library/system.web.ui.htmlcontrols.htmlbutton.onserverclick(v=vs.110).aspx

So:

<button runat="server" class="btn btn-primary" onserverclick="Method_Name"...

Alternatively, you can use Bootstrap classes with the <asp:Button... tags as well. You'll simply apply them to an attribute called CssClass instead of `class'.

So:

<asp:Button type="submit" runat="server" CssClass="btn btn-primary" OnClick="Method_Name"...

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