分享
 
 
 

典型Datagrid分页、排序、删除代码

王朝asp·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

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

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >

<HTML>

<HEAD>

<title>WebForm1</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">

</HEAD>

<body MS_POSITIONING="GridLayout">

<form id="Form1" method="post" runat="server">

<script>

window.resizeTo(screen.width,screen.height-30);

</script>

<asp:datagrid id="DataGrid1" style="Z-INDEX: 101; LEFT: 48px; POSITION: absolute; TOP: 56px" runat="server"

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

AutoGenerateColumns="False" AllowSorting="True">

<Columns>

<asp:BoundColumn DataField="au_id" SortExpression="au_id" ReadOnly="True" HeaderText="ID"></asp:BoundColumn>

<asp:BoundColumn DataField="city" SortExpression="city" ReadOnly="True" HeaderText="城市"></asp:BoundColumn>

<asp:TemplateColumn SortExpression="au_id" HeaderText="操作">

<ItemTemplate>

<asp:LinkButton runat="server" ID="delbutton" Text="删除" CommandName="Delete" CausesValidation="false"></asp:LinkButton>

</ItemTemplate>

</asp:TemplateColumn>

</Columns>

<PagerStyle Visible="False" PageButtonCount="6"></PagerStyle>

</asp:datagrid><asp:label id="Label1" style="Z-INDEX: 106; LEFT: 312px; POSITION: absolute; TOP: 24px" runat="server"></asp:label><asp:linkbutton id="LBtfir" style="Z-INDEX: 105; LEFT: 72px; POSITION: absolute; TOP: 24px" runat="server">首页</asp:linkbutton><asp:linkbutton id="LBtlast" style="Z-INDEX: 104; LEFT: 256px; POSITION: absolute; TOP: 24px" runat="server">尾页</asp:linkbutton><asp:linkbutton id="LBtpre" style="Z-INDEX: 103; LEFT: 192px; POSITION: absolute; TOP: 24px" runat="server">上一页</asp:linkbutton><asp:linkbutton id="LBtnex" style="Z-INDEX: 102; LEFT: 120px; POSITION: absolute; TOP: 24px" runat="server">下一页</asp:linkbutton></form>

</body>

</HTML>

代码

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

{

/// <summary>

/// WebForm1 的摘要说明。

/// </summary>

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);

}

/// <summary>

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

/// 此方法的内容。

/// </summary>

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.PageCount-1)

{

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("<script>alert('"+ex.Message+"')</script>");

//Page.RegisterStartupScript("","<script>alert('"+ex.Message+"')</script>");

}

}

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();

}

}

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有