资源描述
.Net程序设计试验汇报
——ASP.NET
班级: 计10A-1
姓名: 黄溥
学号: 1010104
日期: 5月22日
一、 试验目及要求
1. 熟悉Cookie, Response, Request, Session, Server, Application等对象。
2. 实现用户登录cookie保留。
3. 实现用户登录信息Session保留。
4. 实现用户上传文件(Server对象)。
二、 程序功效
1. 各个网页间切换, 打开, 关闭及退出。
2. 用户登陆判定。
3. 新建数据源, 连接并打开数据库。
4. 三种数据操作使用: GridView, DetailsView, DropDownList。
三、 程序结构
1. Default: 登陆窗口。
2. Default2: 数据操作窗口。
四、 源程序
1. Default源程序:
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class Default : Page
{
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Data Source=RenatoPuPC;Initial Catalog=JiaoWu;Integrated Security=True";
try
{
conn.Open();
string no= TextBox1.Text;
string password = TextBox2.Text;
string str_sql = "select count(*) from Students where Student_No=@Student_No and Student_Password=@Student_Password";
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = str_sql;
cmd.CommandType = CommandType.Text;
cmd.Parameters.Add(new SqlParameter("@Student_No", no));//给sql语句内@Student_No参数赋值, 传输学号no
cmd.Parameters.Add(new SqlParameter("@Student_Password", password));//给sql语句内@Student_Name参数赋值, 传输学号name
int count = Convert.ToInt32(cmd.ExecuteScalar());
if (count > 0)
{
Response.Redirect("Default2.aspx");
}
else
{
Response.Write("用户名密码输入错误");
}
}
finally
{
conn.Close();
}
}
}
2.Default2源程序:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class Default2 : System.Web.UI.Page
{
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
DetailsView1.ChangeMode(DetailsViewMode.Edit);
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
GridView1.SelectedIndex = -1;
GridView1.DataBind();
}
protected void DetailsView1_ItemInserted(object sender, DetailsViewInsertedEventArgs e)
{
GridView1.SelectedIndex = -1;
GridView1.DataBind();
}
protected void DetailsView1_ItemUpdated(object sender, DetailsViewUpdatedEventArgs e)
{
GridView1.SelectedIndex = -1;
GridView1.DataBind();
}
}
五、 数据库
六、 运行截图
七、 试验问题及处理
1. 模板页总是无法显示或显示错误, 经过问询同学和请教老师得出正确模板使用步骤以下:
1) 新建模板页。
2) 在资源管理器中导入“images文件夹”“.html”“.css”文件。
3) 选中.html设计选项卡中表格, 源选项卡中复制已经选中代码上面非选中代码由下到上至div复制到master源选项卡div , 源选项卡中复制已经选中代码下面非选中代码由上到下至\div复制到master源选项卡\div。
4) 然后再在新建页面(选择母版页)上设计除模板以外控件。
u 此次试验直接把.html中样式拷贝到了新建页面上, 模板页没起作用。
2. 相关数据绑定混乱问题: 处理措施是用谁绑谁, 不用不要绑定。
3. 代码正确但因版本问题无法运行: 删除命名空间, 并将后面对应部分改为
public partial class Default : Page。
4. 注意控件一致性: .aspx设计选项卡中控件名=.aspx源选项卡中控件标识名=.aspx.cs中控件名。
5. 注意每个.aspx文件源选项卡中第一行:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Default" %>匹配问题。
6. DropDownList无法回送数据: 将DropDwonList中AutoPostBack选中。
八、 试验总结
经过此次试验, 我熟悉了VS环境下编写ASP.NET程序方法, 结合对数据库操作完成了一个小型教务信息管理系统。
在此次试验中我碰到了很多问题, 有编写错误, 有VS版本问题, 以及部分不很常见问题。经过上网查阅资料, 问询同学及自己思索都得到了处理。另外, 也了解了C#强大功效, 所以期望在未来学习实践中, 我能够在编写C#语言方面有更大进步。
展开阅读全文