C# – ASP.NET C# Button OnClick even not firing

.netc++code-behindonclick

I've absolutely exhausted Google and I can't find a solution to this problem. When click a button on my .aspx page, the corresponding function is not called from .aspx.cs file. I'll just post my code and see if there are any takers.

AddUser.aspx:

<%@ Page Title="" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="AddUser.aspx.cs" Inherits="AddUser" %>

<asp:Content ID="tb_AddUserHeader" ContentPlaceHolderID="tb_HeaderPlaceHolder" Runat="Server">
</asp:Content>

<asp:Content ID="tb_AddUserContent" ContentPlaceHolderID="tb_ContentPlaceHolder" Runat="Server">
    <h1>Add User</h1>
    <fieldset>
        <asp:Label ID="tb_lblAddUser_Authorized" runat="server" Visible="false">
            <br />
            <table>
                <tr>
                    <td><u>Username:</u></td>
                    <td><asp:TextBox ID="tb_txtbxUsername" runat="server" Width="200"></asp:TextBox></td>
                </tr>
                <tr>
                    <td><u>Password:</u></td>
                    <td><asp:TextBox ID="tb_txtbxPassword" runat="server" Width="200" TextMode="Password"></asp:TextBox></td>
                </tr>
                <tr>
                    <td><u>Account Type:</u></td>
                    <td><asp:DropDownList ID="tb_ddAccountType" runat="server">
                        <asp:ListItem Value="v" Text="Viewer"></asp:ListItem>
                        <asp:ListItem Value="t" Text="Tester"></asp:ListItem>
                        <asp:ListItem Value="a" Text="Admin"></asp:ListItem>
                        </asp:DropDownList></td>
                </tr>
                <tr>
                    <td><asp:Button ID="tb_btnAddUserSubmit" runat="server" Text="Submit" OnClick="tb_btnAddUserSubmit_Click" UseSubmitBehavior="true" /></td>
                </tr>
            </table>
            <br />
        </asp:Label>

        <asp:Label ID="tb_lblAddUser_Output" runat="server" Visible="false"></asp:Label>

        <asp:Label ID="tb_lblAddUser_Unauthorized" runat="server" Visible="true">
            <br />Only administrators are authorized to view this page
            <br />
            <br />
        </asp:Label>
    </fieldset>
</asp:Content>

and my corresponding codebehind file, AddUser.aspx.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using tbBusinessObjects;
using tbWebClientControllers;

public partial class AddUser : System.Web.UI.Page
{
    tbWebUserController m_userController;

    protected void Page_Load(object sender, EventArgs e)
    {
        m_userController = new tbWebUserController();

        //if (this.IsPostBack)
        //{
        //    String username = tb_txtbxUsername.Text;

        //    //tb_btnAddUser_Click(sender, e);
        //}
        //else
        {
            if (HttpContext.Current.User.Identity.IsAuthenticated)
            {
                FormsIdentity id = (FormsIdentity)HttpContext.Current.User.Identity;
                FormsAuthenticationTicket ticket = id.Ticket;

                if (ticket.UserData == "a")
                {
                    tb_lblAddUser_Authorized.Visible = true;
                    tb_lblAddUser_Unauthorized.Visible = false;
                }
            }
        }
    }

    protected void tb_btnAddUserSubmit_Click(object sender, EventArgs e)
    {
        tbUser user = new tbUser();
        user.m_username = tb_txtbxUsername.Text;
        user.m_password = tb_txtbxPassword.Text;
        user.m_type = tb_ddAccountType.SelectedValue.ToCharArray()[0];

        if (m_userController.InsertUser(user))
        {
            tb_lblAddUser_Output.Text = "<br />User was successfully added<br /><br />";
        }
        else
        {
            tb_lblAddUser_Output.Text = "<br />There was an error adding user<br /><br />";
        }

        tb_lblAddUser_Output.Visible = true;
        tb_lblAddUser_Authorized.Visible = false;
    }
}

I've set multiple breakpoints in my Click function and they're never hit. I tried simply catching my Page_Load function with a Page.IsPostBack, but it none of the data is saved from the textboxes or dropdown.

I've also tried changing the UseSubmitBehavior tag between true and false and removing it completely and it still doesn't work. I've copy/pasted as much code from other pages that use button events that are working and it still won't work. I have absolutely no idea what is going on right now. >_<

EDIT: And just in case it would help in any way, here is my Site.master…

<%@ Master Language="C#" AutoEventWireup="true" CodeFile="Site.master.cs" Inherits="SiteMaster" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head id="tb_head" runat="server">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
    <title>KersTech Hydraulic/Electric Hybrid Data Recording and Telemetry</title>

    <link href="tb_style.css" rel="stylesheet" type="text/css" media="screen" />

    <asp:ContentPlaceHolder id="tb_HeaderPlaceHolder" runat="server">
    </asp:ContentPlaceHolder>

</head>

<body>

<form id="tb_formBody" runat="server">

<!-- Begin wrapper -->
<div id="tb_wrapper">
<asp:Label ID="tb_lblFormsAuthenticationUserData" runat="server" Text="Nothing" Visible="false"></asp:Label>

<!-- Begin top -->
<div id="tb_top">
    <ul id="tb_nav">
        <asp:Label ID="tb_lblMasterMenu" runat="server"></asp:Label>
        <li><asp:LoginStatus ID="tb_LoginStatus" runat="server" LogoutAction="Redirect" LogoutPageUrl="Logout.aspx" /></li>
    </ul>
    <div id="tb_Greeting">
        <asp:LoginView ID="tb_MasterLoginView" runat="server">
            <LoggedInTemplate>
                Logged in as <asp:LoginName ID="MasterLoginName" runat="server" />
            </LoggedInTemplate>
        </asp:LoginView>
    </div>

    <asp:Label ID="test" runat="server"></asp:Label>
</div>

<!-- Begin content -->
<div id="tb_content">
    <asp:ContentPlaceHolder id="tb_ContentPlaceHolder" runat="server">
    </asp:ContentPlaceHolder>
</div>
<!-- End content -->

<!-- Begin footer -->
<div id="tb_footer"><div id="something">
    <!-- Begin badges -->
    <div>
        <a href="http://validator.w3.org/check?uri=referer"><img src="http://www.w3.org/Icons/valid-xhtml10" alt="Valid XHTML 1.0 Strict" height="31" width="88" /></a> <!-- HTML validation badge -->
        <a href="http://jigsaw.w3.org/css-validator/check/referer">
            <img style="border:0;width:88px;height:31px" src="http://jigsaw.w3.org/css-validator/images/vcss" alt="Valid CSS!" /> <!-- CSS validation badge -->
        </a>
    </div></div>
    <!-- End badges -->
</div>
<!-- End footer -->

</div>
<!-- End wrapper -->

</form>


</body>

</html>

EDIT2: I moved my Button outside of my label and manually set the button's visibility along with the label's visibility (that was the only reason I was using those labels in the first place), and it worked just fine. I just spent the last hour and a half trying to figure this out, so in case anyone else ever has the same issue again:

ASP:BUTTONs WILL NOT FIRE FROM INSIDE OF ASP:LABELs!!!!!

Best Solution

try butting this line in the page load

tb_btnAddUserSubmit.Click += new EventHandler(tb_btnAddUserSubmit_Click);

I think the problem may be that the click handler is not register in the click event