我正在开发一个asp.net Web应用程序,我正在尝试构建一个具有login.aspx和login.aspx.cs的简单登录页面。
aspx页面
<%@ Page Title="Log In" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeBehind="Login.aspx.cs" Inherits="nikeeWebApplication.Account.Login" culture="auto" meta:resourcekey="PageResource1" uiculture="auto" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<asp:Login ID="LoginUser" runat="server" EnableViewState="False"
RenderOuterTable="False" meta:resourcekey="LoginUserResource1">
<LayoutTemplate>
<span class="failureNotification">
<asp:Literal ID="FailureText" runat="server"
meta:resourcekey="FailureTextResource1"></asp:Literal>
</span>
<asp:ValidationSummary ID="LoginUserValidationSummary" runat="server" CssClass="failureNotification"
ValidationGroup="LoginUserValidationGroup"
meta:resourcekey="LoginUserValidationSummaryResource1"/>
<div class="accountInfo">
<fieldset class="login">
<legend>LOG IN</legend>
<p>
<asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName"
meta:resourcekey="UserNameLabelResource1">Username:</asp:Label>
<asp:TextBox ID="UserName" runat="server" CssClass="textEntry"
meta:resourcekey="UserNameResource1"></asp:TextBox>
<asp:RequiredFieldValidator ID="UserNameRequired" runat="server" ControlToValidate="UserName"
CssClass="failureNotification" ErrorMessage="User Name is required." ToolTip="User Name is required."
ValidationGroup="LoginUserValidationGroup"
meta:resourcekey="UserNameRequiredResource1">*</asp:RequiredFieldValidator>
</p>
<p>
<asp:Label ID="PasswordLabel" runat="server" AssociatedControlID="Password"
meta:resourcekey="PasswordLabelResource1">Password:</asp:Label>
<asp:TextBox ID="Password" runat="server" CssClass="passwordEntry"
TextMode="Password" meta:resourcekey="PasswordResource1"></asp:TextBox>
<asp:RequiredFieldValidator ID="PasswordRequired" runat="server" ControlToValidate="Password"
CssClass="failureNotification" ErrorMessage="Password is required." ToolTip="Password is required."
ValidationGroup="LoginUserValidationGroup"
meta:resourcekey="PasswordRequiredResource1">*</asp:RequiredFieldValidator>
</p>
</fieldset>
<p class="submitButton">
<asp:Button ID="LoginButton" runat="server" CommandName="Login" Text="Log In"
ValidationGroup="LoginUserValidationGroup"
meta:resourcekey="LoginButtonResource1"/>
</p>
</div>
</LayoutTemplate>
</asp:Login>
</asp:Content>
cs page
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Configuration;
using System.Data.SqlClient;
namespace nikeeWebApplication.Account
{
public partial class Login : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString);
protected void Page_Load(object sender, EventArgs e)
{
}
protected void LoginButton_Click(object sender, EventArgs e)
{
con.Open();
SqlCommand cmd = new SqlCommand("select * from UserMaster where UserName =@username and Password=@password", con);
cmd.Parameters.AddWithValue("@username", UserName.Text);
cmd.Parameters.AddWithValue("@password", Password.Text);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
Response.Redirect("Details.aspx");
}
else
{
ClientScript.RegisterStartupScript(Page.GetType(), "validation", "<script language='javascript'>alert('Invalid Username and Password')</script>");
}
}
}
}
但是当我从登录页面访问文本框时,它会给出错误。
感谢您的帮助
答案 0 :(得分:0)
您需要访问以下文本框:
TextBox Password = LoginUser.FindControl("Password") as TextBox;
TextBox UserName = LoginUser.FindControl("UserName") as TextBox;
cmd.Parameters.AddWithValue("@username", UserName.Text);
cmd.Parameters.AddWithValue("@password", Password.Text);
或者您可以使用:
cmd.Parameters.AddWithValue("@username", LoginUser.UserName);
cmd.Parameters.AddWithValue("@password", LoginUser.Password);
再次,为了摆脱DataTable错误,添加using语句:
using System.Data;