AspNetpager分页控件问题?

大师 2004-10-12 10:12:29
给datalist分页,设定好每页显示的数量,页数能够算出来,但点击每一页都只显示第一页?
...全文
311 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
celerityok 2004-11-14
  • 打赏
  • 举报
回复
关注!
asuntea 2004-10-20
  • 打赏
  • 举报
回复
up
大师 2004-10-19
  • 打赏
  • 举报
回复
谢谢!
mingzaili 2004-10-19
  • 打赏
  • 举报
回复
我刚看到一篇很好的帖子:把内容贴出来给你看吧:
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="DataGrid1.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
<meta content="Microsoft Visual Studio 7.0" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<table align="center">
<tr>
<td style="HEIGHT: 104px" colspan="2">
<asp:datagrid id="DataGrid1" runat="server" Width="700px" Height="152px" BorderColor="#FFC080"
BorderWidth="1px" BackColor="#FFE0C0" CellPadding="3" AllowPaging="True" HorizontalAlign="Center"
Font-Names="Verdana" Font-Size="8pt">
<AlternatingItemStyle BackColor="Linen"></AlternatingItemStyle>
<ItemStyle VerticalAlign="Bottom"></ItemStyle>
<HeaderStyle HorizontalAlign="Center" VerticalAlign="Bottom" BackColor="#FF8000"></HeaderStyle>
<FooterStyle HorizontalAlign="Justify" BackColor="White"></FooterStyle>
<PagerStyle HorizontalAlign="Right" ForeColor="Red" Mode="NumericPages"></PagerStyle>
</asp:datagrid></td>
</tr>
<tr align="center">
<td align="left"><font size="2">一共</font> <asp:Label ID="lblallcount" Runat="server" Font-size="8pt" ForeColor="#0000ff" font-names="verdana"></asp:Label> <font size="2">记录</font>
<font size="2">共</font> <asp:label id="lblpagecount" runat="server" Font-Size="8pt" ForeColor="#0000ff" Font-Names="Verdana"></asp:label> <font size="2">页</font>
<font size="2">第</font> <asp:label id="lblcurrentindex" runat="server" Font-Size="8pt" ForeColor="#0000ff" Font-Names="Verdana"></asp:label> <font size="2">页</font></td>
<td align="right"><asp:linkbutton id="first" runat="server" font-size="8pt" ForeColor="#3300ff" CommandArgument="first"
Font-Names="Verdana"></asp:linkbutton>
<asp:linkbutton id="prev" Font-Size="8pt" ForeColor="#3300ff" CommandArgument="prev" Runat="server"
Font-Names="Verdana"></asp:linkbutton>
<asp:linkbutton id="next" Font-Size="8pt" ForeColor="#3300ff" CommandArgument="next" Runat="server"
Font-Names="Verdana"></asp:linkbutton>
<asp:linkbutton id="last" Font-Size="8pt" ForeColor="#3300ff" CommandArgument="last" Runat="server"
Font-Names="Verdana"></asp:linkbutton>
<font size="2">跳转到第</font><asp:TextBox ID="to" Runat="server" Columns="1" /><font size="2">页</font><asp:Button ID="go" Text="GO" Runat="server" BackColor="WhiteSmoke" Font-Names="Verdana" />
</td>
</tr>
</table>
</form>
</body>
</HTML>
上面是html
下面是c#代码:
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label lblallcount;
protected System.Web.UI.WebControls.Label lblpagecount;
protected System.Web.UI.WebControls.Label lblcurrentindex;
protected System.Web.UI.WebControls.LinkButton first;
protected System.Web.UI.WebControls.LinkButton prev;
protected System.Web.UI.WebControls.LinkButton next;
protected System.Web.UI.WebControls.LinkButton last;
protected System.Web.UI.WebControls.TextBox to;
protected System.Web.UI.WebControls.Button go;
protected System.Web.UI.WebControls.DataGrid DataGrid1;

private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
first.Text="最首页";
prev.Text="前一页";
next.Text="下一页";
last.Text="最后页";
if(!IsPostBack)
{
BindGrid();
}

}


#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.DataGrid1.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.Page_Changed);
this.first.Click += new System.EventHandler(this.btnClick);
this.prev.Click += new System.EventHandler(this.btnClick);
this.next.Click += new System.EventHandler(this.btnClick);
this.last.Click += new System.EventHandler(this.btnClick);
this.go.Click += new System.EventHandler(this.goClick);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

void Page_Changed(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
DataGrid1.CurrentPageIndex=e.NewPageIndex;
BindGrid();
}

void BindGrid()
{
SqlConnection cn=new SqlConnection("Server=localhost;uid=sa;pwd=aaa;Database=sinco");
cn.Open();
SqlDataAdapter da=new SqlDataAdapter("select * from 员工 order by 序号",cn);
DataSet ds=new DataSet();
da.Fill(ds,"员工");
DataGrid1.DataSource=ds.Tables["员工"].DefaultView;
//或者DataGrid1.DataSource=ds.Tables["员工"];
DataGrid1.DataBind();
cn.Close();
showstate();
}

void showstate()
{
SqlConnection cn=new SqlConnection("Server=localhost;uid=sa;pwd=aaa;Database=sinco");
cn.Open();
SqlDataAdapter da=new SqlDataAdapter("select * from 员工 order by 序号",cn);
DataSet ds=new DataSet();
da.Fill(ds,"员工");
DataTable dt=ds.Tables["员工"];
int count=dt.Rows.Count;
lblallcount.Text=count.ToString();
lblpagecount.Text=DataGrid1.PageCount.ToString();
lblcurrentindex.Text=(DataGrid1.CurrentPageIndex+1).ToString();
}

void btnClick(object sender,System.EventArgs e)
{
string arg=((LinkButton)sender).CommandArgument;
switch(arg)
{
case ("first"):
DataGrid1.CurrentPageIndex=0;
break;
case ("prev"):
if(DataGrid1.CurrentPageIndex>0)
DataGrid1.CurrentPageIndex--;
break;
case ("next"):
if(DataGrid1.CurrentPageIndex<(DataGrid1.PageCount-1))
DataGrid1.CurrentPageIndex++;
break;
case ("last"):
DataGrid1.CurrentPageIndex=DataGrid1.PageCount-1;
break;
default:
DataGrid1.CurrentPageIndex=Convert.ToInt32(arg);
break;
}
BindGrid();
}


private void goClick(object sender, System.EventArgs e)
{
if(to.Text.Trim()!="")
{
int index=Int32.Parse(to.Text.Trim())-1;
if(index>=0&&index<DataGrid1.PageCount)
{
DataGrid1.CurrentPageIndex=index;
}
}
BindGrid();
}
}
对你应该有用!
大师 2004-10-19
  • 打赏
  • 举报
回复
今天早上上班心情很好,来了没有5分钟问题就解决了!!!
非常感谢 webdiyer(陕北吴旗娃) 老师的控件。
也感谢回帖的人,尤其是gzliuxi(左右), wjcking(www.cs-aspx.net) 的解答!
shiningsun33 2004-10-18
  • 打赏
  • 举报
回复
这位仁兄真的没话说。。。好人哪
wjcking 2004-10-18
  • 打赏
  • 举报
回复
给你说明白点

//在
private void InitializeComponent()
{
//加上下面这句话
this.NetPage.PageChanged += new Wuqi.Webdiyer.PageChangedEventHandler(this.NetPage_PageChanged);
//其中NetPage改为你给控件起的名字
}

这样 你点击页面才会变 不然只会第一页 还有就是 你把控件剪贴移动位置 这句都要写过~~~

gzliuxi 2004-10-18
  • 打赏
  • 举报
回复
给你说明白点

//在
private void InitializeComponent()
{
//加上下面这句话
this.NetPage.PageChanged += new Wuqi.Webdiyer.PageChangedEventHandler(this.NetPage_PageChanged);
//其中NetPage改为你给控件起的名字
}

这样 你点击页面才会变 不然只会第一页 还有就是 你把控件剪贴移动位置 这句都要写过~~~
gzliuxi 2004-10-18
  • 打赏
  • 举报
回复

private void InitializeComponent()
{
//加上下面这句话
this.NetPage.PageChanged += new Wuqi.Webdiyer.PageChangedEventHandler(this.NetPage_PageChanged);
//NetPage为控件名字
}
gzliuxi 2004-10-18
  • 打赏
  • 举报
回复
private void InitializeComponent()
{
this.NetPage.PageChanged += new Wuqi.Webdiyer.PageChangedEventHandler(this.NetPage_PageChanged);

}
NetPage为控件名字
张海霖 2004-10-18
  • 打赏
  • 举报
回复
这个控件是娃的吗?没用过
webdiyer 2004-10-18
  • 打赏
  • 举报
回复
你是怎么绑定数据的?把BindData();方法的代码帖出来看看。谢谢!
大师 2004-10-18
  • 打赏
  • 举报
回复
up
wangrenda 2004-10-12
  • 打赏
  • 举报
回复
存储过程,他的控件带例子你可以参考
qingyun1020 2004-10-12
  • 打赏
  • 举报
回复
没用过datalist,哈,着就研究研究
给你顶先
大师 2004-10-12
  • 打赏
  • 举报
回复
再问:如何写分页事件啊
shixianyong 2004-10-12
  • 打赏
  • 举报
回复
ding
CodePlayer 2004-10-12
  • 打赏
  • 举报
回复
是得,
还有你绑定数据得时候要写在
if(!PostBack)
{
}
里面,不然,每次刷新页面会重新绑定控件得,你看到得永远是第一页。
大师 2004-10-12
  • 打赏
  • 举报
回复
是下面的吗?
void ChangePage(object src,Wuqi.Webdiyer.PageChangedEventArgs e)

{
AspNetPager1.CurrentPageIndex=e.NewPageIndex;
BindData();
}
CodePlayer 2004-10-12
  • 打赏
  • 举报
回复
你没写分页事件吧

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧