asp.net中repeater简单的分页

 我来答
匿名用户
2016-05-07
展开全部
其实网上相关资料是很多的
第一,把前台页面写好,例如
<td colspan="2" style="font-size:12pt;color:#0099ff; background-color:#e6feda;">
共<asp:Label ID="lblpc" runat="server" Text="Label"></asp:Label>页 当前为第
<asp:Label ID="lblp" runat="server" Text="Label"></asp:Label>页
<asp:HyperLink ID="hlfir" runat="server" Text="首页"></asp:HyperLink>
<asp:HyperLink ID="hlp" runat="server" Text="上一页"></asp:HyperLink>
<asp:HyperLink ID="hln" runat="server" Text="下一页"></asp:HyperLink>
<asp:HyperLink ID="hlla" runat="server" Text="尾页"></asp:HyperLink>
跳至第
<asp:DropDownList ID="ddlp" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlp_SelectedIndexChanged" >
</asp:DropDownList>页
</td>

第二、在后台实现分页
private PagedDataSource pds()
{
string connstring = ConfigurationManager.ConnectionStrings["pconn"].ConnectionString;
SqlConnection con = new SqlConnection(connstring);

DataSet ds = new DataSet();
SqlDataAdapter sda = new SqlDataAdapter("select * from authors",con);
sda.Fill(ds,"name");
SqlDataAdapter sda2 = new SqlDataAdapter("select * from titleauthor",con);
sda2.Fill(ds,"title");
ds.Relations.Add("myrela",ds.Tables["name"].Columns["au_id"],ds.Tables["title"].Columns["au_id"]);

PagedDataSource pds = new PagedDataSource();
pds.DataSource = ds.Tables["name"].DefaultView;
pds.AllowPaging = true;//允许分页
pds.PageSize = 5;//单页显示项数
pds.CurrentPageIndex = Convert.ToInt32(Request.QueryString["page"]);
return pds;
}

protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Footer)
{
DropDownList ddlp = (DropDownList)e.Item.FindControl("ddlp");

HyperLink lpfirst = (HyperLink)e.Item.FindControl("hlfir");
HyperLink lpprev = (HyperLink)e.Item.FindControl("hlp");
HyperLink lpnext = (HyperLink)e.Item.FindControl("hln");
HyperLink lplast = (HyperLink)e.Item.FindControl("hlla");

pds().CurrentPageIndex = ddlp.SelectedIndex;

int n = Convert.ToInt32(pds().PageCount);//n为分页数
int i = Convert.ToInt32(pds().CurrentPageIndex);//i为当前页

Label lblpc = (Label)e.Item.FindControl("lblpc");
lblpc.Text = n.ToString();
Label lblp = (Label)e.Item.FindControl("lblp");
lblp.Text = Convert.ToString(pds().CurrentPageIndex + 1);

if (!IsPostBack)
{
for (int j = 0; j < n; j++)
{
ddlp.Items.Add(Convert.ToString(j + 1));
}
}

if (i <= 0)
{
lpfirst.Enabled = false;
lpprev.Enabled = false;
lplast.Enabled = true;
lpnext.Enabled = true;
}
else
{
lpprev.NavigateUrl = "?page=" + (i - 1);
}
if (i >= n - 1)
{
lpfirst.Enabled = true;
lplast.Enabled = false;
lpnext.Enabled = false;
lpprev.Enabled = true;
}
else
{
lpnext.NavigateUrl = "?page=" + (i + 1);
}

lpfirst.NavigateUrl = "?page=0";//向本页传递参数page
lplast.NavigateUrl = "?page=" + (n - 1);

ddlp.SelectedIndex = Convert.ToInt32(pds().CurrentPageIndex);//更新下拉列表框中的当前选中页序号
}

}
protected void ddlp_SelectedIndexChanged(object sender, EventArgs e)
{//脚模板中的下拉列表框更改时激发
string pg=Convert.ToString((Convert.ToInt32(((DropDownList)sender).SelectedValue)-1));//获取列表框当前选中项
Response.Redirect("repeate.aspx?page="+pg);//页面转向
}
}

可能比较乱,麻烦您请看 http://blog.csdn.net/qin_zhangyongheng/article/details/7823192
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式