温馨提示:代码在线浏览功能只能做为源码浏览参考,不能展示项目的全部,如果想更进一步了解该代码请下载:Asp.net简单网络选课系统源码
当前文件路径:MyElectCourse/adminStudentDetails.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
//该源码下载自www.51aspx.com(51aspx.com) 13
14
public partial class adminStudentDetails : System.Web.UI.Page 15
{ 16
protected void Page_Load(object sender, EventArgs e) 17
{ 18
if (Session["userName"] == null)//如果没有登录,转向登录界面 19
{ 20
Response.Redirect("Login.aspx"); 21
} 22
else 23
{ 24
if (!IsPostBack) 25
GridViewBind(); //绑定GridView 26
} 27
} 28
29
private void GridViewBind() 30
{ 31
string SqlStr = ""; 32
if (txtstuID.Text.Trim() == "") //根据是否查询来定义sql语句 33
{ 34
SqlStr = "SELECT student.*,Depart.* FROM Student ,Depart where Student.stuDepart=Depart.departID order by Student.stuDepart"; 35
} 36
else 37
{ 38
SqlStr = "SELECT student.*,Depart.* FROM Student ,Depart where Student.stuDepart=Depart.departID and Student.stuID like '%"+txtstuID.Text.Trim()+"%' order by Student.stuDepart"; 39
} 40
string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; //取出连接字符串 41
DataSet ds = new DataSet(); //创建DataSet数据集 42
SqlConnection conn = new SqlConnection(connStr); //创建连接对象 43
try 44
{ 45
if (conn.State.ToString() == "Closed") //如果连接没有打开,打开连接 46
conn.Open(); 47
SqlDataAdapter da = new SqlDataAdapter(SqlStr, conn); 48
da.Fill(ds); //为DataSet数据集填充数据 49
50
GridView1.DataSource = ds.Tables[0].DefaultView; //为GridView指名数据源 51
GridView1.DataBind(); //绑定数据 52
} 53
catch (Exception ex) 54
{ 55
Response.Write("数据库错误,错误原因:" + ex.Message); 56
Response.End(); 57
} 58
finally 59
{ 60
if (conn.State.ToString() == "Open") //操作完,如果连接处于打开,则关闭连接 61
conn.Close(); 62
} 63
} 64
65
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 66
{ 67
if (((DropDownList)e.Row.FindControl("ddlDepart")) != null) 68
{ 69
//生成DropDownList的值,绑定数据 70
DropDownList ddldepart = (DropDownList)e.Row.FindControl("ddlDepart"); 71
string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; 72
string SqlStr = "SELECT * from Depart"; 73
DataSet ds = new DataSet(); 74
75
SqlConnection conn = new SqlConnection(connStr);//创建连接对象 76
if (conn.State.ToString() == "Closed")//如果连接状态处于关闭状态,打开连接 77
conn.Open(); 78
SqlDataAdapter da = new SqlDataAdapter(SqlStr, conn); 79
da.Fill(ds);//将数据填充到数据集中 80
if (conn.State.ToString() == "Open")//如果连接状态处于打开状态,关闭连接 81
conn.Close(); 82
83
ddldepart.DataSource = ds.Tables[0].DefaultView;//为DropDownList设置数据源 84
ddldepart.DataTextField = "departName";//设置显示字段 85
ddldepart.DataValueField = "departID";//设置值字段 86
ddldepart.DataBind();//绑定数据 87
88
//设置DropDownList中选择的项 89
ddldepart.SelectedValue = ((HiddenField)e.Row.FindControl("hdfDepart")).Value; 90
ddldepart.SelectedItem.Text = ((HiddenField)e.Row.FindControl("hdfDepart")).Value; 91
} 92
93
} 94
//GridView控件RowEditing事件 95
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e) 96
{ 97
GridView1.EditIndex = e.NewEditIndex; //GridView编辑项索引等于单击行的索引 98
GridViewBind();//重新为GridView绑定数据 99
100
} 101
//GridView控件RowCancelingEdit事件 102
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) 103
{ 104
GridView1.EditIndex = -1;//GridView编辑项索引为-1,即任何一行都不处于编辑状态 105
GridViewBind();//重新为GridView绑定数据 106
107
} 108
//GridView控件RowUpdating事件 109
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) 110
{ 111
//取出编辑行的主键值和修改后的各字段值 112
string stuid = GridView1.DataKeys[e.RowIndex].Values[0].ToString(); 113
string stuName = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtName")).Text; 114
int stuDepart = int.Parse(((DropDownList)GridView1.Rows[e.RowIndex].FindControl("ddlDepart")).SelectedValue); 115
int stuGrade = int.Parse(((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtGrade")).Text); 116
int stuClass = int.Parse(((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtClass")).Text); 117
118
//根据主键和更新后的值,更新到数据库中 119
string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; 120
string SqlStr = "update Student set stuName='" + stuName + "',stuDepart='" + stuDepart + "',stuGrade='" + stuGrade + "',stuClass='" + stuClass + "' where stuID=" + stuid; 121
try 122
{ 123
SqlConnection conn = new SqlConnection(connStr); //创建连接对象 124
if (conn.State.ToString() == "Closed") //如果连接关闭,打开连接 125
conn.Open(); 126
SqlCommand comm = new SqlCommand(SqlStr, conn); 127
comm.ExecuteNonQuery(); //执行修改 128
comm.Dispose(); 129
if (conn.State.ToString() == "Open") //如果连接打开,关闭连接 130
conn.Close(); 131
GridView1.EditIndex = -1; 132
GridViewBind(); 133
} 134
catch (Exception ex) //异常处理 135
{ 136
Response.Write("数据库错误,错误原因:" + ex.Message); 137
Response.End(); 138
} 139
} 140
//GridView控件RowDeleting事件 141
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e) 142
{ 143
string id = GridView1.DataKeys[e.RowIndex].Values[0].ToString(); //取出要删除记录的主键值 144
string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; //取出连接字符串 145
string SqlStr = "delete from Student where stuID=" + id; 146
try 147
{ 148
SqlConnection conn = new SqlConnection(connStr); //创建连接对象 149
if (conn.State.ToString() == "Closed") 150
conn.Open(); 151
SqlCommand comm = new SqlCommand(SqlStr, conn); 152
comm.ExecuteNonQuery(); //执行删除 153
comm.Dispose(); 154
if (conn.State.ToString() == "Open") 155
conn.Close(); 156
157
GridView1.EditIndex = -1; 158
GridViewBind(); 159
} 160
catch (Exception ex) 161
{ 162
Response.Write("数据库错误,错误原因:" + ex.Message); 163
Response.End(); 164
} 165
} 166
167
protected void imgBtnQuery_Click(object sender, ImageClickEventArgs e) 168
{ 169
GridViewBind(); //根据查询条件,重新对GridView绑定数据 170
} 171
} 172





}
}