0

我制作了一个登录页面来验证用户。我有一个如下的 sql 表:

表:tblUsers

userID (int is identity)
username (nvarchr (50))
password (nvarchr (50))

登录.ASPX

    Username<asp:TextBox ID="txtUsername" runat="server"></asp:TextBox><br />
    Password <asp:TextBox ID="txtPassword" runat="server"></asp:TextBox><br />
    <asp:Button ID="cmdLogin" runat="server" Text="Login" 
        onclick="cmdLogin_Click" /><br />
    <asp:Label ID="lblError" runat="server" EnableViewState="False"></asp:Label><br />
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:ConStr %>" 
        SelectCommand="SELECT * FROM [tblUser] WHERE (([username] = @username) AND ([password] = @password))">
        <SelectParameters>
            <asp:ControlParameter ControlID="txtPassword" Name="username" 
                PropertyName="Text" Type="String" />
            <asp:ControlParameter ControlID="txtPassword" Name="password" 
                PropertyName="Text" Type="String" />
        </SelectParameters>
    </asp:SqlDataSource>

验证用户在 SqlDataSource 的 SelectCommand 中。

SELECT * FROM [tblUser] WHERE (([username] = @username) AND ([password] = @password))

如何通过 SqlDataSource 验证用户?我想要登录按钮的代码。

4

1 回答 1

1

尝试这个:

protected void cmdLogin_Click(object sender, EventArgs e)
{
    DataView dView = (DataView)SqlDataSource1.Select(DataSourceSelectArguments.Empty);
    if (dView.Count == 1)
        Response.Redirect("~/Default.aspx");
    else
        lblError.Text="Incorrect username or password!";
}
于 2013-09-18T14:30:26.017 回答