我创建了:
该怎么做?
我已经为数据库连接代码创建了sql helper文件并调用了该方法,而没有使用sqldatasource进行连接。
当我尝试分页时,收到错误:
GridView'GridView1'触发了事件PageIndexChanging,但事实并非如此 处理。
答案 0 :(得分:22)
您需要在代码后面声明一个处理PageIndexChanging事件的方法。
类似的东西:
protected void GridView1_PageIndexChanging (object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
bindGridView(); //bindgridview will get the data source and bind it again
}
private void bindGridView()
{
GridView1.DataSource=getData();
GridView1.DataBind();
}
提供示例代码:
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
bindGridView(); //bindgridview will get the data source and bind it again
}
protected void Page_Load(object sender , EventArgs e)
{
if(!IsPostBack)
bindGridView();
}
//this is some sample data
private void bindGridView()
{
DataTable t = new DataTable();
t.Columns.Add("Col1");
t.Columns.Add("Col2");
DataRow r = null;
for (int i = 0; i < 25; i++)
{
r = t.NewRow();
r.ItemArray = new object[] { "Val" + i, " Another " + i };
t.Rows.Add(r);
}
GridView1.DataSource = t;
GridView1.DataBind();
}
这是标记:
<asp:GridView OnPageIndexChanging="GridView1_PageIndexChanging" AllowPaging="true" PageSize="10" ID="GridView1" runat="server" AutoGenerateColumns="true">
产生这个:
答案 1 :(得分:2)
对于分页,您可以使用OnPageIndexChanging ...
例如
你必须在你的GridView中使用 OnPageIndexChanging =“gvdetails_PageIndexChanging” ......
您必须在下面的代码中将代码写入事件,如
protected void gvdetails_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gvdetails.PageIndex = e.NewPageIndex;
BindData();
}
有关详细信息,请查看以下链接,我在文章中使用页面索引更改...
我希望这会帮助你......与他人分享......谢谢!
答案 2 :(得分:1)
这是最终答案:
Imports System.Collections.Generic ' library
Protected Sub grdEmployees_PageIndexChanging1(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.GridViewPageEventArgs) Handles grdEmployees.PageIndexChanging
grdEmployees.PageIndex = e.NewPageIndex
LoadEmployeeList() 'FUNCTION FOR DATASET
grdEmployees.DataBind()
End Sub
答案 3 :(得分:0)
要解决这个问题,我必须仔细查看我的数据源和数据键。我有一组从SQL Server返回的记录,我正在做的是将它们绑定到POCO。这个类有几个Integer类型的公共属性。这些整数是网格上的数据键。我用字符串替换了他们的类型,而不是绕过铸造问题。
答案 4 :(得分:0)
您只需将其添加到您的代码中:
protected void GridViewTrsEmail_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridViewTrsEmail.PageIndex = e.NewPageIndex;
GridViewTrsEmail.DataBind();
}