asp.net – GridView’GridView1’触发的事件PageIndexChanging没有被处理

前端之家收集整理的这篇文章主要介绍了asp.net – GridView’GridView1’触发的事件PageIndexChanging没有被处理前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我创造了:

>一个母版页和一个名为Detail的内容页.
>按钮单击事件,在网格视图中显示数据.
>在网格视图中,列是自动生成的.
>我想在网格视图中显示11列,但它不仅仅是页面
尺寸.

该怎么办?

我已经创建了数据库连接代码sql助手文件,并调用方法,而不是使用sqldatasource进行连接.

当我尝试做分页,得到错误

The GridView ‘GridView1’ fired event PageIndexChanging which wasn’t
handled.

解决方法

您需要在代码后面声明一个处理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">

产生这个:

原文链接:https://www.f2er.com/aspnet/246709.html

猜你在找的asp.Net相关文章