📄 stmanagerdep.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.SqlClient;
using System.Configuration;
namespace STcontract.Manager
{
/// <summary>
/// ManagerDep 的摘要说明。
/// </summary>
public class ManagerDep : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid STmyGrid;
SqlConnection STconn;
private void Page_Load(object sender, System.EventArgs e)
{
if(Session["STNickName"] != null)
{
//获得数据库连接字符串
string STconnection = ConfigurationSettings.AppSettings["strconnection"];
STconn = new SqlConnection(STconnection);
if(!IsPostBack)
{
//自定义BindGrid ()方法,加载连锁店的信息
BindGrid();
}
}
else
{
Response.Redirect("../Index.aspx");
}
}
//加载连锁店的信息
public void BindGrid()
{
//创建查询连锁店信息的sql语句
string STstrsql="select STDepID,STDepName,STDepMaster,STDepInfo from STDep";
//创建SqlDataAdapter的实例
SqlDataAdapter STda=new SqlDataAdapter(STstrsql,STconn);
//创建DataSet的实例
DataSet STds=new DataSet();
//将信息填充到DataSet
STda.Fill(STds);
//获取DataSet的数据源
STmyGrid.DataSource = STds;
//将信息绑定到DataGrid
STmyGrid.DataBind();
}
//处理分页
public void STmyGrid_Page(object sender,DataGridPageChangedEventArgs e)
{
//获取DataGrid的当前显示页的索引
STmyGrid.CurrentPageIndex=e.NewPageIndex;
BindGrid();
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.STmyGrid.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.STmyGrid_cancel);
this.STmyGrid.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.STmyGrid_edit);
this.STmyGrid.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.STmyGrid_update);
this.STmyGrid.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.STmyGrid_delete);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
//点击取消所触发的事件
public void STmyGrid_cancel(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
//设置DataGrid控件中要编辑的项的索引为-1
STmyGrid.EditItemIndex=-1;
BindGrid();
}
//点击删除所触发的事件
public void STmyGrid_delete(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
//创建删除指定ID连锁店的sql语句
string STstrsql = "delete from STDep where STDepID = @STDepID";
//创建数据库的SqlCommand对象
SqlCommand STcmd = new SqlCommand(STstrsql,STconn);
//向SqlCommand对象添加参数
STcmd.Parameters.Add(new SqlParameter("@STDepID",SqlDbType.Int,4));
//向参数赋值
STcmd.Parameters["@STDepID"].Value = STmyGrid.DataKeys[(int)e.Item.ItemIndex];
STcmd.Connection.Open();
try
{
//执行sql语句
STcmd.ExecuteNonQuery();
}
catch(SqlException)
{
}
STcmd.Connection.Close();
BindGrid();
}
//点击编辑所触发的事件
public void STmyGrid_edit(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
//获取DataGrid所要编辑的项的索引
STmyGrid.EditItemIndex=(int)e.Item.ItemIndex;
BindGrid();
}
//点击更新所触发的事件
public void STmyGrid_update(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
//创建更新连锁店信息的sql语句
string STstrsql="update STDep set STDepName = @STDepName , STDepMaster = @STDepMaster , STDepInfo = @STDepInfo where STDepID = @STDepID";
//创建数据库的SqlCommand对象
SqlCommand STcmd=new SqlCommand(STstrsql,STconn);
try
{
//向SqlCommand对象添加参数
STcmd.Parameters.Add(new SqlParameter("@STDepID",SqlDbType.Int,4));
STcmd.Parameters.Add(new SqlParameter("@STDepName",SqlDbType.VarChar,50));
STcmd.Parameters.Add(new SqlParameter("@STDepMaster",SqlDbType.VarChar,50));
STcmd.Parameters.Add(new SqlParameter("@STDepInfo",SqlDbType.VarChar,200));
string stupdatetext=((TextBox)e.Item.Cells[1].Controls[0]).Text ;
//向参数赋值
STcmd.Parameters["@STDepName"].Value=stupdatetext;
stupdatetext=((TextBox)e.Item.Cells[2].Controls[0]).Text;
STcmd.Parameters["@STDepMaster"].Value=stupdatetext;
stupdatetext=((TextBox)e.Item.Cells[3].Controls[0]).Text;
STcmd.Parameters["@STDepInfo"].Value=stupdatetext;
STcmd.Parameters["@STDepID"].Value=STmyGrid.DataKeys[(int)e.Item.ItemIndex];
STcmd.Connection.Open();
//执行sql语句
STcmd.ExecuteNonQuery();
STmyGrid.EditItemIndex=-1;
}
catch(SqlException ex)
{
throw ex;
}
STcmd.Connection.Close();
BindGrid();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -