datagrid|分页|排序<% 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" 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" 106; LEFT: 312px; POSITION: absolute; TOP: 24px" runat="server"></asp:label><asp:linkbutton id="LBtfir" 105; LEFT: 72px; POSITION: absolute; TOP: 24px" runat="server">首页</asp:linkbutton><asp:linkbutton id="LBtlast" 104; LEFT: 256px; POSITION: absolute; TOP: 24px" runat="server">尾页</asp:linkbutton><asp:linkbutton id="LBtpre" 103; LEFT: 192px; POSITION: absolute; TOP: 24px" runat="server">上一页</asp:linkbutton><asp:linkbutton id="LBtnex" 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.Di
Datagrid分页、排序、删除代码
80酷酷网 80kuku.com