📄 roomdatamng.aspx.cs
字号:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.Odbc;
using System.Configuration;
namespace RoomMngSystem
{
/// <summary>
/// WebForm2 的摘要说明。
/// </summary>
public class WebForm2 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Web.UI.WebControls.Label Label14;
protected System.Web.UI.WebControls.Button Button7;
protected System.Web.UI.WebControls.Button Button6;
protected System.Web.UI.WebControls.Button Button5;
protected System.Web.UI.WebControls.Button Button8;
protected System.Web.UI.WebControls.Button Button9;
protected System.Web.UI.WebControls.Button Button10;
protected System.Web.UI.WebControls.Button Button11;
protected System.Web.UI.WebControls.Image Image1;
protected System.Web.UI.WebControls.Panel Panel2;
//房屋信息
public OdbcDataReader InitDropdownList_buildinginfo()
{//初始化下拉列表框
OdbcConnection conn = new OdbcConnection();
conn.ConnectionString=ConfigurationSettings.AppSettings["oCn_Str"];
conn.Open();
OdbcCommand cmd=new OdbcCommand("select [bno] from [buildinginfo]",conn);
return cmd.ExecuteReader(CommandBehavior.CloseConnection);
}
//单位信息
public OdbcDataReader InitDropdownList_unit()
{//初始化下拉列表框
OdbcConnection conn = new OdbcConnection();
conn.ConnectionString=ConfigurationSettings.AppSettings["oCn_Str"];
conn.Open();
OdbcCommand cmd=new OdbcCommand("select [index],[unit] from [unit]",conn);
return cmd.ExecuteReader(CommandBehavior.CloseConnection);
}
//用途
public OdbcDataReader InitDropdownList_using()
{//初始化下拉列表框
OdbcConnection conn = new OdbcConnection();
conn.ConnectionString=ConfigurationSettings.AppSettings["oCn_Str"];
conn.Open();
OdbcCommand cmd=new OdbcCommand("select [index],[using] from [using]",conn);
return cmd.ExecuteReader(CommandBehavior.CloseConnection);
}
//水
public OdbcDataReader InitDropdownList_water()
{//初始化下拉列表框
OdbcConnection conn = new OdbcConnection();
conn.ConnectionString=ConfigurationSettings.AppSettings["oCn_Str"];
conn.Open();
OdbcCommand cmd=new OdbcCommand("select [index],[water] from [water]",conn);
return cmd.ExecuteReader(CommandBehavior.CloseConnection);
}
//电
public OdbcDataReader InitDropdownList_elect()
{//初始化下拉列表框
OdbcConnection conn = new OdbcConnection();
conn.ConnectionString=ConfigurationSettings.AppSettings["oCn_Str"];
conn.Open();
OdbcCommand cmd=new OdbcCommand("select [index],[elect] from [elect]",conn);
return cmd.ExecuteReader(CommandBehavior.CloseConnection);
}
//网
public OdbcDataReader InitDropdownList_net()
{//初始化下拉列表框
OdbcConnection conn = new OdbcConnection();
conn.ConnectionString=ConfigurationSettings.AppSettings["oCn_Str"];
conn.Open();
OdbcCommand cmd=new OdbcCommand("select [index],[net] from [net]",conn);
return cmd.ExecuteReader(CommandBehavior.CloseConnection);
}
//电话
public OdbcDataReader InitDropdownList_phone()
{//初始化下拉列表框
OdbcConnection conn = new OdbcConnection();
conn.ConnectionString=ConfigurationSettings.AppSettings["oCn_Str"];
conn.Open();
OdbcCommand cmd=new OdbcCommand("select [tel_bit],[tel_info] from [phone]",conn);
return cmd.ExecuteReader(CommandBehavior.CloseConnection);
}
//是不闲置
public OdbcDataReader InitDropdownList_unused()
{//初始化下拉列表框
OdbcConnection conn = new OdbcConnection();
conn.ConnectionString=ConfigurationSettings.AppSettings["oCn_Str"];
conn.Open();
OdbcCommand cmd=new OdbcCommand("select [unused_bit],[unused_info] from [unused]",conn);
return cmd.ExecuteReader(CommandBehavior.CloseConnection);
}
//性别
public OdbcDataReader InitDropdownList_sex()
{//初始化下拉列表框
OdbcConnection conn = new OdbcConnection();
conn.ConnectionString=ConfigurationSettings.AppSettings["oCn_Str"];
conn.Open();
OdbcCommand cmd=new OdbcCommand("select [index],[sex] from [sex]",conn);
return cmd.ExecuteReader(CommandBehavior.CloseConnection);
}
//多媒体
public OdbcDataReader InitDropdownList_multimedia()
{//初始化下拉列表框
OdbcConnection conn = new OdbcConnection();
conn.ConnectionString=ConfigurationSettings.AppSettings["oCn_Str"];
conn.Open();
OdbcCommand cmd=new OdbcCommand("select [media_int],[media] from [multimedia]",conn);
return cmd.ExecuteReader(CommandBehavior.CloseConnection);
}
private void DataBindGrid()
{
//定义数据连接对象,其中数据库连接字符串是在Web.Config文件中定义的
OdbcDataAdapter da;
OdbcConnection conn = new OdbcConnection();
conn.ConnectionString=ConfigurationSettings.AppSettings["oCn_Str"];
//创建数据适配器对象
da =new OdbcDataAdapter("Select * from [V_RoomInfo]",conn);
//创建DataSet对象
DataSet ds = new DataSet();
try
{
//填充数据集
da.Fill(ds,"testTable");
//进行数据绑定
DataGrid1.DataSource = ds.Tables["testTable"];
DataGrid1.DataBind();
}
catch
{
}
}
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
string user;
try
{
user=Session["UserName"].ToString();
}
catch
{
this.Response.Redirect("../Enter.aspx");
}
if(!IsPostBack)
{
DataBindGrid();
}
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Button5.Click += new System.EventHandler(this.LinkButton1_Click);
this.Button6.Click += new System.EventHandler(this.LinkButton2_Click);
this.Button7.Click += new System.EventHandler(this.LinkButton3_Click);
this.DataGrid1.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.OnPageIndexChanged);
this.DataGrid1.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.OnCancelCommand);
this.DataGrid1.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.OnEditCommand);
this.DataGrid1.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.OnUpdateCommand);
this.DataGrid1.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.OnDeleteCommand);
this.DataGrid1.SelectedIndexChanged += new System.EventHandler(this.DataGrid1_SelectedIndexChanged);
this.Button11.Click += new System.EventHandler(this.Button4_Click);
this.Button10.Click += new System.EventHandler(this.Button3_Click);
this.Button9.Click += new System.EventHandler(this.Button2_Click);
this.Button8.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void OnEditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex=e.Item.ItemIndex;
DataBindGrid();
}
private void OnDeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
OdbcConnection conn = new OdbcConnection();
conn.ConnectionString=ConfigurationSettings.AppSettings["oCn_Str"];
string sqlcom;
//打开数据连接
conn.Open();
try
{
sqlcom="DELETE [RoomInfo] WHERE [index]="+((TextBox)e.Item.FindControl("IdLabel")).Text;
//定义命令对象
OdbcCommand cmd = new OdbcCommand(sqlcom,conn);
//执行SQL命令
cmd.ExecuteNonQuery();
DataBindGrid();
}
catch
{
return;
}
//关闭连接对象
conn.Close();
}
private void OnCancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex=-1;
DataBindGrid();
}
private void OnPageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
this.DataGrid1.CurrentPageIndex=e.NewPageIndex;
DataBindGrid();
}
private void OnUpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
string cell4=((DropDownList)e.Item.FindControl("Nametxt")).SelectedItem.Value ;
string cell5=((TextBox)e.Item.FindControl("Textbox1")).Text;
string cell6=((TextBox)e.Item.FindControl("Textbox2")).Text;
string cell7=((TextBox)e.Item.FindControl("Textbox3")).Text;
string cell8=((DropDownList)e.Item.FindControl("Textbox4")).SelectedItem.Value;
string cell9=((DropDownList)e.Item.FindControl("Textbox5")).SelectedItem.Value;
string cell10=((DropDownList)e.Item.FindControl("Textbox6")).SelectedItem.Value;
string cell11=((DropDownList)e.Item.FindControl("Textbox7")).SelectedItem.Value;
string cell12=((DropDownList)e.Item.FindControl("Textbox8")).SelectedItem.Value;
string cell13=((DropDownList)e.Item.FindControl("Textbox9")).SelectedItem.Value;
string cell14=((TextBox)e.Item.FindControl("Textbox10")).Text;
string cell15=((DropDownList)e.Item.FindControl("Textbox11")).SelectedItem.Value;
string cell16=((DropDownList)e.Item.FindControl("Textbox12")).SelectedItem.Value;
string cell17=((TextBox)e.Item.FindControl("Textbox13")).Text;
string cell3=((TextBox)e.Item.FindControl("IdTxt")).Text;
string cell18=((DropDownList)e.Item.FindControl("Textbox14")).SelectedItem.Value;
string sqlcom="UPDATE [RoomInfo] set bno='"+cell4+"',rno='"+cell5+"',area="+cell6+",seatnum="+cell7+",unit='"+cell8+"',using="+cell9+",water="+cell10+",elec="+cell11+",net="+cell12+",phone="+cell13+",charge='"+cell14+"',used="+cell15+",sex="+cell16+",memo='"+cell17+"',multimedia='"+cell18+"' WHERE [index]="+cell3;
OdbcConnection conn = new OdbcConnection();
conn.ConnectionString=ConfigurationSettings.AppSettings["oCn_Str"];
//定义命令对象
OdbcCommand cmd = new OdbcCommand(sqlcom,conn);
//打开数据连接
conn.Open();
try
{
//执行SQL命令
cmd.ExecuteNonQuery();
//取消编辑
DataGrid1.EditItemIndex = -1;
DataBindGrid();
}
catch
{
//输出异常信息
}
finally
{
//关闭连接对象
conn.Close();
}
}
private void Button1_Click(object sender, System.EventArgs e)
{
System.Web.UI.WebControls.CheckBox chkExport;
foreach(DataGridItem oDataGridItem in DataGrid1.Items)
{
chkExport=(CheckBox)oDataGridItem.FindControl("chkExport");
chkExport.Checked=true;
}
}
private void Button2_Click(object sender, System.EventArgs e)
{
System.Web.UI.WebControls.CheckBox chkExport;
foreach(DataGridItem oDataGridItem in DataGrid1.Items)
{
chkExport=(CheckBox)oDataGridItem.FindControl("chkExport");
chkExport.Checked=false;
}
}
private void Button3_Click(object sender, System.EventArgs e)
{
System.Web.UI.WebControls.CheckBox chkExport;
string sID,sqlcom;
OdbcConnection conn = new OdbcConnection();
conn.ConnectionString=ConfigurationSettings.AppSettings["oCn_Str"];
//打开数据连接
conn.Open();
foreach(DataGridItem oDataGridItem in DataGrid1.Items)
{
chkExport=(CheckBox)oDataGridItem.FindControl("chkExport");
if(true==chkExport.Checked)
{
try
{
sID=((TextBox)(oDataGridItem.FindControl("IdLabel"))).Text;
sqlcom="DELETE [RoomInfo] WHERE [index]="+sID;
//定义命令对象
OdbcCommand cmd = new OdbcCommand(sqlcom,conn);
//执行SQL命令
cmd.ExecuteNonQuery();
}
catch
{
return;
}
}
}
DataBindGrid();
}
private void LinkButton1_Click(object sender, System.EventArgs e)
{
this.Response.Redirect("RoomUserMng.aspx");
}
private void LinkButton3_Click(object sender, System.EventArgs e)
{
this.Response.Redirect("RoomDataMng.aspx");
}
private void LinkButton2_Click(object sender, System.EventArgs e)
{
this.Response.Redirect("RoomSysMng.aspx");
}
private void Button4_Click(object sender, System.EventArgs e)
{
this.Response.Redirect("RoomDataAdd.aspx");
}
private void DataGrid1_SelectedIndexChanged(object sender, System.EventArgs e)
{
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -