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

问题描述 投票:0回答:4

我已经筋疲力尽了谷歌,我无法找到解决这个问题的方法。单击.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>

和我在文件后面的相应代码,添加User.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标记并完全删除它仍然无法正常工作。我已经从使用按钮事件的其他页面复制/粘贴了尽可能多的代码,但它仍然无效。我完全不知道现在发生了什么。 > _ <

编辑:以防万一它会有任何帮助,这是我的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内部消失:标签!!!!!

c# .net onclick code-behind
4个回答
0
投票

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


0
投票

我在.cs代码后面找不到你的tb_btnAddUserSubmit声明,以及处理程序。

您需要有一个AddUser.Designer.cs类。一定要检查一下。

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

© www.soinside.com 2019 - 2024. All rights reserved.