我的页面有一个DATAGRID做的分页浏览.但老是出现无效的 CurrentPageIndex 值。它必须大于等于 0 且小于 PageCount这个错误。 protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.binddate();
}
} protected void binddate()
{
SqlConnection con = DB.createCon();
con.Open();
SqlCommand cmd = new SqlCommand("select top 500 * from pubs order by NowDate desc", con);
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = cmd;
DataSet ds = new DataSet();
sda.Fill(ds);
this.GD.DataSource = ds.Tables[0];
this.GD.DataBind();
con.Close();
} protected void GD_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
{
//新开分页
this.GD.CurrentPageIndex = e.NewPageIndex;
//调用方法
this.binddate();
}
{
if (!this.IsPostBack)
{
this.binddate();
}
} protected void binddate()
{
SqlConnection con = DB.createCon();
con.Open();
SqlCommand cmd = new SqlCommand("select top 500 * from pubs order by NowDate desc", con);
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = cmd;
DataSet ds = new DataSet();
sda.Fill(ds);
this.GD.DataSource = ds.Tables[0];
this.GD.DataBind();
con.Close();
} protected void GD_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
{
//新开分页
this.GD.CurrentPageIndex = e.NewPageIndex;
//调用方法
this.binddate();
}
解决方案 »
- 求教-网站asp.net问题
- 网站管理员页面
- 关于ajax的使用,鼠标划过动态显示层,第一次会出不来,怎么回事?
- vb.net 工程调用c#
- 安装好VS2003后,想建立ASP.NET项目,发现问题,请高手帮忙!谢谢!
- cuteeditor默认根目录修改?
- 请教关于DropDownList和DataGrid配合使用的问题(在线等)
- onrowdatabound 这个属性为什么在 aspx 的设计模式中有用 ,在 cs文件中gridview
- C# 调用MFC封装的时DLL出错,求大侠们帮忙!
- 在asp.net中如何调用以前用delphi写的dll组件?
- 如何 清空 所有 用户的 session ???
- 如何 循环获取 服务器端控件 TextBox 的值
protected void GD_DeleteCommand(object source, DataGridCommandEventArgs e)
{
if ((GD.CurrentPageIndex == GD.PageCount - 1) && GD.Items.Count == 1)
{
//如果当前页-1>1表示当前页为多页的最后一页
if (GD.CurrentPageIndex - 1 > 1)
{
//由于是多页中的最后一页,且是最后一条记录,会产生异常,此时将当前页手动减1,避免异常
GD.CurrentPageIndex = GD.CurrentPageIndex - 1;
}
else
{
GD.CurrentPageIndex = 0;
}
}
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{ GridView1.PageIndex = e.NewPageIndex;
binddate()
}
Private Sub Grid_EditCommand( _
ByVal source As System.Object, _
ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs _
) Handles Grid.EditCommand Dim ID As Long = Grid.DataKeys(e.Item.ItemIndex)
Dim nextUrl As String = "Edit.aspx?ID=" & ID.ToString()
Response.Redirect(nextUrl) End Sub
我VB>NEt中代码如上,得有Handles Grid.EditCommand和对象关联才行的。
前台代码:
<asp:DataGrid ID="GD" runat="server" Width="100%" BackColor="White" BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" CellPadding="3" ForeColor="Black" GridLines="Vertical" ShowHeader="False" AutoGenerateColumns="False" PageSize="1" AllowPaging="True" OnPageIndexChanged="ProjectCompletedGD_PageIndexChanged">
<FooterStyle BackColor="#CCCCCC" />
<SelectedItemStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" NextPageText="下一页" PrevPageText="上一页" />
<AlternatingItemStyle BackColor="#CCCCCC" />
<HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
<Columns>
<asp:TemplateColumn HeaderText="ID">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem,"ID")%>
</ItemTemplate>
<ItemStyle Width="5%" />
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="项目名称">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem,"Name")%>
</ItemTemplate>
<ItemStyle Width="30%" />
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="建设地点">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem,"Location")%>
</ItemTemplate>
<ItemStyle Width="25%" />
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="建设单位">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem,"Owner")%>
</ItemTemplate>
<ItemStyle Width="15%" />
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="项目状态">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "Status")%>
</ItemTemplate>
<ItemStyle Width="15%" />
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="管理操作">
<ItemTemplate>
<a href="add.aspx?ID=<%# DataBinder.Eval(Container.DataItem,"ID")%>">处理</a>
</ItemTemplate>
<ItemStyle Width="10%" />
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
每次在引发翻页事件之后,页面将回发到服务端,这个时候你的datagird没有再次绑定,哪有数据呢?
前面某位兄台已经告诉你答案了...