📄 roomsysmng.aspx.cs
字号:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.Odbc;
using System.Configuration;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace RoomMngSystem
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Panel Panel1;
protected System.Web.UI.WebControls.Panel Panel2;
protected System.Web.UI.WebControls.LinkButton LinkButton4;
protected System.Web.UI.WebControls.LinkButton LinkButton5;
protected System.Web.UI.WebControls.LinkButton LinkButton6;
string datatable,head,col;
protected System.Web.UI.WebControls.LinkButton LinkButton7;
protected System.Web.UI.WebControls.LinkButton LinkButton8;
protected System.Web.UI.WebControls.LinkButton LinkButton9;
protected System.Web.UI.WebControls.LinkButton LinkButton11;
protected System.Web.UI.WebControls.LinkButton LinkButton12;
protected System.Web.UI.WebControls.Panel Panel3;
protected System.Web.UI.WebControls.Label Label2;
protected System.Web.UI.WebControls.Label Label3;
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.Button Button5;
protected System.Web.UI.WebControls.TextBox TextBox2;
protected System.Web.UI.WebControls.Label dLabel1;
protected System.Web.UI.WebControls.Button Button7;
protected System.Web.UI.WebControls.Button Button6;
protected System.Web.UI.WebControls.Button Button8;
protected System.Web.UI.WebControls.Button Button10;
protected System.Web.UI.WebControls.Button Button9;
protected System.Web.UI.WebControls.Button Button11;
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Web.UI.WebControls.LinkButton LinkButton1;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.Button Button12;
protected System.Web.UI.WebControls.Image Image1;
protected System.Web.UI.WebControls.Button Button4;
private void DataBindGrid(string table,string col)
{
//定义数据连接对象,其中数据库连接字符串是在Web.Config文件中定义的
OdbcDataAdapter da;
OdbcConnection conn = new OdbcConnection();
conn.ConnectionString=ConfigurationSettings.AppSettings["oCn_Str"];
//创建数据适配器对象
da =new OdbcDataAdapter("Select * from ["+datatable+"]",conn);
//创建DataSet对象
DataSet ds = new DataSet();
try
{
//填充数据集
da.Fill(ds,"testTable");
//进行数据绑定
DataGrid1.DataSource = ds.Tables["testTable"];
DataGrid1.DataBind();
for(int i=0;i<ds.Tables[0].Rows.Count;i++)
{
Label lb = new Label();
// TextBox text =new TextBox();
lb=(Label)DataGrid1.Items[i].FindControl("dLabel1"); //Label1是你第一模板列中Label的ID
// text=(TextBox)DataGrid1.Items[i].FindControl("dTextBox1");
lb.Text=ds.Tables["testTable"].Rows[i][col].ToString(); //假设第一列中,要帮定的数据是ZID字段
// text.Text=ds.Tables["testTable"].Rows[i][col].ToString();
}
}
catch
{
// Label1.Text="出错";
}
}
private void DataBindGridText(string col)
{
OdbcDataAdapter da;
OdbcConnection conn = new OdbcConnection();
conn.ConnectionString=ConfigurationSettings.AppSettings["oCn_Str"];
//创建数据适配器对象
da =new OdbcDataAdapter("Select * from ["+datatable+"]",conn);
//创建DataSet对象
DataSet ds = new DataSet();
da.Fill(ds,"txt");
}
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
string user;
try
{
user=Session["UserName"].ToString();
}
catch
{
this.Response.Redirect("../Enter.aspx");
}
if(!IsPostBack)
{
Session["tablename"]="";
Session["headname"]="";
Session["colname"]="";
}
else
{
col=Session["colname"].ToString();
head=Session["headname"].ToString();
datatable=Session["tablename"].ToString();
// Label1.Text="";
}
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Button8.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.Button1.Click += new System.EventHandler(this.LinkButton10_Click);
this.Button12.Click += new System.EventHandler(this.Button12_Click);
this.LinkButton4.Click += new System.EventHandler(this.LinkButton4_Click);
this.LinkButton5.Click += new System.EventHandler(this.LinkButton5_Click);
this.LinkButton6.Click += new System.EventHandler(this.LinkButton6_Click);
this.LinkButton7.Click += new System.EventHandler(this.LinkButton7_Click);
this.LinkButton8.Click += new System.EventHandler(this.LinkButton8_Click);
this.LinkButton9.Click += new System.EventHandler(this.LinkButton9_Click);
this.LinkButton12.Click += new System.EventHandler(this.LinkButton12_Click);
this.LinkButton11.Click += new System.EventHandler(this.LinkButton11_Click);
this.LinkButton1.Click += new System.EventHandler(this.LinkButton13_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.Button11.Click += new System.EventHandler(this.Button1_Click);
this.Button9.Click += new System.EventHandler(this.Button2_Click);
this.Button10.Click += new System.EventHandler(this.Button3_Click);
this.Button4.Click += new System.EventHandler(this.Button4_Click);
this.Button5.Click += new System.EventHandler(this.Button5_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void OnEditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
//取得字符段
col=Session["colname"].ToString();
head=Session["headname"].ToString();
datatable=Session["tablename"].ToString();
DataGrid1.EditItemIndex=e.Item.ItemIndex;
DataBindGrid(datatable,col);
}
private void OnDeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
OdbcConnection conn = new OdbcConnection();
conn.ConnectionString=ConfigurationSettings.AppSettings["oCn_Str"];
string sqlcom,txtcol;
txtcol=((Label)e.Item.FindControl("dLabel")).Text;
//打开数据连接
conn.Open();
try
{
sqlcom="DELETE ["+datatable+"] WHERE ["+head+"]='"+txtcol+"'";
//定义命令对象
OdbcCommand cmd = new OdbcCommand(sqlcom,conn);
//执行SQL命令
cmd.ExecuteNonQuery();
DataBindGrid(datatable,col);
}
catch
{
}
//关闭连接对象
conn.Close();
}
private void OnCancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
//取得字符段
// col=Session["colname"].ToString();
// head=Session["headname"].ToString();
// datatable=Session["tablename"].ToString();
DataGrid1.EditItemIndex=-1;
DataBindGrid(datatable,col);
}
private void OnPageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
this.DataGrid1.CurrentPageIndex=e.NewPageIndex;
DataBindGrid(datatable,col);
}
private void OnUpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
string cell3=((Label)e.Item.FindControl("dLabel")).Text;
string cell4=((TextBox)e.Item.FindControl("dTextBox1")).Text;
string sqlcom="UPDATE ["+datatable +"] set ["+col+"]='"+cell4 +"' where [编号]='"+cell3+"'";
OdbcConnection conn = new OdbcConnection();
conn.ConnectionString=ConfigurationSettings.AppSettings["oCn_Str"];
//定义命令对象
OdbcCommand cmd = new OdbcCommand(sqlcom,conn);
try
{
//打开数据连接
conn.Open();
//执行SQL命令
cmd.ExecuteNonQuery();
//取消编辑
DataGrid1.EditItemIndex = -1;
DataBindGrid(datatable,col);
}
catch(Exception err)
{
//输出异常信息
Label1.Text="更新失败!";
}
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 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)
{
sqlcom="DELETE ["+datatable+"] WHERE ["+head+"]='"+((Label)oDataGridItem.FindControl("dLabel")).Text+"'";
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -