实验一
1. 实验要求
基本的网页制作
2. 目的
掌握HTML与ASPX基础,联系制作基本的ASP.NET网页; 使用VS2010提供的导航控件并结合站点地图制作基本的导航栏
3. 工程截图
工程运行效果图:
4. 关键代码
Bighead.aspx关键代码:
<%@ Page Language="C#"
Inherits="htpage._1" %>
<!DOCTYPE html>
AutoEventWireup="true" CodeBehind="1.aspx.cs"
<html xmlns="/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<style type="text/css">
.auto-style1 {
font-size: xx-large;
}
.auto-style2 {
font-size: larger;
}
</style>
</head>
<body style="color: #FF0000">
<form id="form1" runat="server">
<div style="font-size: xx-large; font-style: italic; color: #FF0000; background-color: #66CCFF">
<br />
<span class="auto-style2">今日聚焦</span><br />
<br />
<br />
</div>
<asp:TreeView ID="TreeView1" runat="server" DataSourceID="SiteMapDataSource1"
OnSelectedNodeChanged="TreeView1_SelectedNodeChanged">
</asp:TreeView>
<asp:SiteMapDataSource ID="SiteMapDataSource1" runat="server" /> <br />
<br />
<asp:TreeView ID="TreeView2" runat="server" DataSourceID="SiteMapDataSource2">
</asp:TreeView>
<asp:SiteMapDataSource ID="SiteMapDataSource2" runat="server" /> <br />
<br />
<asp:Image ID="Image1" runat="server" ImageUrl="cat.jpg" Height="760px" Width="1359px" />
<br />
<br />
<span class="auto-style1">当前日期</span><asp:Label ID="timeLabel" runat="server" Text="Label" CssClass="auto-style1"></asp:Label>
</form>
</body>
</html>
Web.sitemap站点地图文件,而定了导航信息:
<?xml version="1.0" encoding="utf-8" ?>
<siteMap xmlns="/AspNet/SiteMap-File-1.0" >
<siteMapNode url="" title="新闻中心" description="">
<siteMapNode url="" title="国内新闻" description="" />
<siteMapNode url="" title="国外新闻" description="" />
<siteMapNode url="" title="娱乐新闻" description="" />
<siteMapNode url="" title="体育新闻" description="" />
<siteMapNode url="" title="军事新闻" description="" />
</siteMapNode>
</siteMap>
实验二
1. 实验要求
实现在线人数统计系统
2. 实验目的
利用ASP.NET内置对象application和session实现在线人数统计系统
3. 工程截图
Web
页面显示如图所示:
工程文件如图:
4. 关键代码
Global.asax文件关键代码
void Application_Start(object sender, EventArgs e)
{
Application.Add("online", 0);
}
void Session_Start(object sender, EventArgs e)
{
Application.Lock(); int iNum = Int32.Parse(Application["online"].ToString()) + 1; Application.Set("online", iNum); Application.UnLock();
}
void Session_End(object sender, EventArgs e)
{
Application.Lock();
int iNum = Int32.Parse(Application["online"].ToString()) - 1; Application.Set("online", iNum);
Application.UnLock();
}
因为要调用Session_Endh函数,所以必须修改config文件
<sessionState mode="InProc" timeout="1" cookieless="false"></sessionState> Default.aspx.cs关键代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.Write("<b>当前在线人数为: " + Application["online"] + "</b>");
Response.AddHeader("Refresh", "10"); //设置10秒刷新一次页面 }
}
实验三
1. 实验要求
利用三层架构对数据库进行操作
2. 实验目的
学会利用ASP.NET的三层架构:表示层,业务逻辑层,数据访问层对数据库进行操作
3. 工程截图
输入界面如图:
输入成功则返回:
没有输入直接提交则返回:
用户名输入错误则返回:
用户名密码输入错误则返回:
工程列表:
4. 关键代码
App_Code文件夹中的四个类文件:
DBHelper.cs提供对数据库操作的支持
using System.Data;
using System.Data.Odbc;
/// <summary>
///DBHelper 的简要说明
/// </summary>
public class DBHelper
{
private String connectionString = "Driver={Microsoft Access Driver (*.mdb)};DBQ=c:\\test.mdb";
public bool ExecuteNonQuery(String sql)
{
try
{
OdbcConnection connection = new OdbcConnection(connectionString); connection.Open();
OdbcCommand command = new OdbcCommand();
command.Connection = connection;
command.CommandText = sql;
command.ExecuteNonQuery();
connection.Close();
}
catch (Exception exp)
{
return false;
}
return true;
}
public DataSet ExecuteReader(String sql)
{
try
{
OdbcConnection connection = new OdbcConnection(connectionString); connection.Open();
DataSet m_set = new DataSet();
OdbcDataAdapter dataAdapter = new OdbcDataAdapter(sql,
connection);
dataAdapter.Fill(m_set, "source");
connection.Close();
return m_set;
}
catch (Exception exp)
{
return null;
}
} } UserService.cs提供对查询user信息的操作函数 using System.Data; public class UserService { public User GetUserByName(String userName) { String sql = "select * from table_user where userName='" + userName + "'"; DBHelper helper = new DBHelper(); DataSet m_set = helper.ExecuteReader(sql); if (m_set.Tables[0].Rows.Count == 0) { return null; } else { User user = new User(); user.UserName = m_set.Tables[0].Rows[0][1].ToString(); user.UserPwd = m_set.Tables[0].Rows[0][2].ToString(); return user; } } } UserManager.cs服务于User类,利用UserService.cs提供的函数进行对user提交的信息进行查找验证操作 public class UserManager
{
public int login(User user)
{
string errStr;
int ret;
if ((user.UserName.Length == 0) || (user.UserPwd.Length == 0)) {
//errStr = "Input is null.";
return 1;
}
UserService service = new UserService();
User temp = service.GetUserByName(user.UserName); if (temp == null)
{
//errStr = "No such username.";
return 2;
}
if (temp.UserName.Equals(user.UserName) && (temp.UserPwd.Equals(user.UserPwd)))
{
//errStr = "Success";
return 0;
}
else
{
//errStr = "Name and pwd are not matched."; return 3;
}
}
}
User.cs为login.cs提供查找验证用户信息的服务
public class User{
private String userName = "";
private String userPwd = "";
public String UserName
{
get
{
return userName;
}
set
{
userName = value;
}
}
public String UserPwd
{
get
{
return userPwd;
}
set
{
userPwd = value;
}
}
}
Login.html
<form action="login.aspx" method="post" name="form1">
UserName:<input type="text" name="userName" size="20" /><br /> Password:<input type="password" name="userPwd" size="20" /><br /> <input type="submit" name="submit" value="登录" />
Login.aspx文件中不添加代码,在login.aspx.cs文件中添加
Login.aspx.cs
public partial class login : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
try
{
String name = Request.Form.Get("userName");
String pwd = Request.Form.Get("userPwd");
//Response.Write(name);
//Response.Write(pwd);
User user = new User();
user.UserName = name;
user.UserPwd = pwd;
UserManager manager = new UserManager();
/*
if (manager.login(user))
{
Response.Write("登陆成功!");
}
else
{
Response.Write("登录失败。");
}
* */
switch(manager.login(user))
{
case 1:
{
Response.Write("登陆名为空"); break;
}
case 2:
{
Response.Write("没有此用户名"); break;
}
case 3:
{
Response.Write("账号密码不匹配"); break;
}
default:
{
Response.Write("登陆成功"); break;
}
}
}
catch (Exception exp)
{
Response.Write(exp.ToString());
}
}
}
封面设计: 贾丽
地 址:中国河北省秦皇岛市河北大街438号 邮 编:066004
电 话:0335-8057068
传 真:0335-8057068
网 址:http://jwc.