【问题标题】:Persist checkbox in gridview while custom paging自定义分页时在gridview中保留复选框
【发布时间】:2015-12-27 19:35:36
【问题描述】:

我创建了一个网格视图并在项目模板中添加了一个复选框。这个网格与DataKey (primary key) 一起只有几列。由于性能提升,此网格将根据页码点击获取每个页面更改的下一组记录。这样就完成了。

现在,当用户在第一页中选择一个复选框,然后转到第 2 页并返回到第一页时,用户将不会像之前那样看到复选框被选中。

那么当用户将页面移动到页面时,有没有一种好方法可以保留复选框?

此复选框可用作标记,以选择稍后可以通过网格外的按钮删除的行。

【问题讨论】:

  • 复选框有什么作用?它是否对其 oncheckedchanged 事件执行任何类型的服务器操作,例如更新单个或一组字段?
  • 用户希望选中该复选框,然后通过单击网格外的按钮执行删除。它就像 Gmail 上的功能。

标签: asp.net gridview checkbox


【解决方案1】:

由于每次选择分页时都会收到一个新集合,因此我建议采用以下方法:

通过 javascript 创建一个 array[] 对象,该对象将在选中复选框时添加到列表数据键中,如果取消选中复选框,则将其删除。像这样的:

var selectedDataKeys = [];

$('.checkboxclass').on('change', function() {

   // Considering you assign the data key as id for the checkbox otherwise implement a way to retrieve the id.
   var dataKey = $(this).prop('id');

   // Determine if the dataKey is in the selected data keys array
   var isContained = (selectedDataKeys.indexOf(dataKey) > -1 );

   if($(this).is(':checked')) {
      // If is contained is false - add to the array
      if (!isContained)
         selectedDataKeys.push(dataKey);
   } else {
      // If is contained is true - remove to the array
      if (isContained){
         selectedDataKeys = $.grep(selectedDataKeys, function(value) {
            return value != dataKey;
         });
      }
   }
});

从此时起,客户端用户将拥有一个选定项目的活动列表,现在由您来使用该列表来操作您的网格显示页面。通过将网格显示上的所有项目与 selectedDataKeys 数组进行比较来修改文档上的显示,或者发送这些键并在服务器端进行比较。

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 2012-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-13
    • 1970-01-01
    • 2012-03-27
    • 1970-01-01
    • 2017-01-27
    相关资源
    最近更新 更多