温馨提示:代码在线浏览功能只能做为源码浏览参考,不能展示项目的全部,如果想更进一步了解该代码请下载:Asp.net简单网络选课系统源码
当前文件路径:MyElectCourse/teacherSubmitScore.aspx.cs

1using System; 2
using System.Data; 3
using System.Configuration; 4
using System.Collections; 5
using System.Web; 6
using System.Web.Security; 7
using System.Web.UI; 8
using System.Web.UI.WebControls; 9
using System.Web.UI.WebControls.WebParts; 10
using System.Web.UI.HtmlControls; 11
using System.Data.SqlClient; 12
13
public partial class teacherSubmitScore : System.Web.UI.Page 14
{ 15
protected void Page_Load(object sender, EventArgs e) 16
{ 17
if (!this.IsPostBack) 18
{ 19
imgBtnSubmitScore.Attributes.Add("OnClick", "javascript:return confirm('您只能提交一次成绩,确实要提交吗?')"); //成绩只能提交一次,给出提示 20
BindDDL(); //初始化课程下拉列表框 21
BindGridView(); //根据下拉列表框选择的课程显示选修该门课程的学生 22
} 23
} 24
25
private void BindDDL() 26
{ 27
string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; //取出连接字符串 28
string SqlStr = "SELECT distinct cource.courceID,cource.courceName from Elect,Cource where Elect.courceID=Cource.courceID and Elect.teaID='"+Session["userName"].ToString()+"'"; 29
DataSet ds = new DataSet(); 30
31
SqlConnection conn = new SqlConnection(connStr); //创建连接对象 32
if (conn.State.ToString() == "Closed") //如果连接关闭,打开 33
conn.Open(); 34
SqlDataAdapter da = new SqlDataAdapter(SqlStr, conn); 35
da.Fill(ds); //从数据库中取数据放到DataSet数据集中 36
if (conn.State.ToString() == "Open") 37
conn.Close(); 38
39
ddlCource.DataSource = ds.Tables[0].DefaultView; 40
ddlCource.DataTextField = "courceName"; //下拉列表框每项显示课程名称 41
ddlCource.DataValueField = "courceID"; //下拉列表框每项的值为课程编号 42
ddlCource.DataBind(); 43
} 44
45
protected void kcddl_SelectedIndexChanged(object sender, EventArgs e) 46
{ 47
BindGridView(); 48
} 49
50
//为GridView控件绑定数据 51
private void BindGridView() 52
{ 53
string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; 54
string ValidateStr = "select max(Score) as score from Elect where courceID='" + ddlCource.SelectedValue + "' and teaID='"+Session["userName"].ToString()+"'"; 55
SqlConnection conn = new SqlConnection(connStr); //创建连接对象 56
if (conn.State.ToString() == "Closed") //连接如果关闭,打开 57
conn.Open(); 58
SqlCommand cmd = new SqlCommand(ValidateStr,conn); //创建操作数据库对象 59
SqlDataReader sdr = cmd.ExecuteReader(); //执行查询 60
if (sdr.Read()) 61
{ 62
if ((int.Parse(sdr["score"].ToString())) > 0) //判断成绩是否提交 63
{ 64
GridView1.Visible = false; 65
Response.Write("<script language=javascript>alert('该课程成绩已经提交,不能再次提交!')</script>"); 66
} 67
else 68
{ 69
sdr.Close(); 70
GridView1.Visible = true; 71
string SqlStr = "SELECT student.*,Elect.* FROM Student ,Elect where Student.stuID=Elect.stuID and Elect.courceID='" + ddlCource.SelectedValue + "'order by Student.stuID, Student.stuGrade,Student.stuClass"; 72
DataSet ds = new DataSet(); 73
try 74
{ 75
if (conn.State.ToString() == "Closed") //连接如果关闭,打开 76
conn.Open(); 77
SqlDataAdapter da = new SqlDataAdapter(SqlStr, conn); 78
da.Fill(ds); //向DataSet数据集填充数据 79
80
GridView1.DataSource = ds.Tables[0].DefaultView;//为GridView指名数据源 81
GridView1.DataBind(); //绑定数据 82
} 83
catch (Exception ex) //异常处理 84
{ 85
Response.Write("数据库错误,错误原因:" + ex.Message); 86
Response.End(); 87
} 88
finally 89
{ 90
if (conn.State.ToString() == "Open") //如果连接打开,关闭 91
conn.Close(); 92
} 93
} 94
} 95
else 96
{ 97
if (conn.State.ToString() == "Open") //如果连接打开,关闭 98
conn.Close(); 99
} 100
} 101
102
//提交成绩事件 103
protected void imgBtnSubmitScore_Click(object sender, ImageClickEventArgs e) 104
{ 105
string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; 106
SqlConnection conn = new SqlConnection(connStr); //创建连接对象 107
string strUpdate = ""; 108
int score; 109
string stuID = ""; 110
string courceID = ddlCource.SelectedValue; //课程编号 111
int flag = 0; //标志 112
string teaID = Session["userName"].ToString(); 113
if (conn.State.ToString() == "Closed") //连接如果关闭,打开 114
conn.Open(); 115
int i; 116
for (i = 0; i < GridView1.Rows.Count; i++) 117
{ 118
score = int.Parse(((TextBox)GridView1.Rows[i].FindControl("txtScore")).Text.Trim());//取出学生成绩 119
stuID = GridView1.Rows[i].Cells[0].Text; 120
strUpdate = "update Elect set Score=" + score + " where stuID='" + stuID + "' and courceID='" + courceID + "' and teaID='" + teaID + "'"; 121
try 122
{ 123
SqlCommand cmd = new SqlCommand(strUpdate, conn);//创建Command对象 124
flag = cmd.ExecuteNonQuery(); //执行添加成绩操作 125
} 126
catch (Exception ex) //异常处理 127
{ 128
Response.Write("数据库错误,错误原因:" + ex.Message); 129
Response.End(); 130
} 131
} 132
if (conn.State.ToString() == "Open") //如果连接打开,关闭 133
conn.Close(); 134
if ((flag == 1) && (i == GridView1.Rows.Count)) 135
{ 136
Response.Write("成功录入学生成绩!此后将不能再修改!"); 137
} 138
} 139
}






}