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

📄 coursemanager.aspx.cs

📁 guan yu pai ke xi tong de ruan jian
💻 CS
字号:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
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 Y2T03.CourseScheduler.CourseBLL;
using Y2T03.CourseScheduler.CourseModel;

public partial class CourseManager : System.Web.UI.Page
{
    /// <summary>
    /// 页面加载事件
    /// </summary>
    protected void Page_Load(object sender, EventArgs e)
    {
        //
        // 判断是否是首次加载
        //
        if (!Page.IsPostBack)
        {
            LoadTreeProduct();  // 获取 TreeView 中的所有节点
        }
    }

    /// <summary>
    /// 向 TreeView 中加入产品类型节点
    /// </summary>
    protected void LoadTreeProduct()
    {
        try
        {
            this.trvCourse.Nodes.Clear();   // 清空 TreeView 中的所有节点

            IList<Product> list = CourseManagerBLL.GetProducts();

            foreach (Product product in list)
            {
                TreeNode node = new TreeNode();
                node.Text = product.Title;  // 设置该节点的显示标题
                node.Value = string.Empty;  // 非课程

                LoadChildNodeSection(product.ProductId, node);

                this.trvCourse.Nodes.Add(node); // 添加到 TreeView 的节点中
            }

            this.trvCourse.ExpandAll(); // 展开所有节点
        }
        catch (Exception ex)
        {
            string err = ex.Message;
        }
    }

    /// <summary>
    /// 向产品节点中加入学习阶段节点
    /// </summary>
    /// <param name="productId">产品版本</param>
    /// <param name="parentNode">产品类型节点</param>
    protected void LoadChildNodeSection(int productId, TreeNode parentNode)
    {
        try
        {
            IList<Section> list = CourseManagerBLL.GetSections();

            foreach (Section section in list)
            {
                TreeNode node = new TreeNode();
                node.Text = section.Title;  // 设置该节点的显示标题
                node.Value = string.Empty;  // 非课程

                LoadChildNodeSection(productId, section.SectionCode, node);

                parentNode.ChildNodes.Add(node);    // 添加到产品类型节点下
            }
        }
        catch (Exception ex)
        {
            string err = ex.Message;
        }
    }

    /// <summary>
    /// 向学习阶段节点中加入课程节点
    /// </summary>
    /// <param name="productId">产品版本</param>
    /// <param name="sectionCode">学习阶段</param>
    /// <param name="parentNode">学习阶段节点</param>
    protected void LoadChildNodeSection(int productId, string sectionCode, TreeNode parentNode)
    {
        try
        {
            IList<Course> list = CourseManagerBLL.GetCourses(productId, sectionCode);

            foreach (Course course in list)
            {
                TreeNode node = new TreeNode();
                node.Text = course.Title;   // 设置该节点的显示标题
                node.Value = course.CourseId.ToString();    //  设置该节点的值为课程编号

                parentNode.ChildNodes.Add(node);    // 添加到学习阶段节点下
            }
        }
        catch (Exception ex)
        {
            string err = ex.Message;
        }
    }

    /// <summary>
    /// 单击 DetailsView 控件中的“删除”按钮时,但在删除操作之前发生
    /// 目的:拦截默认的删除方法,改为调用自定义的删除方法
    /// </summary>
    protected void dtvCourse_ItemDeleting(object sender, DetailsViewDeleteEventArgs e)
    {
        try
        {
            int courseId = Int32.Parse(e.Keys["CourseId"].ToString());  // 获得 DetailsView 的 DataKeyNames 键值,该属性中保存有实体对象的主键

            int nodeOne = this.trvCourse.Nodes[0].ChildNodes.Count;
            int nodeTwo = this.trvCourse.Nodes[1].ChildNodes.Count;

            if ((nodeOne == 4 && nodeTwo == 3) || (nodeOne == 3 && nodeTwo == 4))
            {
                PopupMessage("节点不能全部删除,若想删除,请添加节点后再进行删除!!!");  // 弹出 "删除成功" 的消息提示框
            }
            else
            {
                CourseManagerBLL.Delete(courseId);  // 调用自定义的删除方法

                this.LoadTreeProduct(); // 重新加载课程目录树

                PopupMessage("课程已终止!!!");  //弹出 "课程已终止" 的消息提示框
            }
            this.dtvCourse.ChangeMode(DetailsViewMode.ReadOnly);
        }
        catch (Exception ex)
        {
            string err = ex.Message;
        }
        finally
        {
            e.Cancel = true;    // 取消删除事件,不执行“删除”按钮绑定的方法
        }
    }

    /// <summary>
    /// 弹出操作结果
    /// </summary>
    /// <param name="message">弹出对话框信息内容</param>
    protected void PopupMessage(string message)
    {
        try
        {
            ClientScriptManager csm = this.Page.ClientScript;   // 获取管理客户端脚本的方法
            Type type = this.GetType();     // 获取类型
            string csKey = "PopupMessage";  // 设置消息

            //
            // 判断是否已经注册了启动脚本
            //
            if (!csm.IsStartupScriptRegistered(type, csKey))
            {
                string script = string.Format("alert('{0}');", message);    //格式化输出弹出消息的内容
                csm.RegisterStartupScript(type, csKey, script, true);   //注册启动脚本
            }
        }
        catch (Exception ex)
        {
            string err = ex.Message;
        }
    }

    /// <summary>
    /// 单击 DetailsView 控件中的“添加”按钮时,但在添加操作之前发生
    /// 目的:拦截默认的插入方法,改为调用自定义的插入方法
    /// </summary>
    protected void dtvCourse_ItemInserting(object sender, DetailsViewInsertEventArgs e)
    {
        try
        {
            Course course = new Course();

            //
            // 找到dtvCourse中的行中控件的值
            //
            TextBox title = this.dtvCourse.FindControl("txtTitle") as TextBox;
            TextBox courseCode = this.dtvCourse.FindControl("txtCourseCode") as TextBox;
            DropDownList product = this.dtvCourse.FindControl("ddlGetProducts") as DropDownList;
            DropDownList section = this.dtvCourse.FindControl("ddlGetSections") as DropDownList;
            RadioButtonList speciality = this.dtvCourse.FindControl("rblSpecialities") as RadioButtonList;

            course.Title = title.Text;
            course.CourseCode = courseCode.Text;
            course.Product.ProductId = int.Parse(product.SelectedValue.ToString());
            course.Section.SectionCode = section.SelectedValue.ToString();
            course.Speciality.SpecialityId = int.Parse(speciality.SelectedValue.ToString());

            bool flg = CourseManagerBLL.Create(course);  // 调用自定义的插入方法

            if (flg == true)
            {

                this.LoadTreeProduct();  // 重新加载课程目录树

                PopupMessage("添加成功!!!");  // 弹出 "添加成功" 的消息提示框
            }
            else
            {
                PopupMessage("长度超过限制,课程名称最长 50 位,课程代码最长 10 位, 或者课程名称已经存在!!!");
            }
            this.dtvCourse.ChangeMode(DetailsViewMode.ReadOnly);
        }
        catch (Exception ex)
        {
            string err = ex.Message;
        }
        finally
        {
            e.Cancel = true;    // 取消插入事件,不执行“插入”按钮绑定的方法
        }
    }

    protected void dtvCourse_DataBound(object sender, EventArgs e)
    {
        //
        // 可在这设置绑定 RadioButtonList 或者 DropDownList 的 SelectValued 值
        //
    }

    /// <summary>
    /// 单击 DetailsView 控件中的“更新”按钮时,但在更新操作之前发生
    /// 目的:拦截默认的更新方法,改为调用自定义的更新方法
    /// </summary>
    protected void dtvCourse_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
    {
        try
        {
            Course course = new Course();

            //
            // 找到detailview中的行中控件的值
            //
            Label courseId = this.dtvCourse.FindControl("lblCourseId") as Label;
            TextBox title = this.dtvCourse.FindControl("txtTitle") as TextBox;
            TextBox courseCode = this.dtvCourse.FindControl("txtCourseCode") as TextBox;
            DropDownList product = this.dtvCourse.FindControl("ddlGetProducts") as DropDownList;
            DropDownList section = this.dtvCourse.FindControl("ddlGetSections") as DropDownList;
            RadioButtonList speciality = this.dtvCourse.FindControl("rblSpecialities") as RadioButtonList;

            course.CourseId = int.Parse(courseId.Text);
            course.Title = title.Text;
            course.CourseCode = courseCode.Text;
            course.Product.ProductId = int.Parse(product.SelectedValue.ToString());
            course.Section.SectionCode = section.SelectedValue.ToString();
            course.Speciality.SpecialityId = int.Parse(speciality.SelectedValue.ToString());

            bool flg = CourseManagerBLL.Update(course);  // 调用自定义的更新方法

            if (flg == true)
            {
                this.LoadTreeProduct();  // 重新加载课程目录树

                PopupMessage("修改成功!!!");  // 弹出 "修改成功" 的消息提示框
            }
            else
            {
                PopupMessage("长度超过限制,课程名称最长 50 位,课程代码最长 10 位!!!");
            }
            this.dtvCourse.ChangeMode(DetailsViewMode.ReadOnly);
        }
        catch (Exception ex)
        {
            string err = ex.Message;
        }
        finally
        {
            e.Cancel = true;    // 取消更新事件,不执行“更新”按钮绑定的方法
        }
    }
}

⌨️ 快捷键说明

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