asp.net

位置:IT落伍者 >> asp.net >> 浏览文章

ASP.NET中自定义DataGrid分页设置的实现


发布日期:2020年02月10日
 
ASP.NET中自定义DataGrid分页设置的实现

我实现了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();

}

//翻页代码结束

               

上一篇:从零开始学ASP.NET(基础篇 连载六)

下一篇:ASP.NET里的事务处理