ASP.NET C#按钮OnClick甚至不触发

8tntrjer  于 2023-04-13  发布在  .NET
关注(0)|答案(4)|浏览(210)

我已经彻底耗尽了谷歌,我找不到解决这个问题的方法。当点击我的.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标签,并完全删除它,但它仍然不起作用。我从其他使用按钮事件的页面复制/粘贴了尽可能多的代码,但它仍然不起作用。我完全不知道现在发生了什么。〉_〈
编辑:以防万一它会以任何方式帮助,这里是我的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>

编辑2:我把我的按钮移到了标签的外面,并手动设置按钮的可见性沿着标签的可见性(这是我最初使用这些标签的唯一原因),它工作得很好。我花了一个半小时试图弄清楚这一点,所以如果其他人再次遇到同样的问题:
ASP:按钮无法从ASP:标签内部击发!!!!

vbopmzt1

vbopmzt11#

尝试在页面加载中对接此行

tb_btnAddUserSubmit.Click += new EventHandler(tb_btnAddUserSubmit_Click);

我认为问题可能是click处理程序没有在click事件中注册

cnjp1d6j

cnjp1d6j2#

如果是的话,它将不起作用,所以你需要删除事件和事件名称分配给按钮,然后转到设计,并转到按钮事件属性转到onClick事件双击旁边,它会生成事件,它会自动分配事件名称给按钮。这应该工作

s3fp2yjn

s3fp2yjn3#

尝试在你的页面标题中使用AutoEventWireup=“false”。我ASP.net已经有5年没有在www.example.com工作了,但我记得有时候这会破坏一些东西。

woobm2wo

woobm2wo4#

我找不到你的tb_btnAddUserSubmit声明在.cs代码后面,和处理程序。
你需要有一个AddUser.Designer.cs类。一定要检查它。
您还需要在OnInit中注册事件,而不是Page_load

相关问题