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;
using System.Web.Configuration;

public partial class Admin_CollegeManage : System.Web.UI.Page
{
    private DataBaseConnection DBConn;
    private string collegeName;

    private string oldCollegeName;

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void SqlDataSource1_Inserting(object sender, SqlDataSourceCommandEventArgs e)
    {
        e.Command.Parameters["@college_name"].Value = this.TextBox1.Text;
    }

    protected void LinkButton3_Click(object sender, EventArgs e)
    {
        collegeName = this.TextBox1.Text;

        string connStr = WebConfigurationManager.ConnectionStrings["DesertEagleConnectionString"].ToString();
        this.DBConn = new DataBaseConnection();
        this.DBConn.Open(connStr);

        if (this.DBConn.CheckData("DE_college", "college_name", collegeName))
        {
            Response.Write("<script>alert('此院系已存在')</script>");
        }
        else
        {
            this.SqlDataSource1.Insert();
        } // End of if

        this.TextBox1.Text = "";
        this.DBConn.Close();
    }
    protected void TextBox1_TextChanged(object sender, EventArgs e)
    {

    }

    protected void SqlDataSource1_Updating(object sender, SqlDataSourceCommandEventArgs e)
    {
    }
    protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        // 得到被删除院系id
        int index = e.NewSelectedIndex;
        oldCollegeName = ((Label)this.GridView1.Rows[index].Cells[0].FindControl("Label1")).Text;

        string userName = this.User.Identity.Name; //用户名

        // 连接字符串connStr
        string connStr = WebConfigurationManager.ConnectionStrings["DesertEagleConnectionString"].ToString();

        SqlConnection sqlConn = new SqlConnection(connStr);
        if (sqlConn.State == ConnectionState.Closed)
        {
            sqlConn.Open();
        }

        // 更新关联的DE_bloger表。
        string sqlCmdStr = "UPDATE DE_bloger SET college_id = 2 WHERE college_id = " + oldCollegeName;
        SqlCommand sqlCom = new SqlCommand(sqlCmdStr, sqlConn);
        sqlCom.ExecuteNonQuery();

        // 删除DE_college中的旧院系
        sqlCmdStr = "DELETE FROM DE_college WHERE college_id = " + oldCollegeName;
        SqlCommand sqlCom2 = new SqlCommand(sqlCmdStr, sqlConn);
        sqlCom2.ExecuteNonQuery();

        // 重新绑定数据
        this.GridView1.DataBind();
    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        //gridview的更新按钮单击时激发
        for (int i = 0; i < this.GridView1.Controls[0].Controls.Count; i++)
        {
            if (this.GridView1.Controls[0].Controls[i].FindControl("TextBox1") != null)
            {
                collegeName = ((TextBox)(this.GridView1.Controls[0].Controls[i].FindControl("TextBox1"))).Text;
                break;
            }
        }
        string connStr = WebConfigurationManager.ConnectionStrings["DesertEagleConnectionString"].ToString();
        this.DBConn = new DataBaseConnection();
        this.DBConn.Open(connStr);

        if (this.DBConn.CheckData("DE_college", "college_name", collegeName))
        {
            Response.Write("<script>alert('此院系已存在')</script>");
            for (int i = 0; i < this.GridView1.Controls[0].Controls.Count; i++)
            {
                if (this.GridView1.Controls[0].Controls[i].FindControl("TextBox1") != null)
                {
                    ((TextBox)(this.GridView1.Controls[0].Controls[i].FindControl("TextBox1"))).Text = oldCollegeName;
                    break;
                }
            }
        }

        else
        {
            ;
        } // End of if
        this.DBConn.Close();
    }
    protected void LinkButton1_Load(object sender, EventArgs e)
    {
        for (int i = 0; i < this.GridView1.Controls[0].Controls.Count; i++)
        {
            if (this.GridView1.Controls[0].Controls[i].FindControl("Label1") != null)
            {
                this.oldCollegeName = ((Label)(this.GridView1.Controls[0].Controls[i].FindControl("Label1"))).Text;
                break;
            }
        }
    }
}
