Skip to main content
 首页 » 编程设计

c# 函数未被调用

2024年02月06日11telwanggs

我有一个未被调用的函数。我尝试使用调试来编写文件,它正确地写入了第一条调试消息 "debug0"。但不是其他消息。

这是aspx:

<%@PageLanguage="C#"AutoEventWireup="true" CodeFile="Login.aspx.cs" Inherits="Login" %> 
 
<!DOCTYPE html PUBLIC > 
 
<html> 
    <head runat="server"> 
        <title/> 
        <link rel="stylesheet" type="text/css" href=".//style.css" /> 
    </head> 
    <body> 
        <form class="container" runat="server"> 
            <asp:login id="Login1" runat="server" style="width: 100%;"> 
                <LayoutTemplate> 
                    <div id="content"> 
                        <h1>blah<br>Site Login </h1> 
                        <asp:TextBox class="field" placeholder="Username" ID="username" runat="server"> </asp:TextBox> 
                        <asp:RequiredFieldValidator ID="UserNameRequired" runat="server" ControlToValidate="UserName" ErrorMessage="User Name is required." ToolTip="User Name is required." ValidationGroup="Login1">* 
                        </asp:RequiredFieldValidator> 
                        <br> 
                        <asp:TextBox class="field" placeholder="Password" ID="password" runat="server" TextMode="Password"/> 
                        <asp:RequiredFieldValidator ID="PasswordRequired" runat="server" ControlToValidate="Password" ErrorMessage="Password is required." ToolTip="Password is required." ValidationGroup="Login1">* 
                        </asp:RequiredFieldValidator> 
                        <br> 
                        <asp:Button class="btn" ID="LoginButton" runat="server" CommandName="Login" Text="Log In" ValidationGroup="Login1"/> 
                        <br> 
                        <asp:Literal ID="FailureText" runat="server" EnableViewState="False"/> 
                    </div> 
                </LayoutTemplate> 
            </asp:login> 
        </form> 
    </body> 
</html> 

和代码背后:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Data.SqlClient; 
using System.Configuration; 
using System.IO; 
using System.Text; 
 
public partial class Login : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 
        File.AppendAllText(@"C:\inetpub\Site\log.txt", "debug0"); 
    } 
 
    protected void Login1_Authenticate(object sender,AuthenticateEventArgs e) 
    { 
        File.AppendAllText(@"C:\inetpub\Site\log.txt", "debug1"); 
        SqlConnection con = new SqlConnection(); 
        try 
        {        
            con.ConnectionString =ConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString; 
            con.Open(); 
 
            string command = "SELECT Username FROM Members WHERE Usernam = @Username AND Password = @Password"; 
 
            SqlCommand comm = new SqlCommand(command, con); 
            comm.Parameters.Add(new SqlParameter("Username", Login1.UserName)); 
            comm.Parameters.Add(new SqlParameter("Password", Login1.Password)); 
 
            SqlDataReader reader = comm.ExecuteReader(); 
            if (reader.Read()) 
            { 
                File.AppendAllText(@"C:\inetpub\Site\log.txt", "debug2"); 
 
                if (reader["Username"].ToString() == "1") 
                { 
                    Response.Redirect("Upload/index.php", false); 
                } 
            } 
        } 
        catch (Exception ex) 
        { 
            File.AppendAllText(@"C:\inetpub\Site\log.txt", "debug3"); 
            Console.WriteLine(ex.ToString()); 
        } 
        finally 
        { 
            if (con != null) 
                con.Close(); 
        } 
    } 
} 

请您参考如下方法:

您似乎错过了在 asp.net 标记中绑定(bind)事件

   <asp:login  
       id="Login1"  
       runat="server"  
       style="width: 100%;" 
       OnAuthenticate="Login1_Authenticate">