繁体中文
设为首页
加入收藏
当前位置:ASP技术首页 >> ASP基础 >> Datagrid分页、排序、删除代码

Datagrid分页、排序、删除代码

2006-07-15 08:00:00  作者:  来源:互联网  浏览次数:0  文字大小:【】【】【
简介: WebForm1 window.resizeTo(screen.width,screen.height-30); 首页尾页上一页下一页 代码 default.ASPx.cs using System; using System.Collections; using System.ComponentModel; us...
关键字:排序 Datagrid 代码

<%@ Page language="c#" Codebehind="default.ASPx.cs" AutoEventWireup="false" Inherits="datagrid.WebForm1" %>

WebForm1

HorizontalAlign="Center" DataKeyField="au_id" PageSize="3" AllowPaging="True" Width="408px"

AutoGenerateColumns="False" AllowSorting="True">

首页尾页上一页下一页

代码

default.ASPx.cs

using System;

using System.Collections;

using System.ComponentModel;

using System.Data;

using System.Data.SqlClient;

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 datagrid

{

///

/// WebForm1 的摘要说明。

///

public class WebForm1 : System.Web.UI.Page

{

protected System.Web.UI.WebControls.LinkButton LBtnex;

protected System.Web.UI.WebControls.LinkButton LBtpre;

protected System.Web.UI.WebControls.LinkButton LBtlast;

protected System.Web.UI.WebControls.LinkButton LBtfir;

protected System.Web.UI.WebControls.Label Label1;

protected System.Web.UI.WebControls.DataGrid DataGrid1;

public string SortField;

private void Page_Load(object sender, System.EventArgs e)

{

// 在此处放置用户代码以初始化页面

if(!Page.IsPostBack)

{

databind();

}

}

public void databind()

{

SqlConnection con = new SqlConnection("server=it003;database=pubs;uid=sa;pwd=");

con.Open();

string sql="select * from authors";

SqlDataAdapter ada = new SqlDataAdapter(sql,con);

DataSet ds = new DataSet();

ada.Fill(ds,"temp");

ds.Tables["temp"].DefaultView.Sort=SortField;

DataGrid1.DataSource=ds.Tables["temp"].DefaultView;

DataGrid1.DataBind();

ada.Dispose();

con.Close();

Label1.Text="共"+ds.Tables["temp"].DefaultView.Count.ToString()+"条记录,"+DataGrid1.PageCount.ToString()+"页,第"+(DataGrid1.CurrentPageIndex+1).ToString()+"页,本页"+DataGrid1.Items.Count.ToString()+"条记录";

}

#region Web 窗体设计器生成的代码

override protected void OnInit(EventArgs e)

{

//

// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。

//

InitializeComponent();

base.OnInit(e);

}

///

/// 设计器支持所需的方法 - 不要使用代码编辑器修改

/// 此方法的内容。

///

private void InitializeComponent()

{

this.DataGrid1.SortCommand += new System.Web.UI.WebControls.DataGridSortCommandEventHandler(this.DataGrid1_SortCommand);

this.DataGrid1.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_DeleteCommand_1);

this.DataGrid1.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid1_ItemDataBound_1);

this.LBtfir.Click += new System.EventHandler(this.LBtfir_Click);

this.LBtlast.Click += new System.EventHandler(this.LBtlast_Click);

this.LBtpre.Click += new System.EventHandler(this.LBtpre_Click);

this.LBtnex.Click += new System.EventHandler(this.LBtnex_Click);

this.Load += new System.EventHandler(this.Page_Load);

}

#endregion

private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)

{

DataGrid1.CurrentPageIndex=e.NewPageIndex;

databind();

}

private void LBtnex_Click(object sender, System.EventArgs e)

{

if(DataGrid1.CurrentPageIndex

{

DataGrid1.CurrentPageIndex++;

databind();

}

}

private void LBtpre_Click(object sender, System.EventArgs e)

{

if(DataGrid1.CurrentPageIndex>0)

{

DataGrid1.CurrentPageIndex--;

databind();

}

}

private void LBtfir_Click(object sender, System.EventArgs e)

{

DataGrid1.CurrentPageIndex=0;

databind();

}

private void LBtlast_Click(object sender, System.EventArgs e)

{

DataGrid1.CurrentPageIndex=DataGrid1.PageCount-1;

databind();

}

private void DataGrid1_DeleteCommand_1(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)

{

try{

string ID = (string)DataGrid1.DataKeys[(int) e.Item.ItemIndex];

string sql="delete from authors where au_id='"+Convert.ToString(ID)+"'";

SqlConnection con = new SqlConnection("server=it003;database=pubs;uid=sa;pwd=");

con.Open();

SqlCommand com=new SqlCommand(sql,con);

com.ExecuteNonQuery();

com.Dispose();

con.Close();

if (DataGrid1.Items.Count == 1 && DataGrid1.CurrentPageIndex > 0)

{

DataGrid1.CurrentPageIndex--;

}

databind();

}

catch(Exception ex)

{

Response.Write("");

//Page.RegisterStartupScript("","");

}

}

private void DataGrid1_ItemDataBound_1(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)

{

if(e.Item.ItemIndex<0) return;

e.Item.Attributes.Add("onmouseover","currentcolor=this.style.backgroundColor;this.style.backgroundColor='cccccc'");

e.Item.Attributes.Add("onmouseout","this.style.backgroundColor=currentcolor");

LinkButton lbtnDel = (LinkButton)e.Item.FindControl("delbutton");

string ID = (string)DataGrid1.DataKeys[(int) e.Item.ItemIndex];

lbtnDel.Attributes.Add("onclick", "return confirm('您真的要删除"+ID+ " 行吗?');");

}

private void DataGrid1_SortCommand(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)

{

SortField=(string)e.SortExpression;

databind();

}

}

}

责任编辑:admin
相关文章