📄 roommanage.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;
public partial class roomManage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
HotelDBConn hotelDB = new HotelDBConn();
ArrayList rooms = hotelDB.getHotelInfo();
TableRow row = null;
TableCell cell = null;
ImageButton imgEdit = null;
ImageButton imgDel = null;
TextBox txt = null;
Label lbl = null;
int rowNum = 0;
foreach (Hotel hotel in rooms)
{
row = new TableRow();
rowNum++;
//房间号
cell = new TableCell();
txt = new TextBox();
txt.Text = hotel.Room.ToString();
txt.Enabled = false;
cell.Controls.Add(txt);
row.Cells.Add(cell);
//房型
cell = new TableCell();
txt = new TextBox();
txt.Text = hotel.Type;
txt.Enabled = false ;
cell.Controls.Add(txt);
row.Cells.Add(cell);
//房价
cell = new TableCell();
txt = new TextBox();
txt.Text = hotel.Price.ToString();
txt.Enabled = false ;
cell.Controls.Add(txt);
row.Cells.Add(cell);
//房间电话
cell = new TableCell();
txt = new TextBox();
txt.Text = hotel.Telephone.ToString ();
txt.Enabled = false ;
cell.Controls.Add(txt);
row.Cells.Add(cell);
//备注
cell = new TableCell();
txt = new TextBox();
txt.Text = hotel.Memo;
txt.Enabled = false ;
cell.Controls.Add(txt);
row.Cells.Add(cell);
//折扣
cell = new TableCell();
txt = new TextBox();
if (hotel.Discount != null)
{
double discount = hotel.Discount;
discount = discount * 100;
txt.Text = discount.ToString();
}
txt.Enabled = false;
txt.Width = 25;
lbl = new Label();
lbl.Text = "%";
cell.Controls.Add(txt);
cell.Controls.Add(lbl);
row.Cells.Add(cell);
//编辑
cell = new TableCell();
imgEdit = new ImageButton();
imgEdit.ID = rowNum.ToString();
imgEdit.ImageUrl = "pic/edit.gif";
imgEdit.Click += new ImageClickEventHandler(imgEdit_Click);
cell.Controls.Add(imgEdit);
row.Cells.Add(cell);
//删除
cell = new TableCell();
imgDel = new ImageButton();
imgDel.ID = "del_" + hotel.Room;
imgDel.ImageUrl = "pic/del.gif";
imgDel.Click += new ImageClickEventHandler(imgDel_Click);
cell.Controls.Add(imgDel);
row.Cells.Add(cell);
Table2.Rows.Add(row);
}
}
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
Hotel hotel = new Hotel();
//房间号
foreach (Control txt in Table1.Rows[1].Cells[0].Controls)
{
if (txt is TextBox)
{
hotel.Room = Convert.ToInt32(((TextBox)txt).Text);
}
}
//房间类型
foreach (TextBox txt in Table1.Rows[1].Cells[1].Controls)
{
/* ArrayList values = new ArrayList();
values.Add("单人间");
values.Add("标准间");
values.Add("豪华套房");
values.Add("总统套房");
values.Add("蜜月套房");
DropDownList dropdown1 = new DropDownList();
dropdown1.DataSource = values;
// dropdown1.DataBind();
hotel.Type = dropdown1.DataSource.ToString() ;
*/
hotel.Type = txt.Text;
}
//房间价格
foreach (TextBox txt in Table1.Rows[1].Cells[2].Controls)
{
hotel.Price = Convert.ToDouble(((TextBox)txt).Text);
}
//房间电话
foreach (TextBox txt in Table1.Rows[1].Cells[3].Controls)
{
hotel.Telephone = Convert.ToInt32(((TextBox)txt).Text);
}
//备注
foreach (TextBox txt in Table1.Rows[1].Cells[4].Controls)
{
hotel.Memo = txt.Text;
}
//折扣
try
{
foreach (Control txt in Table1.Rows[1].Cells[5].Controls)
{
if (txt is TextBox)
{
// ((TextBox)txt).Enabled = false;
hotel.Discount = Convert.ToDouble(((TextBox)txt).Text) / 100;
}
}
}
catch
{
Response.Write("<script>window.alert('折扣输入格式出错!');</script>");
}
if (hotel.Room.ToString() == "")
{
Response.Write("<script>window.alert('房间号不能为空');</script>");
}
else
{
try
{
if (hotel.Discount > 1 || hotel.Discount < 0)
{
foreach (Control txt in Table1.Rows[1].Cells[5].Controls)
{
if (txt is TextBox)
{
hotel.Discount = 1.0;
((TextBox)txt).Text = "100";
}
}
Response.Write("<script>window.alert('折扣超出范围!');</script>");
}
else
{
HotelDBConn hotelDB = new HotelDBConn();
hotelDB.insertRoom(hotel);
Response.Redirect("roomManage.aspx");
}
}
catch (System.Exception err)
{
Response.Write("<script>window.alert('添加新房间出错!');</script>");
}
}
}
protected void imgDel_Click(object sender, ImageClickEventArgs e)
{
Hotel hotel = new Hotel();
HotelDBConn hotelDB = new HotelDBConn();
int room = Convert.ToInt32(((ImageButton)sender).ID.Substring(4));
hotelDB.delRoom(room);
Response.Redirect("roomManage.aspx");
}
protected void imgEdit_Click(object sender, ImageClickEventArgs e)
{
int rowNum = Convert.ToInt32(((ImageButton)sender).ID);
if (((ImageButton)sender).ImageUrl == "pic/edit.gif")
{
//房间号
foreach (TextBox txt in Table2.Rows[rowNum].Cells[0].Controls)
{
txt.Enabled = false;
}
//房间类型
foreach (TextBox txt in Table2.Rows[rowNum].Cells[1].Controls)
{
txt.Enabled = true;
}
//房间价格
foreach (TextBox txt in Table2.Rows[rowNum].Cells[2].Controls)
{
txt.Enabled = true;
}
//房间电话
foreach (TextBox txt in Table2.Rows[rowNum].Cells[3].Controls)
{
txt.Enabled = true;
}
//备注
foreach (TextBox txt in Table2.Rows[rowNum].Cells[4].Controls)
{
txt.Enabled = true ;
}
//折扣
foreach (Control txt in Table2.Rows[rowNum].Cells[5].Controls)
{
if (txt is TextBox)
{
((TextBox)txt).Enabled = true;
}
}
//保存
((ImageButton)sender).ImageUrl = "pic/save.bmp";
}
else
{
Hotel hotel =new Hotel ();
//房间号
foreach (TextBox txt in Table2.Rows[rowNum].Cells[0].Controls)
{
txt.Enabled = false;
hotel.Room = Convert.ToInt32(((TextBox)txt).Text);
}
//房间类型
foreach (TextBox txt in Table2.Rows[rowNum].Cells[1].Controls)
{
txt.Enabled = false;
hotel.Type = txt.Text;
}
//房间价格
foreach (TextBox txt in Table2.Rows[rowNum].Cells[2].Controls)
{
txt.Enabled = false;
hotel.Price = Convert.ToDouble (((TextBox)txt).Text);
}
//房间电话
foreach (TextBox txt in Table2.Rows[rowNum].Cells[3].Controls)
{
txt.Enabled = false;
hotel.Telephone = Convert.ToInt32(((TextBox)txt).Text);
}
//备注
foreach (TextBox txt in Table2.Rows[rowNum].Cells[4].Controls)
{
txt.Enabled = false;
hotel.Memo = txt.Text;
}
//折扣
try
{
foreach (Control txt in Table2.Rows[rowNum].Cells[5].Controls)
{
if (txt is TextBox)
{
((TextBox)txt).Enabled = false;
hotel.Discount = Convert.ToDouble(((TextBox)txt).Text) / 100;
}
}
}
catch
{
Response.Write("<script>window.alert('折扣输入格式出错!');</script>");
}
((ImageButton)sender).ImageUrl = "pic/edit.gif";
//保存记录
try
{
if (hotel.Discount > 1 || hotel.Discount < 0)
{
foreach (Control txt in Table2.Rows[rowNum].Cells[5].Controls)
{
if (txt is TextBox)
{
hotel.Discount = 1.0;
((TextBox)txt).Text = "100";
}
}
Response.Write("<script>window.alert('折扣超出范围!');</script>");
}
else
{
HotelDBConn hotelDB = new HotelDBConn();
hotelDB.updateRoom(hotel);
}
}
catch
{
Response.Write("<script>window.alert('更新房间出错!');</script>");
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -