asp.net中的repeater控件要如何做到分页??
现在我用这段代码已经连接数据库了,并且在页面中可以显示需要的字段,那么如何这些数据分页呢,要加什么句子??protectedvoidPage_Load(objectsen...
现在我用这段代码已经连接数据库了,并且在页面中可以显示需要的字段,那么如何这些数据分页呢,要加什么句子??
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Server =.; Initial Catalog =test; uid = sa; pwd =12345asdfg";
string sql = "select * from leave";
conn.Open();
SqlDataAdapter da = new SqlDataAdapter(sql, conn);
DataSet ds = new DataSet();
da.Fill(ds, "Punish");
Repeater1.DataSource = ds.Tables["Punish"].DefaultView;
Repeater1.DataBind();
} 展开
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Server =.; Initial Catalog =test; uid = sa; pwd =12345asdfg";
string sql = "select * from leave";
conn.Open();
SqlDataAdapter da = new SqlDataAdapter(sql, conn);
DataSet ds = new DataSet();
da.Fill(ds, "Punish");
Repeater1.DataSource = ds.Tables["Punish"].DefaultView;
Repeater1.DataBind();
} 展开
- 你的回答被采纳后将获得:
- 系统奖励15(财富值+成长值)+难题奖励10(财富值+成长值)+提问者悬赏10(财富值+成长值)
1个回答
展开全部
其实网上相关资料是很多的
第一,把前台页面写好,例如
<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
第一,把前台页面写好,例如
<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
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询