asp.net中repeater简单的分页

kuaidi.ping-jia.net  作者:佚名   更新日期:2024-08-02
asp.net里面的Repeater控件怎么做分页功能。

给个按钮 选择页码 数据库top num 就取出来了

你的数据是另一个人提供的什么意思?他每次都是把所有数据提供给你,然后由你自己进行假分页?

其实网上相关资料是很多的
第一,把前台页面写好,例如
<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

  • 关于ASP.net中 Repeater控件,想要显示部分字段值在aspx页面中,但是显 ...
    答:<AlternatingItemTemplate> <%#Eval("userName")%> <%#Eval("topic")%> <%#Eval("content")%> </AlternatingItemTemplate> 先看看有数据没 然后 把下面的AlternatingItemTemplate 删除后再看看
  • asp.net 在repeater控件中加按钮的问题
    答: </ItemTemplate> </asp:Repeater> 后台代码:protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e){ if (e.CommandName.Equals("Forward")){ string str = e.CommandArgument.ToString();} } 把这段代码粘过去,把绑定字段改了。测试过的,没问题。
  • ASP.NET repeater控件传值
    答:.aspx <asp:Repeater ID="Repeater1" runat="server"> <ItemTemplate> <%# (Container.DataItem as User).Id %> <asp:Label ID="lblName" runat="server" Text='<%# (Container.DataItem as User).Name %>'></asp:Label> <asp:Button ID="btnSelect" runat="server" Text="选择" On...
  • asp.net中怎么循环repeater控件取出每行数据
    答:DEMO.aspx asp:Repeater ID="Repeater1" runat="server"> <ItemTemplate> <asp:Label ID="lbl_ID" runat="Server" Text='<%#Eval("ID") %>'></asp:Label> <asp:Label ID="lbl_NAME" runat="Server" Text='<%#Eval("NAME") %>'></asp:Label> </ItemTemplate> </asp:Repeater> ...
  • 关于ASP.NET 中Repeater控件数据显示 与SQL查询的问题
    答:前台:<asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_ItemDataBound"> <ItemTemplate> <asp:HiddenField ID="HiddenField1" runat="server" Value='<%#Eval("大类的Id") %>' /> <%#Eval("大类的名称") %> <asp:Repeater ID="Repeater2" runat="server">...
  • asp.net里面的Repeater控件怎么做分页功能。
    答:给个按钮 选择页码 数据库top num 就取出来了
  • 在asp.net中,下列关于Repeater控件的描述正确的是:
    答:C)Repeater控件不会自动添加HTML的相关内容D)Repeater控件不会生成任何多余的代码 第2题 BD
  • asp.net(C#)repeater控件问题
    答:alert("请选择需要删除的项");else { document.getElementById('dl_item').value = ids;submitAction('delete');} } function submitAction(actn) { document.getElementById('dl_action').value = actn;document.forms[0].action = '';document.forms[0].submit();} cs中:if (IsPostBack...
  • 怎样修改ASP.NET中的Repeater控件title的字体颜色或者增加NEW图标_百...
    答:你可以在数据库中加一个字段,例如是IsNew,类型为字符串 如果IsNew为"1",表示要加NEW图标 如果IsNew为"0",表示不加 那么下面这句 改成 <%# Eval("IsNew").ToString()=="1" ? " 就可以了 后台编辑文章是把 IsNew 也做成可编辑,你想哪个加图标就把它修改成"1"值吧 至于颜色,也是一...
  • asp.net repeater交替模板各行变色,点击,悬浮变色,不要复制粘贴_百度知 ...
    答:--footer不会重复--> </FooterTemplate></asp:Repeater>普通模板和交替模板中的行用css控制,显示不同的颜色,悬浮变色也是css的伪类实现。table tr.white { background-color: white;}table tr.bisque { background-color: bisque;}table tbody tr:hover { background-color:...