网络程序设计
一个简单的登录页面设计,学校的实验
login.aspx文件
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="login.aspx.cs" Inherits="WebApplication1.login" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>登录页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="用户名"></asp:Label>
<asp:TextBox ID="txtUserName" runat="server"></asp:TextBox><br /><br />
<asp:Label ID="Label2" runat="server" Text="密码"></asp:Label>
<asp:TextBox ID="txtPwd" TextMode ="Password" runat="server"></asp:TextBox><br /><br />
<asp:Label ID="Label3" runat="server" Text="请输入用户名密码"></asp:Label><br /><br />
<asp:Button ID="btnLogin" runat="server" OnClick ="click_login" Text="登录" />
<asp:Button ID="btnReset" runat="server" OnClick ="click_clean" Text="重写" />
</div>
</form>
</body>
</html>
login.aspx.cs文件
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.Data;
namespace WebApplication1
{
public partial class login : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void click_login(object sender, EventArgs e)
{
if (txtUserName.Text == "" || txtPwd.Text == "")
{
Label3.Text = "用户名和密码不能为空";
}
else
{
string conStr = "server=LAPTOP-422UBAHH;database=NetSchool;Trusted_Connection=SSPI";
string selUser = "select * from Student where USERID='" + txtUserName.Text + "'";
SqlConnection connection = new SqlConnection(conStr);
connection.Open();
try
{
SqlCommand command = connection.CreateCommand();
command.CommandText = selUser;
SqlDataReader reader = command.ExecuteReader();
if (reader.Read())
{
string strPwd = (string)reader["PASSWORD"];
if (txtPwd.Text == strPwd)
{
Session["userName"] = (string)reader["USERNAME"];
Session["userID"] = txtUserName.Text;
Session["pwd"] = txtPwd.Text;
Server.Transfer("myhome.aspx");
}
else
{
Label3.Text = "密码与用户名不匹配,请重新输入!";
}
}
else
{
Label3.Text = "用户名不存在,请重新输入!";
}
}
catch
{
Label3.Text = "数据库连接失败,抱歉!";
}
}
}
protected void click_clean(object sender, EventArgs e)
{
txtUserName.Text = "";
txtPwd.Text = "";
}
}
}
myhome.aspx文件
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="myhome.aspx.cs" Inherits="WebApplication1.myhome" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>我的空间</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text=""></asp:Label><br />
<asp:Label ID="Label2" runat="server" Text=""></asp:Label><br />
<asp:Label ID="Label3" runat="server" Text=""></asp:Label><br /><br />
<asp:Button ID="bthSearchMyClass" runat="server" OnClick="click_find" Text="查询" />
<br />
<asp:ListBox ID="ListMyClass" runat="server"></asp:ListBox>
<br />
</div>
<p>
<asp:Button ID="Button1" runat="server" OnClick="click_quit" Text="退出" />
</p>
<asp:Label ID="Label4" runat="server" Text="当前课程信息"></asp:Label><br />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="COURSEID" DataSourceID="SqlDataSource4">
<Columns>
<asp:BoundField DataField="COURSEID" HeaderText="课程代码" ReadOnly="True" SortExpression="COURSEID" />
<asp:BoundField DataField="COURSENAME" HeaderText="课程名称" SortExpression="COURSENAME" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource4" runat="server" ConnectionString="<%$ ConnectionStrings:NetSchoolConnectionString %>" SelectCommand="SELECT * FROM [Course]"></asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:NetSchoolConnectionString %>" SelectCommand="SELECT * FROM [Course]"></asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:NetSchoolConnectionString %>" SelectCommand="SELECT * FROM [Student]"></asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NetSchoolConnectionString %>" SelectCommand="SELECT * FROM [SNC]"></asp:SqlDataSource>
</form>
</body>
</html>
myhome.aspx.cs文件
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication1
{
public partial class myhome : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Session["userName"] == null)
{
Server.Transfer("login.aspx");
}
else
{
Label1.Text = "欢迎" + Session["userName"];
Label2.Text = "当前用户名和密码:" + Session["userID"] + " " + Session["pwd"];
Label3.Text = "当前系统时间" + DateTime.Now.ToString();
ListItem li = new ListItem();
li.Text = "我所选修的课程";
li.Value = "value";
ListMyClass.Items.Add(li);
}
}
protected void click_quit(object sender, EventArgs e)
{
Session["userName"] = null;
Session["pwd"] = null;
Server.Transfer("login.aspx");
}
protected void click_find(object sender, EventArgs e)
{
int count = ListMyClass.Items.Count;
int index = 0;
for (int i = 0; i < count; i++)
{
ListItem item = ListMyClass.Items[index];
ListMyClass.Items.Remove(item);
}
index++;
ListMyClass.Items.Add(new ListItem("我所选修的课程", "Value"));
string conStr = "server=LAPTOP-422UBAHH;database=NetSchool;Trusted_Connection=SSPI";
string selCour = "select COURSEID from SNC where USERID='" + Session["userID"] + "'";
SqlConnection connection = new SqlConnection(conStr);
connection.Open();
try
{
SqlCommand command = connection.CreateCommand();
command.CommandText = selCour;
SqlDataReader reader = command.ExecuteReader();
if (!reader.Read())
{
ListMyClass.Items.Add(new ListItem("您当前没有选择课程。", "Value"));
}
else
{
do
{
string courseID = (string)reader["COURSEID"];
//string selCourseName = "select COURSENAME from Course where COURSEID='" + courseID + "'";
ListMyClass.Items.Add(new ListItem(courseID, "Value"));
} while (reader.Read());
}
}
catch
{
ListMyClass.Items.Add(new ListItem("数据库连接失败,抱歉!", "Value"));
}
connection.Close();
}
}
}