⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 courseedit.aspx.cs

📁 学生选课管理系统 谁想要的找我qq564633432
💻 CS
字号:
using System;
using System.Data;
using System.Data.SqlClient;
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;

public partial class courseedit : 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 = "SELECT Course.*,Teacher.* FROM Course,Teacher where Course.Teaid=Teacher.Teaid order by Teacher.Teaid";
        SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["web"]);
        DataSet ds = new DataSet();
        try
        {
            if (conn.State.ToString() == "Closed")
                conn.Open();
            SqlDataAdapter da = new SqlDataAdapter(sqlstr, conn);
            da.Fill(ds);
            GridView1.DataSource = ds.Tables[0].DefaultView;
            GridView1.DataKeyNames = new string[] { "Cno","Teaid" };//为索引设置主键
            GridView1.DataBind();
        }
        catch (Exception ee)
        {
            Response.Write("出错信息" + ee.Message.ToString());
        }
        finally
        {
            conn.Close();
        }
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        GridViewBind();
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string Cno = GridView1.DataKeys[e.RowIndex].Values[0].ToString().Trim();
        string Teaid = GridView1.DataKeys[e.RowIndex].Values[1].ToString().Trim();
        string sqlstr = "delete from Course where Cno='" + Cno + "' and Teaid='"+ Teaid +"'";
        try
        {
            SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["web"]);
            SqlCommand comm = new SqlCommand(sqlstr, conn);
            conn.Open();
            comm.ExecuteNonQuery();
            comm.Dispose();
            GridViewBind();
            conn.Close();
            GridView1.EditIndex = -1;
        }
        catch (Exception ee)
        {
            Response.Write(ee.Message.ToString());
        }

    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        GridViewBind();
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        string Cno = GridView1.DataKeys[e.RowIndex].Values[0].ToString().Trim();
        string oldTeaid = GridView1.DataKeys[e.RowIndex].Values[1].ToString();
        //string stuid = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("Sno")).Text;
        string Teaid = ((DropDownList)GridView1.Rows[e.RowIndex].FindControl("ddlTeacher")).SelectedValue.ToString().Trim(); 
        string Cname = ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[1]).Text.ToString().Trim();
        string Coursetime = ((TextBox)GridView1.Rows[e.RowIndex].Cells[4].Controls[1]).Text.Trim();
        string Caddress = ((TextBox)GridView1.Rows[e.RowIndex].Cells[5].Controls[1]).Text.Trim();
        //string Cinformation = ((TextBox)GridView1.Rows[e.RowIndex].Cells[5].Controls[1]).Text.Trim();

        //取出服务器组件的值
        SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["web"]);
        string sqlstr = "update Course set Teaid='" + Teaid + "',Cname='" + Cname + "',Coursetime='" + Coursetime + "',Caddress='" + Caddress + "' where Cno='" + Cno + "' and Teaid='" + oldTeaid + "'";
        try
        {
            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 ee)
        {
            Response.Write(ee.Message.ToString());
        }
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {

    }
    protected void imgBtnAdd_Click(object sender, ImageClickEventArgs e)
    {
        string Cno = txtCourceID.Text.ToString().Trim();
        string Teaid = ddlTeacherName.SelectedValue.ToString().Trim();
        string Cname = txtCourceName.Text.ToString().Trim();
        string Coursetime = txtTime.Text.ToString().Trim();
        string Caddress = txtAddress.Text.ToString().Trim();
        string Cinformation = txtCourceInformation.Text.ToString().Trim();
        string sqlstr = "insert into Course(Cno, Teaid, Cname, Coursetime, Caddress, Cinformation) values ('"+ Cno + "','"+ Teaid + "','"+ Cname + "','"+ Coursetime +"','"+ Caddress + "', '"+ Cinformation +"')";
        try
        {
            SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["web"]);
            SqlCommand comm = new SqlCommand(sqlstr, conn);
            conn.Open();
            comm.ExecuteNonQuery();
            Response.Write("<script language=javascript>alert('添加成功')</script>");
            comm.Dispose();
            conn.Close();
        }
        catch (Exception ee)
        {
            Response.Write(ee.Message.ToString());
        }
        finally
        {
            GridViewBind();
        }

    }
    protected void imgBtnReset_Click(object sender, ImageClickEventArgs e)
    {

    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -