📄 pagesortindexcode.aspx.cs
字号:
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;
using System.Drawing;
public partial class PageSortIndexCode : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//设置GridView属性
gviewProducts.AllowPaging = true; //设置分页
gviewProducts.AllowSorting = true; //设置排序
gviewProducts.Font.Size = 10; //设置字号大小
gviewProducts.GridLines = GridLines.Both; //设置网格线
gviewProducts.PageSize = 5;
//非同步Callback模式
gviewProducts.EnableSortingAndPagingCallbacks = false;
//分页位置
gviewProducts.PagerSettings.Position = PagerPosition.TopAndBottom;
//分页对齐
gviewProducts.PagerStyle.HorizontalAlign = HorizontalAlign.Center;
gviewProducts.HeaderStyle.BackColor = Color.Tan;
gviewProducts.RowStyle.BackColor = Color.LightGoldenrodYellow;
gviewProducts.AlternatingRowStyle.BackColor = Color.PaleGoldenrod;
gviewProducts.HeaderStyle.ForeColor = Color.Black;
gviewProducts.PagerStyle.BackColor = Color.Goldenrod;
ViewState["Style"] = "0";
}
//设置SqlDataSource连接及Select命令
sqldsProducts.ConnectionString = ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString;
sqldsProducts.SelectCommand = "select * from Products";
//设置GridView数据源ID
gviewProducts.DataSourceID = sqldsProducts.ID;
}
//显示分页页码
private void DisplayPageNo()
{
GridViewRow bottonPagerRow = gviewProducts.BottomPagerRow;
Label bottonPagerNo = new Label();
bottonPagerNo.Text = "目前所在页码(" + (gviewProducts.PageIndex + 1) + "/" + gviewProducts.PageCount + ")";
bottonPagerRow.Cells[0].Controls.Add(bottonPagerNo);
Literal myLiteral = new Literal();
myLiteral.Text = "<BR/>";
bottonPagerRow.Cells[0].Controls.Add(myLiteral);
//动态创建分页按钮
for (int i = 0; i < gviewProducts.PageCount; i++)
{
LinkButton PageNo = new LinkButton();
PageNo.Text = Convert.ToString(i + 1);
PageNo.ID = (PageNo + i.ToString());
PageNo.CommandArgument = i.ToString();
PageNo.Click += new EventHandler(PageNo_Click);
bottonPagerRow.Cells[0].Controls.Add(PageNo);
Literal blank = new Literal();
blank.Text = " ";
bottonPagerRow.Cells[0].Controls.Add(blank);
}
}
void PageNo_Click(object sender, EventArgs e)
{
gviewProducts.PageIndex = Convert.ToInt16(((LinkButton)sender).CommandArgument);
}
//设置排序相关功能
protected void gviewProducts_Sorted(object sender, EventArgs e)
{
switch (gviewProducts.SortDirection)
{
//设置升幂外观样式
case SortDirection.Ascending:
ViewState["Style"] = "0";
break;
//设置降幂外观样式
case SortDirection.Descending:
ViewState["Style"] = "1";
break;
}
}
protected void gviewProducts_DataBound(object sender, EventArgs e)
{
DisplayPageNo();
checkStyle();
}
protected void gviewProducts_PageIndexChanged(object sender, EventArgs e)
{
checkStyle();
}
protected void checkStyle()
{
if (ViewState["Style"].ToString() == "0")
{
gviewStyle1();
ViewState["Style"] = "1";
}
else
{
gviewStyle2();
ViewState["Style"] = "0";
}
}
//GridView样式一
protected void gviewStyle1()
{
gviewProducts.HeaderStyle.BackColor = Color.Tan;
gviewProducts.RowStyle.BackColor = Color.LightGoldenrodYellow;
gviewProducts.AlternatingRowStyle.BackColor = Color.PaleGoldenrod;
gviewProducts.HeaderStyle.ForeColor = Color.Black;
gviewProducts.PagerStyle.BackColor = Color.Goldenrod;
}
//GridView样式二
protected void gviewStyle2()
{
gviewProducts.HeaderStyle.BackColor = Color.MidnightBlue;
gviewProducts.RowStyle.BackColor = Color.LightBlue;
gviewProducts.AlternatingRowStyle.BackColor = Color.Lavender;
gviewProducts.HeaderStyle.ForeColor = Color.White;
gviewProducts.PagerStyle.BackColor = Color.LightPink;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -