您的位置:首页 > 编程语言 > ASP

Asp.Net 简单考题管理系统

2010-06-01 21:57 537 查看






default.aspx.cs

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;

using System.Data.SqlClient;
using System.Data.OleDb;
using System.IO;

namespace Web0531
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
db.dataBind(ListView1, "select * from exam order by id");
try
{
int PageSize = (int)Session["PageSize"];                // 保证从修改页面能返回到原来的页面
int StartRowIndex = (int)Session["StartRowIndex"];
((DataPager)ListView1.FindControl("DataPager1")).SetPageProperties(StartRowIndex, PageSize, true);
}
catch { }
}
}

// 数据项绑定后,将正确答案的选项“蓝色、加粗”显示
protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
{
try
{
Label lb = (Label)e.Item.FindControl("answerLabel");        // 取答案
string ans = lb.Text.Trim() + "Label";                      // 如果答案是a;b;c;d其中之一
Label lbans = (Label)e.Item.FindControl(ans);               // 没有答案时会出错
lbans.CssClass = "bb";                                      // 设置答案选项的样式
}
catch { }
}

// 翻页时执行
protected void ListView1_PagePropertiesChanged(object sender, EventArgs e)
{
int PageSize = ((DataPager)ListView1.FindControl("DataPager1")).PageSize;               // 每页记录数
int StartRowIndex = ((DataPager)ListView1.FindControl("DataPager1")).StartRowIndex;     // 首行的索引值
int TotalRowCount = ((DataPager)ListView1.FindControl("DataPager1")).TotalRowCount;     // 总记录数

//Response.Write("PageSize = " + PageSize.ToString() + " ; StartRowIndex = " + StartRowIndex.ToString() + " ; TotalRowcount = " + TotalRowCount.ToString() + ";");
((DataPager)ListView1.FindControl("DataPager1")).SetPageProperties(StartRowIndex, PageSize, false);
db.dataBind(ListView1, "select * from exam order by id");       // 重新绑定数据
Session["StartRowIndex"] = StartRowIndex;                       // 记录信息到 Session,用于从修改页面返回时能回到原来的页面
Session["PageSize"] = PageSize;
}

// 删除记录
protected void ListView1_ItemDeleting(object sender, ListViewDeleteEventArgs e)
{
string id = ((Label)ListView1.Items[e.ItemIndex].FindControl("idLabel")).Text;          // 要删记录的 id 号
OleDbConnection cn = db.AccessConnection();
cn.Open();
OleDbCommand cmd = new OleDbCommand("delete from exam where id=" + id, cn);
cmd.ExecuteNonQuery();
cn.Close();
cmd.Dispose();

int PageSize = ((DataPager)ListView1.FindControl("DataPager1")).PageSize;               // 若删除的是最后页面中的记录,可能需要调整
int StartRowIndex = ((DataPager)ListView1.FindControl("DataPager1")).StartRowIndex;
int TotalRowCount = ((DataPager)ListView1.FindControl("DataPager1")).TotalRowCount;
if (TotalRowCount < 3)                                                                  // 若记录要删除完,回复原来的所有记录
{
File.Copy(Server.MapPath("App_Data/peixun.bak"), Server.MapPath("App_Data/peixun.mdb"), true);
}
else
{
if (StartRowIndex == TotalRowCount - 1)                                             // 若最后页的记录删完
{
StartRowIndex -= PageSize;                                                      // 要切回到原最后页的前一页,DataPager本身不会处理
((DataPager)ListView1.FindControl("DataPager1")).SetPageProperties(StartRowIndex, PageSize, false);
Session["StartRowIndex"] = StartRowIndex;
Session["PageSize"] = PageSize;
}
}
db.dataBind(ListView1, "select * from exam order by id");                               // 重新绑定数据
}
}
}


modify.aspx.cs

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;

using System.Data.OleDb;

namespace Web0531
{
public partial class modify : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string urlId = Request.QueryString["id"];           // 要修改记录的 id 号
//if (string.IsNullOrEmpty(urlId)) urlId = "4";     // 测试用
OleDbConnection conn = db.AccessConnection();
conn.Open();
OleDbCommand cmd = new OleDbCommand("select * from exam where id=" + urlId, conn);
OleDbDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
if (dr.Read())
{
id.Value = dr["id"].ToString();                 // 把原来的题目信息显示出来
tm.Text = dr["question"].ToString();
a.Text = dr["a"].ToString();
b.Text = dr["b"].ToString();
c.Text = dr["c"].ToString();
d.Text = dr["d"].ToString();
int i = dr["answer"].ToString()[0] - 'A';       // 仅处理单选
if(i>=0 && i<=3) da.Items[i].Selected = true;
}
conn.Close();
dr.Close();
}
}

// 更新记录
protected void Button1_Click(object sender, EventArgs e)
{
string rda = null;
for (int i = 0; i < da.Items.Count; ++i)
{
if (da.Items[i].Selected)
{
rda = Convert.ToString((char)('A' + i));        // 答案,仅处理单选
break;
}
}
if (string.IsNullOrEmpty(rda)) rda = " ";
string sql = "update exam set question='" + tm.Text.Replace("'", "''") + "',a='" + a.Text.Replace("'", "''") + "',b='" + b.Text.Replace("'", "''") + "',c='" + c.Text.Replace("'", "''") + "',d='" + d.Text.Replace("'", "''") + "',answer='" + rda + "' where id=" + id.Value;

OleDbConnection conn = db.AccessConnection();
conn.Open();
OleDbCommand cmd = new OleDbCommand(sql, conn);
cmd.ExecuteNonQuery();                                  // 执行更新
conn.Close();
Response.Redirect("default.aspx");                      // 返回到原来的页面
}
}
}


new.aspx.cs

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;

using System.Data.OleDb;

namespace Web0531
{
public partial class _new : System.Web.UI.Page                      // 插入新的考试题目
{
// 插入新记录
protected void Button1_Click(object sender, EventArgs e)
{
string rda = null;
for (int i = 0; i < da.Items.Count; ++i)                    // 选择的答案
{
if (da.Items[i].Selected)
{
rda = Convert.ToString((char)('A' + i));            // 转换成字符串
break;                                              // 仅考虑单选
}
}
if (string.IsNullOrEmpty(rda)) rda = " ";
string sql = "insert into exam(question,a,b,c,d,answer) values ('" + tm.Text.Replace("'", "''") + "','" + a.Text.Replace("'", "''") + "','" + b.Text.Replace("'", "''") + "','" + c.Text.Replace("'", "''") + "','" + d.Text.Replace("'", "''") + "','" + rda + "')";

OleDbConnection conn = db.AccessConnection();
conn.Open();
OleDbCommand cmd = new OleDbCommand(sql, conn);             // 插入到数据库
cmd.ExecuteNonQuery();
conn.Close();
Response.Redirect("default.aspx");                          // 返回到原来所在的页面,要看刚插入的题目,可单击“最后一页”按钮
}
}
}


完整项目下载:

http://download.csdn.net/source/2417259
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐