首先,分析下原因:

1.前提DataKeyNames绑定。

2.将记录排序分配索引号,从0开始。

3.当点击其他分页时,GridView将重新分配count为当前页记录数

eg: 每页显示2条,共5条,也就是3页。

第一页,GridView.count是2

第二页,GridView.count也是2

第三页,GridView.count是1

也就是说count是当页的条数。而e.CommandArgument索引号是递增不重新分配。

解决方案:

            int iPageIndex = GridView1.PageIndex;   //获取当前页索引
            int iPageSize =  GridView1.PageSize;      //获取每页显示记录数
            int iIndex = Convert.ToInt16(e.CommandArgument)-iPageIndex*iPageSize;  //计算
            string strId = GridView1.DataKeys[iIndex].Value.ToString();

            //ID获取,是删除,还是更新,随你。

 

相关文章:

  • 2021-09-06
  • 2021-08-03
  • 2022-12-23
  • 2021-05-03
  • 2022-12-23
  • 2022-12-23
  • 2022-01-07
  • 2022-02-08
猜你喜欢
  • 2021-09-27
  • 2022-01-08
  • 2021-12-27
  • 2021-11-07
  • 2022-12-23
  • 2021-12-31
  • 2022-12-23
相关资源
相似解决方案