1

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?

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

Comments

2

If you need to use an html button, instead of an ASP Button control (for instance, if you need something other than text in the button), you can still do it like this (using runat=server and 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>

1 Comment

Works great! Without this, we could not use any of the nice Bootstrap buttons in Asp.Net, and the regular asp buttons are ugly.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.