?? adminstudentdetails.aspx.cs
字號:
?using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
//該源碼下載自www.51aspx.com(51aspx.com)
public partial class adminStudentDetails : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Session["userName"] == null)//如果沒有登錄,轉向登錄界面
{
Response.Redirect("Login.aspx");
}
else
{
if (!IsPostBack)
GridViewBind(); //綁定GridView
}
}
private void GridViewBind()
{
string SqlStr = "";
if (txtstuID.Text.Trim() == "") //根據是否查詢來定義sql語句
{
SqlStr = "SELECT student.*,Depart.* FROM Student ,Depart where Student.stuDepart=Depart.departID order by Student.stuDepart";
}
else
{
SqlStr = "SELECT student.*,Depart.* FROM Student ,Depart where Student.stuDepart=Depart.departID and Student.stuID like '%"+txtstuID.Text.Trim()+"%' order by Student.stuDepart";
}
string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; //取出連接字符串
DataSet ds = new DataSet(); //創建DataSet數據集
SqlConnection conn = new SqlConnection(connStr); //創建連接對象
try
{
if (conn.State.ToString() == "Closed") //如果連接沒有打開,打開連接
conn.Open();
SqlDataAdapter da = new SqlDataAdapter(SqlStr, conn);
da.Fill(ds); //為DataSet數據集填充數據
GridView1.DataSource = ds.Tables[0].DefaultView; //為GridView指名數據源
GridView1.DataBind(); //綁定數據
}
catch (Exception ex)
{
Response.Write("數據庫錯誤,錯誤原因:" + ex.Message);
Response.End();
}
finally
{
if (conn.State.ToString() == "Open") //操作完,如果連接處于打開,則關閉連接
conn.Close();
}
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (((DropDownList)e.Row.FindControl("ddlDepart")) != null)
{
//生成DropDownList的值,綁定數據
DropDownList ddldepart = (DropDownList)e.Row.FindControl("ddlDepart");
string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
string SqlStr = "SELECT * from Depart";
DataSet ds = new DataSet();
SqlConnection conn = new SqlConnection(connStr);//創建連接對象
if (conn.State.ToString() == "Closed")//如果連接狀態處于關閉狀態,打開連接
conn.Open();
SqlDataAdapter da = new SqlDataAdapter(SqlStr, conn);
da.Fill(ds);//將數據填充到數據集中
if (conn.State.ToString() == "Open")//如果連接狀態處于打開狀態,關閉連接
conn.Close();
ddldepart.DataSource = ds.Tables[0].DefaultView;//為DropDownList設置數據源
ddldepart.DataTextField = "departName";//設置顯示字段
ddldepart.DataValueField = "departID";//設置值字段
ddldepart.DataBind();//綁定數據
//設置DropDownList中選擇的項
ddldepart.SelectedValue = ((HiddenField)e.Row.FindControl("hdfDepart")).Value;
ddldepart.SelectedItem.Text = ((HiddenField)e.Row.FindControl("hdfDepart")).Value;
}
}
//GridView控件RowEditing事件
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex; //GridView編輯項索引等于單擊行的索引
GridViewBind();//重新為GridView綁定數據
}
//GridView控件RowCancelingEdit事件
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;//GridView編輯項索引為-1,即任何一行都不處于編輯狀態
GridViewBind();//重新為GridView綁定數據
}
//GridView控件RowUpdating事件
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
//取出編輯行的主鍵值和修改后的各字段值
string stuid = GridView1.DataKeys[e.RowIndex].Values[0].ToString();
string stuName = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtName")).Text;
int stuDepart = int.Parse(((DropDownList)GridView1.Rows[e.RowIndex].FindControl("ddlDepart")).SelectedValue);
int stuGrade = int.Parse(((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtGrade")).Text);
int stuClass = int.Parse(((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtClass")).Text);
//根據主鍵和更新后的值,更新到數據庫中
string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
string SqlStr = "update Student set stuName='" + stuName + "',stuDepart='" + stuDepart + "',stuGrade='" + stuGrade + "',stuClass='" + stuClass + "' where stuID=" + stuid;
try
{
SqlConnection conn = new SqlConnection(connStr); //創建連接對象
if (conn.State.ToString() == "Closed") //如果連接關閉,打開連接
conn.Open();
SqlCommand comm = new SqlCommand(SqlStr, conn);
comm.ExecuteNonQuery(); //執行修改
comm.Dispose();
if (conn.State.ToString() == "Open") //如果連接打開,關閉連接
conn.Close();
GridView1.EditIndex = -1;
GridViewBind();
}
catch (Exception ex) //異常處理
{
Response.Write("數據庫錯誤,錯誤原因:" + ex.Message);
Response.End();
}
}
//GridView控件RowDeleting事件
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string id = GridView1.DataKeys[e.RowIndex].Values[0].ToString(); //取出要刪除記錄的主鍵值
string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; //取出連接字符串
string SqlStr = "delete from Student where stuID=" + id;
try
{
SqlConnection conn = new SqlConnection(connStr); //創建連接對象
if (conn.State.ToString() == "Closed")
conn.Open();
SqlCommand comm = new SqlCommand(SqlStr, conn);
comm.ExecuteNonQuery(); //執行刪除
comm.Dispose();
if (conn.State.ToString() == "Open")
conn.Close();
GridView1.EditIndex = -1;
GridViewBind();
}
catch (Exception ex)
{
Response.Write("數據庫錯誤,錯誤原因:" + ex.Message);
Response.End();
}
}
protected void imgBtnQuery_Click(object sender, ImageClickEventArgs e)
{
GridViewBind(); //根據查詢條件,重新對GridView綁定數據
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -