📄 roomusermng.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>
/// RoomUserMng 的摘要说明。
/// </summary>
public class RoomUserMng : System.Web.UI.Page
{
protected System.Web.UI.WebControls.LinkButton LinkButton2;
protected System.Web.UI.WebControls.LinkButton LinkButton3;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.Button Button2;
protected System.Web.UI.WebControls.Button Button3;
protected System.Web.UI.WebControls.Button Button4;
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.Button Button5;
protected System.Web.UI.WebControls.Button Button6;
protected System.Web.UI.WebControls.Button Button7;
protected System.Web.UI.WebControls.Label Label3;
protected System.Web.UI.WebControls.Image Image1;
protected System.Web.UI.WebControls.Panel Panel2;
private void DataBindGrid()
{
//定义数据连接对象,其中数据库连接字符串是在Web.Config文件中定义的
OdbcDataAdapter da;
OdbcConnection conn = new OdbcConnection();
conn.ConnectionString=ConfigurationSettings.AppSettings["oCn_Str"];
//创建数据适配器对象
da =new OdbcDataAdapter("Select * from [UserInfo]",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.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Button2.Click += new System.EventHandler(this.Button2_Click);
this.Button3.Click += new System.EventHandler(this.Button3_Click);
this.Button4.Click += new System.EventHandler(this.Button4_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 [UserInfo] WHERE [ID]='"+((Label)e.Item.FindControl("NameLabel")).Text+"'";
//定义命令对象
OdbcCommand cmd = new OdbcCommand(sqlcom,conn);
//执行SQL命令
cmd.ExecuteNonQuery();
DataBindGrid();
}
catch
{
}
//关闭连接对象
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 cell3=((TextBox)e.Item.FindControl("Textbox1")).Text;
string cell4=((TextBox)e.Item.FindControl("Textbox2")).Text;
string cell5=((TextBox)e.Item.FindControl("Textbox3")).Text;
string cell6=((TextBox)e.Item.FindControl("Textbox4")).Text;
string cell71=((TextBox)e.Item.FindControl("Textbox5")).Text;
string cell7;
if("True"==cell71)
{
cell7="1";
}
else
{
cell7="0";
}
string sqlcom="UPDATE [UserInfo] set ID='"+cell3+"',name='"+cell4+"',pw='"+cell5+"' ,unit='"+cell6+"',type="+cell7+" WHERE ID='"+cell3+"'";
OdbcConnection conn = new OdbcConnection();
conn.ConnectionString=ConfigurationSettings.AppSettings["oCn_Str"];
//定义命令对象
OdbcCommand cmd = new OdbcCommand(sqlcom,conn);
//打开数据连接
conn.Open();
try
{
//取消编辑
DataGrid1.EditItemIndex=-1;
//执行SQL命令
cmd.ExecuteNonQuery();
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=((Label)(oDataGridItem.FindControl("NameLabel"))).Text;
sqlcom="DELETE [UserInfo] WHERE [ID]='"+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("RoomUserAdd.aspx");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -