将登录页面与数据库连接失败



我正试图将我的数据库与login.aspx文件连接,但我一直收到一个错误:

SqlDataSource1类型"系统"。网状物UI。WebControls。SqlDataSource"没有名为"的公共属性OnClick">

我的login.aspx文件代码的主体代码是:

<div id="pageBody">
<form id="loginForm" runat="server">
<div class="imgcontainer">
<img src="/packages/img_avatar2.png" alt="Avatar" class="avatar">
</div>
<div class="container">

<asp:textbox id="usernameLoginTextbox" placeholder="Enter username" runat="server"/>
<asp:Label ID="Label1" Visible="false" CssClass="error px-5 py-1" runat="server" Text=""></asp:Label>

<asp:textbox type="password" id="passwordLoginTextbox" placeholder="Enter password" runat="server"/>
<asp:Label ID="Label2" Visible="false" CssClass="error px-5 py-1" runat="server" Text=""></asp:Label>

<asp:Button runat="server" id="loginButton" Text="Log Me In Amigo" OnClick="loginButton_Click" CssClass="cancelbtn"/>
<asp:Label ID="success" CssClass="bg-success px-5 py-2" ForeColor="White" Visible="false" runat="server" Text="Label"></asp:Label>
</div>
</form>
<asp:SqlDataSource ID="SqlDataSource1" OnClick ="loginButton_Click" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT * FROM [Table]"></asp:SqlDataSource>
</div>

数据库查询代码为:

CREATE TABLE [dbo].[Table]
(
[Id] INT NOT NULL PRIMARY KEY, 
[username] NCHAR(50) NULL, 
[password] NCHAR(15) NULL
)

最后,我的登录按钮点击代码是:

protected void loginButton_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
con.Open();
SqlCommand cmd = new SqlCommand("select * from login where uname=@username and passwd=@password", con);
cmd.Parameters.AddWithValue("@username", usernameLoginTextbox.Text);
cmd.Parameters.AddWithValue("@password", passwordLoginTextbox.Text);
SqlDataReader sdr = cmd.ExecuteReader();
if (sdr.Read())
{
Label1.Visible = false;
Label2.Visible = false;
success.Visible = true;
success.Text = "Login successful";
}
else
{
Label1.Visible = true;
Label2.Visible = true;
Label1.Text = "Username incorrect";
Label2.Text = "password incorrect";
}
con.Close();
}

有人知道这个问题的解决方案吗?或者我是否可以用其他东西代替OnClick?

提前谢谢。

<asp:SqlDataSource标记没有定义OnClick属性,您需要删除";OnClick=";登录按钮点击";来自asp:SqlDataSource标记。看起来像是意外的复制粘贴错误,您在<asp:按钮。

最新更新