分页列表使用
前台代码:
<
div
>
<
asp:Repeater ID
=
"
rptUserList
"
runat
=
"
server
"
>
<
HeaderTemplate
>
<
table
>
<
tr
>
<
td
>
<
input type
=
"
checkbox
"
id
=
"
chkAll
"
runat
=
"
server
"
name
=
"
全选/不选
"
onclick
=
"
checkAll(this);
"
/></
td
>
<
td
>
用户id
</
td
>
<
td
>
用户名
</
td
>
<
td
>
用户性别
</
td
>
<
td
>
用户权限组
</
td
>
<
td
>
删除状态
</
td
>
<
td
>
删除用户
</
td
>
</
tr
>
</
HeaderTemplate
>
<
ItemTemplate
>
<
tr
>
<
td
>
<
asp:CheckBox ID
=
"
chkSel
"
runat
=
"
server
"
/></
td
>
<
td
>
<%
# Eval(
"
Id
"
)
%>
</
td
>
<
td
>
<%
# Eval(
"
UserName
"
)
%>
</
td
>
<
td
>
<%
# Eval(
"
Gender
"
)
%>
</
td
>
<
td
>
<%
# Eval(
"
GroupId
"
)
%>
</
td
>
<
td
>
<%
# Eval(
"
IsDelete
"
)
%>
</
td
>
<
td
>
<
asp:ImageButton ID
=
"
btnToWishList
"
runat
=
"
server
"
AlternateText
=
"
Move to wish list
"
CausesValidation
=
"
false
"
CommandArgument
=
'
<%# Eval("Id") %>
'
CommandName
=
"
del
"
ImageUrl
=
"
~/Comm_Images/button-wishlist.gif
"
ToolTip
=
"
Deltet sh list
"
/>
</
td
>
</
tr
>
</
ItemTemplate
>
<
FooterTemplate
>
</
table
>
</
FooterTemplate
>
</
asp:Repeater
>
<
webdiyer:AspNetPager ID
=
"
AspNetPager2
"
runat
=
"
server
"
Width
=
"
100%
"
UrlPaging
=
"
True
"
SubmitButtonText
=
"
跳 转
"
AlwaysShow
=
"
True
"
ShowCustomInfoSection
=
"
Left
"
NumericButtonTextFormatString
=
"
[{0}]
"
PagingButtonSpacing
=
"
4px
"
SubmitButtonStyle
=
"
border:1px solid #000000;height:20px;width:50px
"
InputBoxStyle
=
"
border:1px #000000 solid;text-align:center
"
HorizontalAlign
=
"
Right
"
TextAfterInputBox
=
"
页
"
TextBeforeInputBox
=
"
第
"
ShowInputBox
=
"
Always
"
PrevPageText
=
"
上一页
"
NumericButtonCount
=
"
5
"
NextPageText
=
"
下一页
"
FirstPageText
=
"
首页
"
LastPageText
=
"
尾页
"
BorderColor
=
"
#FFFFC0
"
OnPageChanged
=
"
AspNetPager1_PageChanged
"
></
webdiyer:AspNetPager
>
</
div
>
后台代码:
protected
void
Page_Load(
object
sender, EventArgs e)
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
if (!Page.IsPostBack)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
AspNetPager2.CurrentPageIndex = 1;
AspNetPager2.PageSize = 3;
BindRptUserList();
}
}
private
void
BindRptUserList()
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
int RecordCount = 0;
this.rptUserList.DataSource = GetUserList(AspNetPager2.CurrentPageIndex, AspNetPager2.PageSize, out RecordCount);
this.rptUserList.DataBind();
AspNetPager2.RecordCount = RecordCount;
Response.Write(RecordCount);
}
private
DataTable GetUserList(
int
pageIndex,
int
pageSize,
out
int
recordCount)
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
recordCount = 0;
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
SqlParameter[] myparams = new SqlParameter[]...{
new SqlParameter("@PageIndex",SqlDbType.Int),
new SqlParameter("@PageSize",SqlDbType.Int),
new SqlParameter("@RecordCount",SqlDbType.Int)
};
myparams[0].Value = pageIndex;
myparams[1].Value = pageSize;
myparams[2].Direction = ParameterDirection.Output;
DataTable dt = SqlHelper.ExecuteTable(SqlHelper.CONN_STRING_Agriculture, CommandType.StoredProcedure, "p_User_GetUserListPager", myparams);
if (dt.Rows.Count > 0)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
recordCount = Convert.ToInt32(myparams[2].Value.ToString());
}
return dt;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
}
protected
void
AspNetPager1_PageChanged(
object
src, Wuqi.Webdiyer.PageChangedEventArgs e)
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
AspNetPager2.CurrentPageIndex=e.NewPageIndex;
}
昨天晚上遇到一个问题: 在分页控件 返回总记录时 。总是为Null让我非常的郁闷,怎么找都找不到原因,
最后单步 发现,问题出现在 sqlhelper 中 把
public static SqlDataReader ExecuteReader(string connectionString, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
{
SqlDataReader reader;
SqlCommand cmd = new SqlCommand();
SqlConnection conn = new SqlConnection(connectionString);
try
{
PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters);
SqlDataReader reader2 = cmd.ExecuteReader(CommandBehavior.CloseConnection);
// cmd.Parameters.Clear();
reader = reader2;
}
catch
{
conn.Close();
throw;
}
return reader;
}
总结下: 学习一样东西,不能心急,要慢慢来,才能看明白问题,只有冷静才能很好的解决问题