ASP.NET C#Button OnClick甚至没有触发

时间:2013-05-01 09:51:02

标签: c# .net onclick code-behind

我已经筋疲力尽了谷歌,我无法找到解决这个问题的方法。单击.aspx页面上的按钮时,不会从.aspx.cs文件中调用相应的函数。我只是发布我的代码,看看是否有任何参与者。

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>

和我对应的代码隐藏文件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;
    }
}

我在Click功能中设置了多个断点,但它们从未被击中过。我尝试使用Page.IsPostBack捕获我的Page_Load函数,但是没有数据是从文本框或下拉列表中保存的。

我也尝试在true和false之间更改UseSubmitBehavior标记并将其完全删除,但仍然无法正常工作。我已经从使用按钮事件的其他页面复制/粘贴了尽可能多的代码,但它仍然无效。我完全不知道现在发生了什么。 &GT; _&LT;

编辑:以防万一它会有任何帮助,这是我的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:我将Button移到我的标签之外,手动设置按钮的可见性以及标签的可见性(这是我首先使用这些标签的唯一原因),并且它工作得很好。我只花了最后一个半小时试图解决这个问题,以防万一其他人再次遇到同样的问题:

ASP:按钮不会从ASP内部消失:标签!!!!!

4 个答案:

答案 0 :(得分:0)

尝试在页面加载中删除此行

tb_btnAddUserSubmit.Click += new EventHandler(tb_btnAddUserSubmit_Click);

我认为问题可能是点击处理程序未在点击事件中注册

答案 1 :(得分:0)

您是否从其他页面/应用程序复制了此方法?如果是,那么它将无法工作,所以你需要删除分配给按钮的事件和事件名称然后转到设计并转到按钮事件属性转到onClick事件双击它旁边,它将生成事件并自动分配事件名称到按钮。这应该工作

答案 2 :(得分:0)

在页眉中尝试AutoEventWireup =“false”。我在5年内没有在ASP.net工作,但我记得有时会破坏它。

答案 3 :(得分:0)

我在后面的.cs代码和处理程序中找不到您的tb_btnAddUserSubmit声明。

您需要具有AddUser.Designer.cs类。确保检查一下。

您还需要在OnInit而不是Page_load中注册事件。