?? adminteacherdetails.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;
public partial class adminTeacherDetails : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GridViewBind();
}
}
//GridView綁定數據方法
private void GridViewBind()
{
string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
string SqlStr = "SELECT Teacher.*,Depart.* FROM Teacher ,Depart where Teacher.teaDepart=Depart.departID order by Teacher.teaDepart";
DataSet ds = new DataSet();
SqlConnection conn = new SqlConnection(connStr);
try
{
if (conn.State.ToString() == "Closed")
conn.Open();
SqlDataAdapter da = new SqlDataAdapter(SqlStr, conn);
da.Fill(ds);
GridView1.DataSource = ds.Tables[0].DefaultView;
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 ddldepart = (DropDownList)e.Row.FindControl("ddlDepart");
// 生成 DropDownList 的值,綁定數據
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;
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;
//
}
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex; //GridView編輯項索引等于單擊行的索引
GridViewBind();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
GridViewBind();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string teaid = GridView1.DataKeys[e.RowIndex].Values[0].ToString(); //取出編輯行的主鍵值
string teaName = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtName")).Text; //取出修改后的值
int teaDepart = int.Parse(((DropDownList)GridView1.Rows[e.RowIndex].FindControl("ddlDepart")).SelectedValue);
string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
string SqlStr = "update Teacher set teaName='" + teaName + "',teaDepart='" + teaDepart + "' where teaID=" + teaid;
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 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 Teacher where teaID=" + 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();
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -