📄 manage.aspx.cs
字号:
//-----------------------------------------------------------------------
//
// WebExpert.NET 1.0
//
// (c) 2003, www.AspCool.com. All rights reserved.
// ASP酷技术网 版权所有
//
// 该源码下载自:http://www.51aspx.com
// 邮箱:tim@aspcool.com
//
// 版权声明:本程序仅供学习使用,你也可以修改后在网站上使用,但使用时必
// 须保留ASP酷技术网(www.AspCool.com)的版权信息和链接。本程序随《ASP.NET
// 网站建设专家》一书赠送,未经作者同意,不得随意修改、传播。
//
// 描述:
// 此文件包含下面的类:
// Manage
//
// 作者: 王保健
// 时间: 2005-01-15
//
//------------------------------------------------------------------------
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Configuration;
namespace AspCool.WebExpert
{
/// <summary>
/// Summary description for Manage.
/// </summary>
public class Manage : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid DataGrid1;
private void Page_Load(object sender, System.EventArgs e)
{
if (!this.IsPostBack)
{
Data_Bind();
}
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.DataGrid1.ItemCreated += new System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid1_ItemCreated);
this.DataGrid1.SelectedIndexChanged += new System.EventHandler(this.DataGrid1_SelectedIndexChanged);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
protected void DG_Edit(Object sender, DataGridCommandEventArgs E)
{
// 设置 DataGrid 控件中要编辑的项的索引。
DataGrid1.EditItemIndex=(int)E.Item.ItemIndex;
// 数据绑定
Data_Bind();
}
protected void DG_Delete(Object sender, DataGridCommandEventArgs E)
{
//创建数据库连接
SqlConnection conn=new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
conn.Open(); //打开数据库连接
//删除电子邮件地址的SQL语句
string sSQL="delete newsletters where itemId="+DataGrid1.DataKeys[(int)E.Item.ItemIndex ];
// 定义SQL命令
SqlCommand comm=new SqlCommand(sSQL,conn);
// 执行SQL命令
comm.ExecuteNonQuery();
// 关闭数据库连接
conn.Close();
//数据绑定
Data_Bind();
}
protected void DG__PageIndexChanged(Object sender, DataGridPageChangedEventArgs E)
{
DataGrid1.CurrentPageIndex=E.NewPageIndex ;
Data_Bind();
}
protected void DG_Update(Object sender, DataGridCommandEventArgs E)
{
//创建数据库连接
SqlConnection conn=new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
conn.Open(); //打开数据库连接
// 从DataGrid中获取电子邮件地址所在的文本框
TextBox txtEmail=(TextBox)E.Item.Cells[1].Controls[0];
// 修改电子邮件地址的SQL语句
string sSQL="update newsletters set Email='"+txtEmail.Text +"' where itemId="+DataGrid1.DataKeys[(int)E.Item.ItemIndex ];
// 定义SQL命令
SqlCommand comm=new SqlCommand(sSQL,conn);
// 执行SQL命令
comm.ExecuteNonQuery();
// 关闭数据库连接
conn.Close();
// 返回到显示页面
DataGrid1.EditItemIndex=-1;
// 数据绑定
Data_Bind();
}
protected void DG_Cancel(Object sender, DataGridCommandEventArgs E)
{
// 设置 DataGrid 控件中要编辑的项的索引为-1。
DataGrid1.EditItemIndex=-1;
// 数据绑定
Data_Bind();
}
private void Data_Bind()
{
//创建数据库连接
SqlConnection myConnection = new SqlConnection( ConfigurationSettings.AppSettings ["ConnectionString"]);
myConnection.Open(); //打开数据库连接
//显示电子邮件信息的SQL语句
string sSql="select itemId,Email,CreatedTime from newsletters order by itemId desc";
SqlDataAdapter adapter=new SqlDataAdapter(sSql,myConnection);
//定义DataSet
DataSet ds=new DataSet();
//填充DataSet
adapter.Fill(ds,"newsletters");
//设定数据源
DataGrid1.DataSource=ds.Tables["newsletters"].DefaultView;
//数据绑定
DataGrid1.DataBind();
//关闭数据库连接
myConnection.Close();
}
private void DataGrid1_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
//枚举该行中的每个单元格
foreach(TableCell tc in e.Item.Cells)
{
//检查每个单元格中的控件数
if(tc.Controls.Count>0)
{
//得到每个控件
foreach(Control con in tc.Controls)
{
//检查每个控件,看是否是DataGridLinkButton
if(con.ToString()=="System.Web.UI.WebControls.DataGridLinkButton")
{
LinkButton lb=(LinkButton)con;
if(lb.CommandName=="Delete")
{
lb.Attributes.Add("onclick", "return confirm('您真的要删除这个电子邮件地址吗?')");
}
}
}
}
}
}
private void DataGrid1_SelectedIndexChanged(object sender, System.EventArgs e)
{
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -