📄 index.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 BLL;
using Model;
using System.Xml;
using System.IO;
public partial class Index : System.Web.UI.Page
{
private RoomOperatorBLL roomOp = new RoomOperatorBLL();
private RoomTypeOperatorBLL roomTypeOp = new RoomTypeOperatorBLL();
protected void Page_Load(object sender, EventArgs e)
{
string path = Server.MapPath("Bin\\RoomState.xml");
int number = 0;
if (File.Exists(path))
{
try
{
XmlDocument doc = new XmlDocument();
doc.Load(path);
XmlElement root = doc.DocumentElement;
if (root != null && root.HasChildNodes)
number = root.ChildNodes.Count;
}
catch (Exception ex)
{
Response.Write("<script>alert(" + ex.Message + ex.StackTrace + ")</script>");
}
}
else
Response.Write("<script>alert('指定的文档不存在!')</script>");
this.rblRoomState.Height = Convert.ToInt32(number * 41.7);
}
/// <summary>
/// 获得图片的完整路径
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
protected string GetUrl(object str)
{
string temp = str.ToString();
string pic = string.Empty;
if (!string.IsNullOrEmpty(temp))
pic = GetImgUrl(temp.Trim());
return pic;
}
/// <summary>
/// 从XML文档中取出对应节点的图片路径
/// </summary>
/// <returns></returns>
private string GetImgUrl(string str)
{
string path = Server.MapPath("Bin\\RoomState.xml");
string url = string.Empty;
if (File.Exists(path))
{
try
{
XmlDocument doc = new XmlDocument();
doc.Load(path);
XmlElement root = doc.DocumentElement;
if (root != null && root.HasChildNodes)
{
XmlNode node = root.SelectSingleNode("state[@value='" + str.Trim() + "']");
if (node != null)
{
url = node.Attributes["imgUrl"].Value;
}
}
}
catch (Exception ex)
{
Response.Write("<script>alert(" + ex.Message + ex.StackTrace + ")</script>");
}
}
else
Response.Write("<script>alert('指定的文档不存在!')</script>");
return url;
}
protected void ddl_RoomType_SelectedIndexChanged(object sender, EventArgs e)
{
string state = this.ddl_RoomType.SelectedValue;
this.DataListImage.DataSourceID = "XmlDataSourceState";
if (!string.IsNullOrEmpty(state))
{
if (!state.Equals("0"))
{
this.dlist_Room.DataSourceID = "";
this.dlist_Room.DataSource = roomOp.GetAllRoomsByTypeId(int.Parse(state));
this.dlist_Room.DataBind();
}
else
this.dlist_Room.DataSourceID = "ObjectDataSourceRoom";
}
}
protected void ddl_RoomType_DataBound(object sender, EventArgs e)
{
ListItem temp = new ListItem();
temp.Text = "--选择类型--";
temp.Value = "0";
temp.Selected = true;
this.ddl_RoomType.Items.Add(temp);
Hashtable ht = Hashtable.Synchronized(new Hashtable());
ArrayList arr = new ArrayList();
for (int i = 0; i < this.ddl_RoomType.Items.Count; i++)
//将DropDownList中的值添加到HashTable中并对应键/值
ht.Add(this.ddl_RoomType.Items[i].Value, this.ddl_RoomType.Items[i].Text);
arr.Add(ht);
this.ddl_RoomType.Items.Clear();//清空DropDownList中的值
this.ddl_RoomType.Items.Add(temp);//将自己定义的ListItem放在第一位
//循环遍历依次将值再次添加到DropDownList中并且排序
for (int i = 0; i < arr.Count; i++)
{
Hashtable hash = (Hashtable)arr[i];
if (hash != null)
{
IDictionaryEnumerator id = hash.GetEnumerator();
while (id.MoveNext())
{
if (!id.Value.Equals("--选择类型--"))
{
ListItem item = new ListItem();
item.Text = id.Value.ToString();
item.Value = id.Key.ToString();
this.ddl_RoomType.Items.Add(item);
}
}
}
}
this.ddl_RoomType.Text = "--选择类型--";
}
protected void dlist_Room_ItemCommand(object source, DataListCommandEventArgs e)
{
int n = e.Item.ItemIndex;
this.dlist_Room.Items[n].BackColor = System.Drawing.Color.Red;
//必须先确保设置了DataKeyField属性
int key = Convert.ToInt32(this.dlist_Room.DataKeys[n]);//找到选中项所对应的DataKeyField属性
Session["key"] = key;
Room room = new Room();
room = roomOp.GetRoomByRoomID(key);
//选择之前重新绑定一次
BeginSetColor(room.State.Trim());
}
/// <summary>
/// 设置颜色之前先判断需要设置的状态
/// </summary>
/// <param name="str"></param>
private void BeginSetColor(string str)
{
string path = Server.MapPath("Bin\\RoomState.xml");
if (File.Exists(path))
{
try
{
XmlDocument doc = new XmlDocument();
doc.Load(path);
XmlElement root = doc.DocumentElement;
if (root != null && root.HasChildNodes)
{
XmlNode node = root.SelectSingleNode("state[@value='" + str.Trim() + "']");
if (node != null)
{
this.rblRoomState.SelectedValue = node.Attributes["value"].Value;//选中单选按钮集合中的某项
//遍历 DataList 找到匹配的项
foreach (DataListItem temp in this.DataListImage.Items)
{
if (temp.FindControl("ImageButtonState") != null)
{
ImageButton img = temp.FindControl("ImageButtonState") as ImageButton;
if (img.AlternateText.Equals(str.Trim()))
SetBorderColor(img);
}
}
}
}
}
catch (Exception ex)
{
Response.Write("<script>alert(" + ex.Message + ex.StackTrace + ")</script>");
}
}
else
Response.Write("<script>alert('指定的文档不存在!')</script>");
}
/// <summary>
/// 设置选中后对应的图片颜色和边框
/// </summary>
/// <param name="imgBtn"></param>
private void SetBorderColor(ImageButton imgBtn)
{
//遍历所有的图片恢复原来的边框
foreach (DataListItem temp in this.DataListImage.Items)
{
ImageButton img = temp.FindControl("ImageButtonState") as ImageButton;
if (img != null)
{
img.BorderWidth = 0;
img.BorderColor = System.Drawing.Color.Transparent;
}
}
if (imgBtn != null)
{
imgBtn.BorderWidth = 2;
imgBtn.BorderColor = System.Drawing.Color.Red;
imgBtn.BorderStyle = BorderStyle.Dashed;
}
}
protected void rblRoomState_SelectedIndexChanged(object sender, EventArgs e)
{
BeginSetColor(this.rblRoomState.SelectedValue);
if (Session["Key"] != null)
{
int key = int.Parse(Session["Key"].ToString());
Room room = new Room();
room = roomOp.GetRoomByRoomID(key);
//由于回发导致房间号的选中消失
//遍历所有的房间号恢复到原来的选中状态
foreach (DataListItem temp in this.dlist_Room.Items)
{
Label number = temp.FindControl("lbl_roomNumber") as Label;
if (number != null)
{
//提取存入会话Session的房间编号查询出之前选中的房间号并重新选中
if (number.Text.Equals(room.Roomnumber))
{
temp.BorderWidth = 1;
temp.BackColor = System.Drawing.Color.Red;
}
}
}
}
}
/// <summary>
/// 更改房间状态
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btn_Modify_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(this.rblRoomState.SelectedValue))//如果已经选中
{
if (Session["Key"] != null) //会话中存在要更改的项
{
try
{
int key = int.Parse(Session["Key"].ToString());
Room room = new Room();
room = roomOp.GetRoomByRoomID(key);
room.State = this.rblRoomState.SelectedValue;
if (roomOp.ModifyValuesFormRoom(room))
{
Server.Transfer("~\\Index.aspx");
}
}
catch (Exception ex)
{
throw new Exception(ex.Message + ex.StackTrace);
}
}
}
else
Response.Write("<script>alert('请选中您要更新的房间!')</script>");
}
protected void DataListImage_ItemCommand(object source, DataListCommandEventArgs e)
{
ImageButton img = DataListImage.Items[e.Item.ItemIndex].FindControl("ImageButtonState") as ImageButton;
if (img != null)
{
SetBorderColor(img);
this.rblRoomState.SelectedValue = img.AlternateText.Trim();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -