熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

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

2013-11-13 12:21:23  來源: .NET編程 

  我實現了DataGrid的個性化分頁現在把代碼貼出來朋友們參考同時在這個功能的實現過程參考了飛刀從國外翻譯的一篇文章

  先定義ASPX頁面注意AllowCustomPaging要設為False

<body>
<form id=DictList method=post runat=server
<TABLE style=BORDERCOLLAPSE: collapse cellSpacing= width=% border=
<TR>
<td bgColor=#cc>信息<FONT face=Arial color=#ffffff>數據維護</FONT>
</td>
</TR>
<tr>
<td><FONT face=宋體></FONT></td>
</tr>
<tr>
<td><asp:datagrid id=MyDataGrid runat=server Width=% PageSize= AllowPaging=True AutoGenerateColumns=False DataKeyField=FDictid
<SelectedItemStyle BackColor=#FFC></SelectedItemStyle>
<HeaderStyle BackColor=#CC></HeaderStyle>
<Columns>
<asp:ButtonColumn Text=選擇 HeaderText=選擇 CommandName=Select
<HeaderStyle FontBold=True HorizontalAlign=Center Width=%></HeaderStyle>
<ItemStyle FontBold=True HorizontalAlign=Center></ItemStyle>
</asp:ButtonColumn>
<asp:BoundColumn DataField=FDictID SortExpression=FDictID asc HeaderText=標識號
<HeaderStyle Width=%></HeaderStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField=FNameCn SortExpression=FNameCn asc HeaderText=名稱
<HeaderStyle Width=%></HeaderStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField=FNameEn SortExpression=FNameEn asc HeaderText=英文名稱
<HeaderStyle Width=%></HeaderStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField=FNote SortExpression=FNote asc HeaderText=描敘
<HeaderStyle Width=%></HeaderStyle>
</asp:BoundColumn>
</Columns>
<PagerStyle Visible=False></PagerStyle>
</asp:datagrid></td>
</tr>
</TABLE>
<TABLE style=BORDERCOLLAPSE: collapse cellSpacing= width=% bgColor=#ff border=
<TR>
<td align=right><asp:linkbutton id=btnFirst runat=server CommandArgument=fist>首頁</asp:linkbutton>  
<asp:linkbutton id=btnPrev runat=server Width=px 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=px CssClass=textbox></asp:TextBox>
<asp:Button id=btnGo runat=server Text=GO CssClass=button Width=px></asp:Button></td>
</TR>
</TABLE>
</form>

  codebehind主要功能部分代碼

private void Page_Load(object sender SystemEventArgs e)
{
// 在此處放置用戶代碼以初始化頁面
myConnection = new SqlConnection(SystemConfigurationConfigurationSettingsAppSettings[ConnString]);

if (!IsPostBack)
BindGrid();
}

public void BindGrid()
{
string strSql =SELECT * FROM t_dict ;
SqlDataAdapter myCommand = new SqlDataAdapter(strSql myConnection);
DataSet ds = new DataSet();
myCommandFill(ds t_dict);
MyDataGridDataSource=dsTables[t_dict]DefaultView;
MyDataGridDataBind();
ShowStatsPage();
}

private void PagerButtonClick(object sender SystemEventArgs e)
{
//獲得LinkButton的參數值
String arg = ((LinkButton)sender)CommandArgument;

switch(arg)
{
case (next):
if (MyDataGridCurrentPageIndex < (MyDataGridPageCount ))
MyDataGridCurrentPageIndex ++;
break;
case (prev):
if (MyDataGridCurrentPageIndex > )
MyDataGridCurrentPageIndex ;
break;
case (fist):
MyDataGridCurrentPageIndex=;
break;
case (last):
MyDataGridCurrentPageIndex = (MyDataGridPageCount );
break;
default:
//本頁值
MyDataGridCurrentPageIndex = ConvertToInt(arg);
break;
}
BindGrid();
}

void ShowStatsPage()
{
//顯示頁面信息
lblCurrentIndexText = [<font color=blue>當前為第 + ((int)MyDataGridCurrentPageIndex+) + 頁</font>];
lblPageCountText = [<font color=blue>共 + MyDataGridPageCount + 頁</font>];
}

private void MyDataGrid_PageIndexChanged(object source SystemWebUIWebControlsDataGridPageChangedEventArgs e)
{
//處理按下數字的方法
MyDataGridCurrentPageIndex = eNewPageIndex;
BindGrid();
}

private void btnGo_Click(object sender SystemEventArgs e)
{
//頁面直接跳轉的代碼
if(txtGoPageTextTrim()!=)
{
int PageI=IntParse(txtGoPageTextTrim());
if (PageI >= && PageI < (MyDataGridPageCount))
MyDataGridCurrentPageIndex = PageI ;
}
BindGrid();
}
//翻頁代碼結束


From:http://tw.wingwit.com/Article/program/net/201311/15732.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.