Code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DataGridPaging.aspx.cs" Inherits="DataGridPaging" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>未命名頁面</title>
<style type="text/css">
a{color:black;text-decoration:none ; }
.ffff{text-decoration:none ; color:#c00 ;}
.grid_sel_back
{
background-image:url(Images/gridselback.gif);
background-repeat:repeat-x
}
.title_bar
{
background-image:url(Images/titlebar.gif);
background-repeat:repeat-x
}
</style>
</head>
<body MS_POSITIONING="GridLayout">
<form id="Form1" runat="server">
<asp:datagrid id="MyDataGrid" runat="server" AutoGenerateColumns="true"
HorizontalAlign="Center" AlternatingItemStyle-BackColor="#eeeeee"
HeaderStyle-BackColor="#aaaadd" Font-Size="8pt" Font-Name="Verdana"
CellSpacing="0" CellPadding="3" GridLines="Both" BorderWidth="1"
BorderColor="black" OnPageIndexChanged="MyDataGrid_Page" PagerStyle-HorizontalAlign="Right"
PagerStyle-Mode="NumericPages" PageSize="5" AllowPaging="True">
<AlternatingItemStyle BackColor="#EEEEEE"></AlternatingItemStyle>
<HeaderStyle BackColor="#AAAADD" Font-Bold="True" HorizontalAlign="Center"></HeaderStyle>
<PagerStyle HorizontalAlign="Right" Mode="NumericPages"></PagerStyle>
</asp:datagrid><br />
<p style="FONT-SIZE:9pt" align="center">
<asp:label id="lblPageCount" runat="server"></asp:label>
<asp:label id="lblCurrentIndex" runat="server"></asp:label>
<asp:linkbutton id="btnFirst" onclick="PagerButtonClick" runat="server" Font-Name="verdana"
Font-size="8pt" ForeColor="navy" CommandArgument="0"></asp:linkbutton>
<asp:linkbutton id="btnPrev" onclick="PagerButtonClick" runat="server" Font-Name="verdana"
Font-size="8pt" ForeColor="navy" CommandArgument="prev"></asp:linkbutton>
<asp:linkbutton id="btnNext" onclick="PagerButtonClick" runat="server" Font-Name="verdana"
Font-size="8pt" ForeColor="navy" CommandArgument="next"></asp:linkbutton>
<asp:linkbutton id="btnLast" onclick="PagerButtonClick" runat="server" Font-Name="verdana"
Font-size="8pt" ForeColor="navy" CommandArgument="last"></asp:linkbutton>
<asp:GridView ID="MyDataGrid1" runat="server" HorizontalAlign="Center" AlternatingItemStyle-BackColor="#eeeeee"
HeaderStyle-BackColor="#aaaadd" Font-Size="8pt" Font-Name="Verdana"
CellSpacing="0" CellPadding="3" GridLines="Both" BorderWidth="1"
BorderColor="black" PagerStyle-HorizontalAlign="Right"
PageSize="5" AllowPaging="True" OnPageIndexChanging="MyDataGrid1_PageIndexChanging">
<AlternatingRowStyle BackColor="#EEEEEE"/>
<HeaderStyle BackColor="#AAAADD" Font-Bold="True" HorizontalAlign="Center" CssClass="title_bar "></HeaderStyle>
<PagerStyle HorizontalAlign="Right"></PagerStyle>
<SelectedRowStyle CssClass="grid_sel_back" />
</asp:GridView><br />
<asp:label id="lblPageCount1" runat="server"></asp:label>
<asp:label id="lblCurrentIndex1" runat="server"></asp:label>
<asp:linkbutton id="btnFirst1" onclick="PagerButtonClick1" runat="server" Font-Name="verdana"
Font-size="8pt" ForeColor="navy" CommandArgument="0"></asp:linkbutton>
<asp:linkbutton id="btnPrev1" onclick="PagerButtonClick1" runat="server" Font-Name="verdana"
Font-size="8pt" ForeColor="navy" CommandArgument="prev1"></asp:linkbutton>
<asp:linkbutton id="btnNext1" onclick="PagerButtonClick1" runat="server" Font-Name="verdana"
Font-size="8pt" ForeColor="navy" CommandArgument="next1"></asp:linkbutton>
<asp:linkbutton id="btnLast1" onclick="PagerButtonClick1" runat="server" Font-Name="verdana"
Font-size="8pt" ForeColor="navy" CommandArgument="last1"></asp:linkbutton>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" /><p align="center"
style="font-size: 9pt">
///======================
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="true"
PageSize="3" Width="310px" >
<PagerStyle CssClass="ffff"/>
<HeaderStyle CssClass="grid_sel_back" />
</asp:GridView>
</p>
</form>
</body>
</html>
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DataGridPaging.aspx.cs" Inherits="DataGridPaging" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>未命名頁面</title>
<style type="text/css">
a{color:black;text-decoration:none ; }
.ffff{text-decoration:none ; color:#c00 ;}
.grid_sel_back
{
background-image:url(Images/gridselback.gif);
background-repeat:repeat-x
}
.title_bar
{
background-image:url(Images/titlebar.gif);
background-repeat:repeat-x
}
</style>
</head>
<body MS_POSITIONING="GridLayout">
<form id="Form1" runat="server">
<asp:datagrid id="MyDataGrid" runat="server" AutoGenerateColumns="true"
HorizontalAlign="Center" AlternatingItemStyle-BackColor="#eeeeee"
HeaderStyle-BackColor="#aaaadd" Font-Size="8pt" Font-Name="Verdana"
CellSpacing="0" CellPadding="3" GridLines="Both" BorderWidth="1"
BorderColor="black" OnPageIndexChanged="MyDataGrid_Page" PagerStyle-HorizontalAlign="Right"
PagerStyle-Mode="NumericPages" PageSize="5" AllowPaging="True">
<AlternatingItemStyle BackColor="#EEEEEE"></AlternatingItemStyle>
<HeaderStyle BackColor="#AAAADD" Font-Bold="True" HorizontalAlign="Center"></HeaderStyle>
<PagerStyle HorizontalAlign="Right" Mode="NumericPages"></PagerStyle>
</asp:datagrid><br />
<p style="FONT-SIZE:9pt" align="center">
<asp:label id="lblPageCount" runat="server"></asp:label>
<asp:label id="lblCurrentIndex" runat="server"></asp:label>
<asp:linkbutton id="btnFirst" onclick="PagerButtonClick" runat="server" Font-Name="verdana"
Font-size="8pt" ForeColor="navy" CommandArgument="0"></asp:linkbutton>
<asp:linkbutton id="btnPrev" onclick="PagerButtonClick" runat="server" Font-Name="verdana"
Font-size="8pt" ForeColor="navy" CommandArgument="prev"></asp:linkbutton>
<asp:linkbutton id="btnNext" onclick="PagerButtonClick" runat="server" Font-Name="verdana"
Font-size="8pt" ForeColor="navy" CommandArgument="next"></asp:linkbutton>
<asp:linkbutton id="btnLast" onclick="PagerButtonClick" runat="server" Font-Name="verdana"
Font-size="8pt" ForeColor="navy" CommandArgument="last"></asp:linkbutton>
<asp:GridView ID="MyDataGrid1" runat="server" HorizontalAlign="Center" AlternatingItemStyle-BackColor="#eeeeee"
HeaderStyle-BackColor="#aaaadd" Font-Size="8pt" Font-Name="Verdana"
CellSpacing="0" CellPadding="3" GridLines="Both" BorderWidth="1"
BorderColor="black" PagerStyle-HorizontalAlign="Right"
PageSize="5" AllowPaging="True" OnPageIndexChanging="MyDataGrid1_PageIndexChanging">
<AlternatingRowStyle BackColor="#EEEEEE"/>
<HeaderStyle BackColor="#AAAADD" Font-Bold="True" HorizontalAlign="Center" CssClass="title_bar "></HeaderStyle>
<PagerStyle HorizontalAlign="Right"></PagerStyle>
<SelectedRowStyle CssClass="grid_sel_back" />
</asp:GridView><br />
<asp:label id="lblPageCount1" runat="server"></asp:label>
<asp:label id="lblCurrentIndex1" runat="server"></asp:label>
<asp:linkbutton id="btnFirst1" onclick="PagerButtonClick1" runat="server" Font-Name="verdana"
Font-size="8pt" ForeColor="navy" CommandArgument="0"></asp:linkbutton>
<asp:linkbutton id="btnPrev1" onclick="PagerButtonClick1" runat="server" Font-Name="verdana"
Font-size="8pt" ForeColor="navy" CommandArgument="prev1"></asp:linkbutton>
<asp:linkbutton id="btnNext1" onclick="PagerButtonClick1" runat="server" Font-Name="verdana"
Font-size="8pt" ForeColor="navy" CommandArgument="next1"></asp:linkbutton>
<asp:linkbutton id="btnLast1" onclick="PagerButtonClick1" runat="server" Font-Name="verdana"
Font-size="8pt" ForeColor="navy" CommandArgument="last1"></asp:linkbutton>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" /><p align="center"
style="font-size: 9pt">
///======================
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="true"
PageSize="3" Width="310px" >
<PagerStyle CssClass="ffff"/>
<HeaderStyle CssClass="grid_sel_back" />
</asp:GridView>
</p>
</form>
</body>
</html>
Code
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class DataGridPaging : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
btnFirst.Text = "最首页";
btnPrev.Text = "前一页";
btnNext.Text = "下一页";
btnLast.Text = "最后页";
btnFirst1.Text = "最首页";
btnPrev1.Text = "前一页";
btnNext1.Text = "下一页";
btnLast1.Text = "最后页";
BindGrid();
BindGrid1();
}
private void ShowStats()
{
lblCurrentIndex.Text = "第 " + (MyDataGrid.CurrentPageIndex + 1).ToString() + " 页";
lblPageCount.Text = "总共 " + MyDataGrid.PageCount.ToString() + " 页";
}
public void PagerButtonClick(object sender, EventArgs e)
{
string arg = ((LinkButton)sender).CommandArgument.ToString();
switch (arg)
{
case "next":
if (MyDataGrid.CurrentPageIndex < (MyDataGrid.PageCount - 1))
{
MyDataGrid.CurrentPageIndex += 1;
}
break;
case "prev":
if (MyDataGrid.CurrentPageIndex > 0)
{
MyDataGrid.CurrentPageIndex -= 1;
}
break;
case "last":
MyDataGrid.CurrentPageIndex = (MyDataGrid.PageCount - 1);
break;
default:
MyDataGrid.CurrentPageIndex = System.Convert.ToInt32(arg);
break;
}
BindGrid();
ShowStats();
}
public void BindGrid()
{
SQLHelper sh = new SQLHelper();
DataTable dt = new DataTable();
dt = sh.ExecuteQuery("select * from loginlog ");
MyDataGrid.DataSource = dt;
MyDataGrid.DataBind();
ShowStats();
}
public void MyDataGrid_Page(object sender, DataGridPageChangedEventArgs e)
{
int startIndex;
startIndex = MyDataGrid.CurrentPageIndex * MyDataGrid.PageSize;
MyDataGrid.CurrentPageIndex = e.NewPageIndex;
BindGrid();
ShowStats();
}
//===============================================================================================================
public void PagerButtonClick1(object sender, EventArgs e)
{
string arg = ((LinkButton)sender).CommandArgument.ToString();
switch (arg)
{
case "next1":
if (MyDataGrid1.PageIndex < (MyDataGrid1.PageCount - 1))
{
MyDataGrid1.PageIndex += 1;
}
break;
case "prev1":
if (MyDataGrid1.PageIndex > 0)
{
MyDataGrid1.PageIndex -= 1;
}
break;
case "last1":
MyDataGrid1.PageIndex = (MyDataGrid1.PageCount - 1);
break;
default:
MyDataGrid1.PageIndex = System.Convert.ToInt32(arg);
break;
}
BindGrid1();
ShowStats1();
}
public void BindGrid1()
{
SQLHelper sh = new SQLHelper();
DataTable dt = new DataTable();
dt = sh.ExecuteQuery("select * from loginlog ");
MyDataGrid1.DataSource = dt;
MyDataGrid1.DataBind();
GridView1.DataSource = dt;
GridView1.DataBind();
ShowStats1();
}
private void ShowStats1()
{
lblCurrentIndex1.Text = "第 " + (MyDataGrid1.PageIndex + 1).ToString() + " 页";
lblPageCount1.Text = "总共 " + MyDataGrid1.PageCount.ToString() + " 页";
}
protected void MyDataGrid1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
int startIndex;
startIndex = MyDataGrid1.PageIndex * MyDataGrid1.PageSize;
MyDataGrid1.PageIndex = e.NewPageIndex;
BindGrid1();
ShowStats1();
}
protected void Button1_Click(object sender, EventArgs e)
{
SQLHelper sh = new SQLHelper();
DataTable dt = new DataTable();
dt = sh.ExecuteQuery("select * from loginlog ");
CreatExcel(dt, this);
}
public void CreatExcel(System.Data.DataTable dt, System.Web.UI.Page thisPage)
{
System.IO.StringWriter sw = new System.IO.StringWriter();
string rowStr = "";
//取所有列名
for (int i = 0; i < dt.Columns.Count; i++)
{
rowStr = rowStr + dt.Columns[i] + "\t";
}
sw.WriteLine(rowStr);
//取每行数据
for (int j = 0; j < dt.Rows.Count; j++)
{
rowStr = "";
for (int i = 0; i < dt.Columns.Count; i++)
{
rowStr = rowStr + dt.Rows[j][i].ToString() + "\t";
}
sw.WriteLine(rowStr);
}
sw.Close();
string filename = DateTime.Now.ToString("yyyyMMddhhmmss");
thisPage.Response.AddHeader("Content-Disposition", "attachment;filename=" + filename + ".xls");
thisPage.Response.ContentType = "application/ms-excel";
thisPage.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
thisPage.Response.Write(sw);
thisPage.Response.End();
}
}
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class DataGridPaging : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
btnFirst.Text = "最首页";
btnPrev.Text = "前一页";
btnNext.Text = "下一页";
btnLast.Text = "最后页";
btnFirst1.Text = "最首页";
btnPrev1.Text = "前一页";
btnNext1.Text = "下一页";
btnLast1.Text = "最后页";
BindGrid();
BindGrid1();
}
private void ShowStats()
{
lblCurrentIndex.Text = "第 " + (MyDataGrid.CurrentPageIndex + 1).ToString() + " 页";
lblPageCount.Text = "总共 " + MyDataGrid.PageCount.ToString() + " 页";
}
public void PagerButtonClick(object sender, EventArgs e)
{
string arg = ((LinkButton)sender).CommandArgument.ToString();
switch (arg)
{
case "next":
if (MyDataGrid.CurrentPageIndex < (MyDataGrid.PageCount - 1))
{
MyDataGrid.CurrentPageIndex += 1;
}
break;
case "prev":
if (MyDataGrid.CurrentPageIndex > 0)
{
MyDataGrid.CurrentPageIndex -= 1;
}
break;
case "last":
MyDataGrid.CurrentPageIndex = (MyDataGrid.PageCount - 1);
break;
default:
MyDataGrid.CurrentPageIndex = System.Convert.ToInt32(arg);
break;
}
BindGrid();
ShowStats();
}
public void BindGrid()
{
SQLHelper sh = new SQLHelper();
DataTable dt = new DataTable();
dt = sh.ExecuteQuery("select * from loginlog ");
MyDataGrid.DataSource = dt;
MyDataGrid.DataBind();
ShowStats();
}
public void MyDataGrid_Page(object sender, DataGridPageChangedEventArgs e)
{
int startIndex;
startIndex = MyDataGrid.CurrentPageIndex * MyDataGrid.PageSize;
MyDataGrid.CurrentPageIndex = e.NewPageIndex;
BindGrid();
ShowStats();
}
//===============================================================================================================
public void PagerButtonClick1(object sender, EventArgs e)
{
string arg = ((LinkButton)sender).CommandArgument.ToString();
switch (arg)
{
case "next1":
if (MyDataGrid1.PageIndex < (MyDataGrid1.PageCount - 1))
{
MyDataGrid1.PageIndex += 1;
}
break;
case "prev1":
if (MyDataGrid1.PageIndex > 0)
{
MyDataGrid1.PageIndex -= 1;
}
break;
case "last1":
MyDataGrid1.PageIndex = (MyDataGrid1.PageCount - 1);
break;
default:
MyDataGrid1.PageIndex = System.Convert.ToInt32(arg);
break;
}
BindGrid1();
ShowStats1();
}
public void BindGrid1()
{
SQLHelper sh = new SQLHelper();
DataTable dt = new DataTable();
dt = sh.ExecuteQuery("select * from loginlog ");
MyDataGrid1.DataSource = dt;
MyDataGrid1.DataBind();
GridView1.DataSource = dt;
GridView1.DataBind();
ShowStats1();
}
private void ShowStats1()
{
lblCurrentIndex1.Text = "第 " + (MyDataGrid1.PageIndex + 1).ToString() + " 页";
lblPageCount1.Text = "总共 " + MyDataGrid1.PageCount.ToString() + " 页";
}
protected void MyDataGrid1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
int startIndex;
startIndex = MyDataGrid1.PageIndex * MyDataGrid1.PageSize;
MyDataGrid1.PageIndex = e.NewPageIndex;
BindGrid1();
ShowStats1();
}
protected void Button1_Click(object sender, EventArgs e)
{
SQLHelper sh = new SQLHelper();
DataTable dt = new DataTable();
dt = sh.ExecuteQuery("select * from loginlog ");
CreatExcel(dt, this);
}
public void CreatExcel(System.Data.DataTable dt, System.Web.UI.Page thisPage)
{
System.IO.StringWriter sw = new System.IO.StringWriter();
string rowStr = "";
//取所有列名
for (int i = 0; i < dt.Columns.Count; i++)
{
rowStr = rowStr + dt.Columns[i] + "\t";
}
sw.WriteLine(rowStr);
//取每行数据
for (int j = 0; j < dt.Rows.Count; j++)
{
rowStr = "";
for (int i = 0; i < dt.Columns.Count; i++)
{
rowStr = rowStr + dt.Rows[j][i].ToString() + "\t";
}
sw.WriteLine(rowStr);
}
sw.Close();
string filename = DateTime.Now.ToString("yyyyMMddhhmmss");
thisPage.Response.AddHeader("Content-Disposition", "attachment;filename=" + filename + ".xls");
thisPage.Response.ContentType = "application/ms-excel";
thisPage.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
thisPage.Response.Write(sw);
thisPage.Response.End();
}
}