這裡我實現了DataGrid的個性化分頁,現在把代碼貼出來朋友們參考,同時在這個功能的實現過程,參考了“飛刀”從國外翻譯的一篇文章。
先定義ASPX頁面,注意AllowCustomPaging要設為"False":
<body>
<form id="DictList" method="post" runat="server">
<TABLE style="BORDER-COLLAPSE: collapse" cellSpacing="0" width="100%" border="1">
<TR>
<td bgColor="#c0c000">信息:<FONT face="Arial" color="#ffffff">數據維護</FONT>
</td>
</TR>
<tr>
<td><FONT face="宋體"></FONT></td>
</tr>
<tr>
<td><asp:datagrid id="MyDataGrid" runat="server" Width="100%" PageSize="20" AllowPaging="True" AutoGenerateColumns="False" DataKeyField="FDictid">
<SelectedItemStyle BackColor="#FFC080"></SelectedItemStyle>
<HeaderStyle BackColor="#C0C000"></HeaderStyle>
<Columns>
<asp:ButtonColumn Text="選擇" HeaderText="選擇" CommandName="Select">
<HeaderStyle Font-Bold="True" HorizontalAlign="Center" Width="8%"></HeaderStyle>
<ItemStyle Font-Bold="True" HorizontalAlign="Center"></ItemStyle>
</asp:ButtonColumn>
<asp:BoundColumn DataField="FDictID" SortExpression="FDictID asc" HeaderText="標識號">
<HeaderStyle Width="15%"></HeaderStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="FNameCn" SortExpression="FNameCn asc" HeaderText="名稱">
<HeaderStyle Width="15%"></HeaderStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="FNameEn" SortExpression="FNameEn asc" HeaderText="英文名稱">
<HeaderStyle Width="15%"></HeaderStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="FNote" SortExpression="FNote asc" HeaderText="描敘">
<HeaderStyle Width="47%"></HeaderStyle>
</asp:BoundColumn>
</Columns>
<PagerStyle Visible="False"></PagerStyle>
</asp:datagrid></td>
</tr>
</TABLE>
<TABLE style="BORDER-COLLAPSE: collapse" cellSpacing="0" width="100%" bgColor="#ff9966" border="1">
<TR>
<td align="right"><asp:linkbutton id="btnFirst" runat="server" CommandArgument="fist">首頁</asp:linkbutton>
<asp:linkbutton id="btnPrev" runat="server" Width="36px" CommandArgument="prev">上一頁</asp:linkbutton>
<asp:linkbutton id="btnNext" runat="server" CommandArgument="next">下一頁</asp:linkbutton>
<asp:linkbutton id="btnLast" runat="server" CommandArgument="last">末頁</asp:linkbutton>
<asp:label id="lblCurrentIndex" runat="server"></asp:label>/<asp:label id="lblPageCount" runat="server"></asp:label>
跳轉到<asp:TextBox id="txtGoPage" runat="server" Width="30px" CssClass="textbox"></asp:TextBox>
<asp:Button id="btnGo" runat="server" Text="GO" CssClass="button" Width="29px"></asp:Button></td>
</TR>
</TABLE>
</form>
codebehind主要功能部分代碼:
private void Page_Load(object sender, System.EventArgs e)
{
// 在此處放置用戶代碼以初始化頁面
myConnection = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["ConnString"]);
if (!IsPostBack)
BindGrid();
}
public void BindGrid()
{
string strSql ="SELECT * FROM t_dict ";
SqlDataAdapter myCommand = new SqlDataAdapter(strSql, myConnection);
DataSet ds = new DataSet();
myCommand.Fill(ds, "t_dict");
MyDataGrid.DataSource=ds.Tables["t_dict"].DefaultView;
MyDataGrid.DataBind();
ShowStatsPage();
}
private void PagerButtonClick(object sender, System.EventArgs e)
{
//獲得LinkButton的參數值
String arg = ((LinkButton)sender).CommandArgument;
switch(arg)
{
case ("next"):
if (MyDataGrid.CurrentPageIndex < (MyDataGrid.PageCount - 1))
MyDataGrid.CurrentPageIndex ++;
break;
case ("prev"):
if (MyDataGrid.CurrentPageIndex > 0)
MyDataGrid.CurrentPageIndex --;
break;
case ("fist"):
MyDataGrid.CurrentPageIndex=0;
break;
case ("last"):
MyDataGrid.CurrentPageIndex = (MyDataGrid.PageCount - 1);
break;
default:
//本頁值
MyDataGrid.CurrentPageIndex = Convert.ToInt32(arg);
break;
}
BindGrid();
}
void ShowStatsPage()
{
//顯示頁面信息
lblCurrentIndex.Text = "[<font color="blue">當前為第:" + ((int)MyDataGrid.CurrentPageIndex+1) + "頁</font>]";
lblPageCount.Text = "[<font color="blue">共:" + MyDataGrid.PageCount + "頁</font>]";
}
private void MyDataGrid_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
//處理按下數字的方法
MyDataGrid.CurrentPageIndex = e.NewPageIndex;
BindGrid();
}
private void btnGo_Click(object sender, System.EventArgs e)
{
//頁面直接跳轉的代碼
if(txtGoPage.Text.Trim()!="")
{
int PageI=Int32.Parse(txtGoPage.Text.Trim())-1;
if (PageI >=0 && PageI < (MyDataGrid.PageCount))
MyDataGrid.CurrentPageIndex = PageI ;
}
BindGrid();
}
//----------------------翻頁代碼結束