温馨提示:代码在线浏览功能只能做为源码浏览参考,不能展示项目的全部,如果想更进一步了解该代码请下载:Asp.net简单网络选课系统源码
当前文件路径:MyElectCourse/adminStudentScore.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 adminStudentScore : System.Web.UI.Page 14
{ 15
protected void Page_Load(object sender, EventArgs e) 16
{ 17
if (!IsPostBack) 18
{ 19
BindDDL(); //初始化课程下拉列表框 20
//GridViewBind(); 21
} 22
} 23
24
private void BindDDL() 25
{ 26
string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; //取出连接字符串 27
string SqlStr = "SELECT distinct courceID,courceName from Cource"; 28
DataSet ds = new DataSet(); 29
30
SqlConnection conn = new SqlConnection(connStr); //创建连接对象 31
if (conn.State.ToString() == "Closed") //如果连接关闭,打开 32
conn.Open(); 33
SqlDataAdapter da = new SqlDataAdapter(SqlStr, conn); 34
da.Fill(ds); //从数据库中取数据放到DataSet数据集中 35
if (conn.State.ToString() == "Open") 36
conn.Close(); 37
38
ddlCource.DataSource = ds.Tables[0].DefaultView; 39
ddlCource.DataTextField = "courceName"; //下拉列表框每项显示课程名称 40
ddlCource.DataValueField = "courceID"; //下拉列表框每项的值为课程编号 41
ddlCource.DataBind(); 42
} 43
44
private void GridViewBind() 45
{ 46
string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; 47
string SqlStr = "SELECT student.*,Elect.* FROM Student ,Elect where Student.stuID=Elect.stuID and Elect.courceID='"+ddlCource.SelectedValue+"' and teaID='"+ddlTeacher.SelectedValue+"' order by Student.stuGrade"; 48
DataSet ds = new DataSet(); 49
SqlConnection conn = new SqlConnection(connStr); 50
try 51
{ 52
if (conn.State.ToString() == "Closed") 53
conn.Open(); 54
SqlDataAdapter da = new SqlDataAdapter(SqlStr, conn); 55
da.Fill(ds); 56
57
GridView1.DataSource = ds.Tables[0].DefaultView; 58
GridView1.DataBind(); 59
} 60
catch (Exception ex) 61
{ 62
Response.Write("数据库错误,错误原因:" + ex.Message); 63
Response.End(); 64
} 65
finally 66
{ 67
if (conn.State.ToString() == "Open") 68
conn.Close(); 69
} 70
} 71
72
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 73
{ 74
if (((DropDownList)e.Row.FindControl("ddlDepart")) != null) 75
{ 76
DropDownList ddldepart = (DropDownList)e.Row.FindControl("ddlDepart"); 77
78
// 生成 DropDownList 的值,绑定数据 79
string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; 80
string SqlStr = "SELECT * from Depart"; 81
DataSet ds = new DataSet(); 82
83
SqlConnection conn = new SqlConnection(connStr); 84
if (conn.State.ToString() == "Closed") conn.Open(); 85
SqlDataAdapter da = new SqlDataAdapter(SqlStr, conn); 86
da.Fill(ds); 87
if (conn.State.ToString() == "Open") conn.Close(); 88
89
ddldepart.DataSource = ds.Tables[0].DefaultView; 90
ddldepart.DataTextField = "departName"; 91
ddldepart.DataValueField = "departID"; 92
ddldepart.DataBind(); 93
// 94
95
// 选中 DropDownList 96
ddldepart.SelectedValue = ((HiddenField)e.Row.FindControl("hdfDepart")).Value; 97
ddldepart.SelectedItem.Text = ((HiddenField)e.Row.FindControl("hdfDepart")).Value; 98
// 99
} 100
101
} 102
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e) 103
{ 104
GridView1.EditIndex = e.NewEditIndex; //GridView编辑项索引等于单击行的索引 105
GridViewBind(); 106
107
} 108
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) 109
{ 110
GridView1.EditIndex = -1; 111
GridViewBind(); 112
113
} 114
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) 115
{ 116
string stuid = GridView1.DataKeys[e.RowIndex].Values[0].ToString(); //取出编辑行的主键值 117
string courceID = ddlCource.SelectedValue; 118
string teaID = ddlTeacher.SelectedValue; 119
int Score = int.Parse(((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtScore")).Text); 120
121
string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; 122
string SqlStr = "update Elect set Score='" + Score + "' where stuID='" + stuid+"' and courceID='"+courceID+"' and teaID='"+teaID+"'"; 123
124
try 125
{ 126
SqlConnection conn = new SqlConnection(connStr); //创建连接对象 127
if (conn.State.ToString() == "Closed") //如果连接关闭,打开连接 128
conn.Open(); 129
SqlCommand comm = new SqlCommand(SqlStr, conn); 130
comm.ExecuteNonQuery(); //执行修改 131
comm.Dispose(); 132
if (conn.State.ToString() == "Open") //如果连接打开,关闭连接 133
conn.Close(); 134
135
GridView1.EditIndex = -1; 136
GridViewBind(); 137
} 138
catch (Exception ex) //异常处理 139
{ 140
Response.Write("数据库错误,错误原因:" + ex.Message); 141
Response.End(); 142
} 143
144
} 145
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e) 146
{ 147
string stuid = GridView1.DataKeys[e.RowIndex].Values[0].ToString(); //取出要删除记录的主键值 148
string courceID = ddlCource.SelectedValue; 149
string teaID = ddlTeacher.SelectedValue; 150
string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; //取出连接字符串 151
string SqlStr = "delete from Elect where stuID='" + stuid + "' and courceID='" + courceID + "' and teaID='" + teaID + "'"; 152
153
try 154
{ 155
SqlConnection conn = new SqlConnection(connStr); //创建连接对象 156
if (conn.State.ToString() == "Closed") 157
conn.Open(); 158
SqlCommand comm = new SqlCommand(SqlStr, conn); 159
comm.ExecuteNonQuery(); //执行删除 160
comm.Dispose(); 161
if (conn.State.ToString() == "Open") 162
conn.Close(); 163
164
GridView1.EditIndex = -1; 165
GridViewBind(); 166
} 167
catch (Exception ex) 168
{ 169
Response.Write("数据库错误,错误原因:" + ex.Message); 170
Response.End(); 171
} 172
173
} 174
protected void ddlTeacher_SelectedIndexChanged(object sender, EventArgs e) 175
{ 176
GridViewBind(); 177
} 178
protected void ddlCource_SelectedIndexChanged(object sender, EventArgs e) 179
{ 180
string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; //取出连接字符串 181
string SqlStr = "SELECT distinct Teacher.teaID,Teacher.teaName from Cource,Teacher where Cource.teaID=Teacher.teaID and courceID='"+ddlCource.SelectedValue+"'"; 182
DataSet ds = new DataSet(); 183
184
SqlConnection conn = new SqlConnection(connStr); //创建连接对象 185
if (conn.State.ToString() == "Closed") //如果连接关闭,打开 186
conn.Open(); 187
SqlDataAdapter da = new SqlDataAdapter(SqlStr, conn); 188
da.Fill(ds); //从数据库中取数据放到DataSet数据集中 189
if (conn.State.ToString() == "Open") 190
conn.Close(); 191
192
ddlTeacher.DataSource = ds.Tables[0].DefaultView; 193
ddlTeacher.DataTextField = "teaName"; //下拉列表框每项显示课程名称 194
ddlTeacher.DataValueField = "teaID"; //下拉列表框每项的值为课程编号 195
ddlTeacher.DataBind(); 196
GridViewBind(); 197
} 198
} 199






}