<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>mingxiguanli</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
<style type="text/css">BODY { BACKGROUND-COLOR: #def3ff }
</style>
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<FONT face="宋体">
<asp:datagrid id="dgAddDelConfirm" style="Z-INDEX: 101; LEFT: 16px; POSITION: absolute; TOP: 56px"
runat="server" AllowSorting="True" ShowFooter="True" AutoGenerateColumns="False" Width="816px"
Height="176px" BorderColor="#3366CC" BorderStyle="None" BorderWidth="1px" BackColor="White"
CellPadding="4" AllowCustomPaging="True" PageSize="18" AllowPaging="True">
<FooterStyle ForeColor="#003399" BackColor="#99CCCC"></FooterStyle>
<SelectedItemStyle Font-Bold="True" ForeColor="#CCFF99" BackColor="#009999"></SelectedItemStyle>
<ItemStyle ForeColor="#003399" BackColor="White"></ItemStyle>
<HeaderStyle Font-Bold="True" ForeColor="#CCCCFF" BackColor="#003399"></HeaderStyle>
<Columns>
<asp:BoundColumn DataField="zhigongming" HeaderText="职工姓名"></asp:BoundColumn>
<asp:BoundColumn DataField="suoshubumen" HeaderText="所属部门"></asp:BoundColumn>
<asp:BoundColumn DataField="zhanghao" HeaderText="账号"></asp:BoundColumn>
<asp:BoundColumn DataField="jine" HeaderText="金额"></asp:BoundColumn>
<asp:BoundColumn DataField="leixing" HeaderText="类型"></asp:BoundColumn>
<asp:BoundColumn DataField="dakehu" HeaderText="客户类型"></asp:BoundColumn>
<asp:BoundColumn DataField="cunruriqi" HeaderText="存入日期"></asp:BoundColumn>
<asp:BoundColumn DataField="lururiqi" HeaderText="录入日期"></asp:BoundColumn>
<asp:ButtonColumn Text="删除" CommandName="Delete"></asp:ButtonColumn>
</Columns>
<PagerStyle HorizontalAlign="Left" ForeColor="#003399" BackColor="#99CCCC" Mode="NumericPages"></PagerStyle>
</asp:datagrid>
<asp:TextBox id="TextBox1" style="Z-INDEX: 102; LEFT: 16px; POSITION: absolute; TOP: 16px" runat="server"></asp:TextBox>
<asp:Button id="Button1" style="Z-INDEX: 103; LEFT: 208px; POSITION: absolute; TOP: 16px" runat="server"
Text="Button"></asp:Button></FONT></form>
</body>
</HTML>
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 lanchuxitong
{
/**//// <summary>
/// mingxiguanli 的摘要说明。
/// </summary>
public class mingxiguanli : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid dgAddDelConfirm;
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.Button Button1;
//定义全局变量用来保存每页的起始项索引
int startIndex=0;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(!IsPostBack)
DataGridDataBind();
}
// ItemDataBound事件
private void changeRowColor(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
//如果是数据项并且是交替项
if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
//添加自定义属性,当鼠标移过来时设置该行的背景色为"6699ff",并保存原背景色
e.Item.Attributes.Add("onmouseover","currentcolor=this.style.backgroundColor;this.style.backgroundColor='#6699ff'");
//添加自定义属性,当鼠标移走时还原该行的背景色
e.Item.Attributes.Add("onmouseout","this.style.backgroundColor=currentcolor");
}
}
Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/**//// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.dgAddDelConfirm.ItemCreated += new System.Web.UI.WebControls.DataGridItemEventHandler(this.dgAddDelConfirm_ItemCreated);
this.dgAddDelConfirm.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.dgAddDelConfirm_PageIndexChanged);
this.dgAddDelConfirm.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dg_DeleteCommand);
this.dgAddDelConfirm.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.changeRowColor);
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void dg_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
//输出信息
Response.Write("删除");
}
private void DataGridDataBind()
{
//定义数据连接对象,其中数据库连接字符串是在Web.Config文件中定义的
SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionSqlServer"].ToString());
//创建数据适配器对象
SqlDataAdapter da = new SqlDataAdapter("select zhigongming,suoshubumen,chuhuzhanghao,lanchujine,lanchuleixing,dakehu,cunruriqi,lururiqi from lanchudengji",conn);
//创建DataSet对象
DataSet ds = new DataSet();
try
{
//从指定的索引开始取PageSize条记录
da.Fill(ds,startIndex,dgAddDelConfirm.PageSize,"CurDataTable");
//填充数据集
da.Fill(ds,"AllDataTable");
//设置DataGrid控件实际要显示的项数
dgAddDelConfirm.VirtualItemCount = ds.Tables["AllDataTable"].Rows.Count;
//进行数据绑定
dgAddDelConfirm.DataSource = ds.Tables["CurDataTable"];
dgAddDelConfirm.DataBind();
}
catch(Exception error)
{
//输出异常信息
Response.Write(error.ToString());
}
}
private void dgAddDelConfirm_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
//判断如果是DataGrid控件中的项、交替项以及编辑项
if(e.Item.ItemType==ListItemType.Item||e.Item.ItemType==ListItemType.AlternatingItem || e.Item.ItemType==ListItemType.EditItem)
{
TableCell myTableCell;
//定义对“删除”单元格的引用
myTableCell = e.Item.Cells[8];
//定义对“删除”单元格中控件的引用
LinkButton btnDel = (LinkButton)myTableCell.Controls[0];
//添加当单击时弹出确认对话框的自定义属性
btnDel.Attributes.Add("onclick", "return confirm('您真的要删除此行吗?');");
btnDel.Text = "删除";
}
}
private void dgAddDelConfirm_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
//设置DataGrid当前页的索引值为用户选择的页的索引
dgAddDelConfirm.CurrentPageIndex = e.NewPageIndex;
//取得当前页为止总共有多少条记录,以便在下一页就从该记录开始读取
startIndex = dgAddDelConfirm.PageSize * dgAddDelConfirm.CurrentPageIndex;
//重新绑定数据
DataGridDataBind();
}
}
}