📄 classroommanager.aspx.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 ClassRoomManager : System.Web.UI.Page
{
/// <summary>
/// 页面加载事件
/// </summary>
protected void Page_Load(object sender, EventArgs e)
{
//
// 判断是否是首次加载
//
if (!Page.IsPostBack)
{
LoadTreeClassRoom(); // 获取 TreeView 中的所有节点
}
}
/// <summary>
/// 向 TreeView 中加入设施类型节点
/// </summary>
protected void LoadTreeClassRoom()
{
try
{
this.trvClassRoom.Nodes.Clear(); // 清空 TreeView 中的所有节点
IList<RoomType> list = ClassRoomManagerBLL.GetRoomTypes();
foreach (RoomType roomType in list)
{
TreeNode node = new TreeNode();
node.Text = roomType.Title; // 设置该节点的显示标题
node.Value = string.Empty; // 非设施
LoadChildNodeClassRoom(roomType.TypeId, node);
this.trvClassRoom.Nodes.Add(node); // 添加到 TreeView 的节点中
}
this.trvClassRoom.ExpandAll(); // 展开所有节点
}
catch (Exception ex)
{
string err = ex.Message;
}
}
/// <summary>
/// 向设施类型节点中加入教学设施节点
/// </summary>
/// <param name="typeId">设施类型</param>
/// <param name="parentNode">设施类型节点</param>
protected void LoadChildNodeClassRoom(int typeId, TreeNode parentNode)
{
try
{
IList<ClassRoom> list = ClassRoomManagerBLL.GetClassRooms(typeId);
foreach (ClassRoom classRoom in list)
{
TreeNode node = new TreeNode();
node.Text = classRoom.Title; // 设置该节点的显示标题
node.Value = classRoom.RoomId.ToString(); // 设置该节点的值为教学设施编号
parentNode.ChildNodes.Add(node); // 添加到教学设施节点下
}
}
catch (Exception ex)
{
string err = ex.Message;
}
}
/// <summary>
/// 单击 DetailsView 控件中的“修改”按钮时,但在修改操作之前发生
/// 目的:拦截默认的更新方法,改为调用自定义的更新方法
/// </summary>
protected void dtvClassRoom_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
{
try
{
ClassRoom classRoom = new ClassRoom();
// 找到dtvClassRoom中的行中控件的值
Label roomId = this.dtvClassRoom.FindControl("lblRoomId") as Label;
TextBox title = this.dtvClassRoom.FindControl("txtTitle") as TextBox;
RadioButtonList roomType = this.dtvClassRoom.FindControl("rbtGetRoomTypes") as RadioButtonList;
DropDownList product = this.dtvClassRoom.FindControl("ddlGetProducts") as DropDownList;
classRoom.RoomId = int.Parse(roomId.Text);
classRoom.Title = title.Text;
classRoom.RoomType.TypeId = int.Parse(roomType.SelectedValue.ToString());
classRoom.Product.ProductId = int.Parse(product.SelectedValue.ToString());
bool flg = ClassRoomManagerBLL.Update(classRoom); // 调用自定义的更新方法
if (flg == true)
{
this.LoadTreeClassRoom(); // 重新加载课程目录树
PopupMessage("修改成功!!!"); // 弹出 "修改成功" 的消息提示框
}
else
{
PopupMessage("设施名称已经存在,并却设施名称长度最长为 10位!!!"); // 弹出消息提示框
}
this.dtvClassRoom.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;
}
}
protected void dtvClassRoom_ItemInserting(object sender, DetailsViewInsertEventArgs e)
{
try
{
ClassRoom classRoom = new ClassRoom();
// 找到dtvClassRoom中的行中控件的值
TextBox title = this.dtvClassRoom.FindControl("txtTitle") as TextBox;
RadioButtonList roomType = this.dtvClassRoom.FindControl("rbtGetRoomTypes") as RadioButtonList;
DropDownList product = this.dtvClassRoom.FindControl("ddlGetProducts") as DropDownList;
classRoom.Title = title.Text;
classRoom.RoomType.TypeId = int.Parse(roomType.SelectedValue.ToString());
classRoom.Product.ProductId = int.Parse(product.SelectedValue.ToString());
bool flg = ClassRoomManagerBLL.Create(classRoom); // 调用自定义的插入方法
if (flg == true)
{
this.LoadTreeClassRoom(); // 重新加载课程目录树
PopupMessage("添加成功!!!"); // 弹出 "添加成功" 的消息提示框
}
else
{
PopupMessage("设施名称已经存在,并却设施名称长度最长为 10位!!!"); // 弹出消息提示框
}
this.dtvClassRoom.ChangeMode(DetailsViewMode.ReadOnly);
}
catch (Exception ex)
{
string err = ex.Message;
}
finally
{
e.Cancel = true; // 取消插入事件,不执行“插入”按钮绑定的方法
}
}
protected void dtvClassRoom_ItemDeleting(object sender, DetailsViewDeleteEventArgs e)
{
try
{
int roomId = int.Parse(e.Keys["RoomId"].ToString()); // 获得 DetailsView 的 DataKeyNames 键值,该属性中保存有实体对象的主键
int nodeOne = this.trvClassRoom.Nodes[0].ChildNodes.Count;
int nodeTwo = this.trvClassRoom.Nodes[1].ChildNodes.Count;
if ((nodeOne == 1 && nodeTwo == 0) || (nodeOne == 0 && nodeTwo == 1))
{
PopupMessage("节点不能全部删除,若想删除,请添加节点后再进行删除!!!"); // 弹出 "删除成功" 的消息提示框
}
else
{
ClassRoomManagerBLL.Delete(roomId); // 调用自定义的删除方法
this.LoadTreeClassRoom(); // 重新加载课程目录树
PopupMessage("删除成功!!!"); // 弹出 "删除成功" 的消息提示框
}
this.dtvClassRoom.ChangeMode(DetailsViewMode.ReadOnly);
}
catch (Exception ex)
{
string err = ex.Message;
}
finally
{
e.Cancel = true; // 取消删除事件,不执行“删除”按钮绑定的方法
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -