📄 admincoursedetails.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 _Default : System.Web.UI.Page
{
SqlConnection conn;
protected void Page_Load(object sender, EventArgs e)
{
string connstr=System .Configuration .ConfigurationManager .AppSettings ["ConnectionString"];
conn = new SqlConnection(connstr );
if (Session["userName"] == null)
{
Response.Redirect("Login.aspx");
}
else
{
if (!IsPostBack)
GridViewBind();
}
}
public void Data_Page(object sender, GridViewPageEventArgs e)
{
try
{
GridView1.PageIndex = e.NewPageIndex;
GridViewBind();
}
catch { }
}
private void GridViewBind()
{
// string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
string SqlStr = "SELECT Cource.*,Teacher.*,type.* FROM Cource,Teacher,type where Cource.teaID=Teacher.teaID and Cource.typeID=type.typeID order by Teacher.teaID";
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("ddlTeacher")) != null)
//{
// DropDownList ddlteacher = (DropDownList)e.Row.FindControl("ddlTeacher");
// // 生成 DropDownList 的值,绑定数据
// // string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
// string SqlStr = "SELECT * from Teacher";
// 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();
// ddlteacher.DataSource = ds.Tables[0].DefaultView;
// ddlteacher.DataTextField = "teaName";
// ddlteacher.DataValueField = "teaID";
// ddlteacher.DataBind();
// //
// // 选中 DropDownList
// ddlteacher.SelectedValue = ((HiddenField)e.Row.FindControl("hdfTeacher")).Value;
// ddlteacher.SelectedItem.Text = ((HiddenField)e.Row.FindControl("hdfTeacher")).Value;
// //
//}
//if (((DropDownList)e.Row.FindControl("ddltype")) != null)
//{
// DropDownList ddltype = (DropDownList)e.Row.FindControl("ddltype");
// // 生成 DropDownList 的值,绑定数据
// // string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
// string SqlStr1 = "SELECT * from type";
// DataSet ds1 = new DataSet();
// // SqlConnection conn = new SqlConnection(connStr);
// if (conn.State.ToString() == "Closed") conn.Open();
// SqlDataAdapter da1 = new SqlDataAdapter(SqlStr1, conn);
// da1.Fill(ds1);
// if (conn.State.ToString() == "Open") conn.Close();
// ddltype.DataSource = ds1.Tables[0].DefaultView;
// ddltype.DataTextField = "typeName";
// ddltype.DataValueField = "typeID";
// ddltype.DataBind();
// // 选中 DropDownList
// ddltype.SelectedValue = ((HiddenField)e.Row.FindControl("courcetype")).Value;
// ddltype.SelectedItem.Text = ((HiddenField)e.Row.FindControl("courcetype")).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 courceID = GridView1.DataKeys[e.RowIndex].Values[0].ToString(); //取出编辑行的主键值
// string oldTeaID = GridView1.DataKeys[e.RowIndex].Values[1].ToString();
// string teaID = ((DropDownList)GridView1.Rows[e.RowIndex].FindControl("ddlTeacher")).SelectedValue; //取出修改后的值
// string courceName = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtName")).Text;
// string courceTime = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtTime")).Text;
// string courceAddress = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtAddress")).Text;
// string xueshi = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("xueshi")).Text;
// string credit = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("credit")).Text;
// string courcerenshu = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("courcerenshu")).Text;
// //string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
// string SqlStr = "update Cource set CourceName='" + courceName + "',teaID='" + teaID + "',courceTime='" + courceTime + "',courceAddress='" + courceAddress + "',xueshi='"+xueshi +"',credit='"+credit +"',courcerenshu='"+courcerenshu +"' where courceID='" + courceID + "' and teaID='" + oldTeaID + "'";
// 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 teaID = GridView1.DataKeys[e.RowIndex].Values[1].ToString();
//string connStr = ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString; //取出连接字符串
string SqlStr1 = "delete from Cource where courceID='" + id + "'and teaID='" + teaID + "'";
string SqlStr = "delete from Elect where courceID='" + id + "'and teaID='" + teaID + "'";
try
{
// SqlConnection conn = new SqlConnection(connStr); //创建连接对象
if (conn.State.ToString() == "Closed")
conn.Open();
SqlCommand comm = new SqlCommand(SqlStr, conn);
SqlCommand comm1 = new SqlCommand(SqlStr1,conn );
comm.ExecuteNonQuery(); //执行删除
comm.Dispose();
comm1.ExecuteNonQuery();
comm1.Dispose();
if (conn.State.ToString() == "Open")
conn.Close();
GridView1.EditIndex = -1;
GridViewBind();
}
catch (Exception ex)
{
Response.Write("数据库错误,错误原因:" + ex.Message);
Response.End();
}
}
//添加课程事件
protected void imgBtnAdd_Click(object sender, ImageClickEventArgs e)
{
if (txtCourceID.Text == "")
{
Response.Write("请输入课程号!");
txtCourceID.Focus();
return;
}
string sql = "insert into Cource(courceID,typeID,teaID,courceName,courceTime,courceAddress,xueshi,credit,courcerenshu,departid,courceInfo) values('" + txtCourceID.Text + "','" + ddltypeName.SelectedValue.ToString() + "','" + ddlTeacherName.SelectedValue.ToString() + "','" + txtCourceName.Text + "','" + txtTime.Text + "','" + txtAddress.Text + "','" + xueshi.Text + "','" + credit.Text + "','" + courcerenshu.Text + "','"+ddldepart.SelectedValue.ToString ()+"','" + txtCourceInformation.Text + "')";
SqlCommand insertCmd = new SqlCommand(sql, conn);
try
{
conn.Open();
int flag = insertCmd.ExecuteNonQuery(); //执行添加
if (flag > 0) //如果添加成功
{
lblMessage.Text = "成功添加课程信息!";
}
else //如果添加失败
{
lblMessage.Text = "添加课程信息失败,查看输入是否正确!";
}
}
catch (System.Exception ee)
{
Response.Write("<script language=javascript>alert('" + ee.Message.ToString() + "')</script>");
}
finally
{
conn.Close();
GridViewBind();
}
}
protected void imgBtnReset_Click(object sender, ImageClickEventArgs e)
{
txtCourceID.Text = "";
txtCourceName.Text = "";
txtTime.Text = "";
txtAddress.Text = "";
txtCourceInformation.Text = "";
courcerenshu.Text = "";
credit.Text = "";
xueshi.Text = "";
txtCourceID.Focus();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -