ASP.NET中自訂DataGrid分頁設定的實現

來源:互聯網
上載者:User
asp.net|datagrid|分頁

我實現了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();
}
//----------------------翻頁代碼結束
 


 



相關文章

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。