【问题标题】:Select All in GridView is not workingGridView 中的全选不起作用
【发布时间】:2018-12-21 00:40:39
【问题描述】:

我有一个 .net Gridview,其中添加了“全选”复选框以选择网格中所有可用的行。下面是我的网格代码

<asp:TemplateColumn>
  <HeaderTemplate>
  <input id="chkAll" type="checkbox" onclick="CheckAllDataGridCheckBoxes('chkItem',this.checked)">
   </HeaderTemplate>
   <ItemTemplate>
   <asp:CheckBox ID="chkItem" runat="server"></asp:CheckBox>
   </ItemTemplate>

这是我的 javascript 代码

<script type="text/javascript">
    function CheckAllDataGridCheckBoxes(aspCheckBoxID, checkVal)
    {
        re = new RegExp(':' + aspCheckBoxID + '$')  //generated control name starts with a colon
        for(i = 0; i < document.forms[0].elements.length; i++)
        {
            elm = document.forms[0].elements[i]
            if (elm.type == 'checkbox')
            {
                if (re.test(elm.name))
                    elm.checked = checkVal
            }
        }
    }
</script>

为什么当我点击“全选”时我的所有行都没有被选中。我的 javascript 有什么问题?

更新的 Javascript

 function CheckAllDataGridCheckBoxes(aspCheckBoxID, checkVal)
    {
        for (i = 0; i < document.forms[0].elements.length; i++)
        {
            elm = document.forms[0].elements[i]
            if (elm.name.endsWith(aspCheckBoxID))
            {
                    elm.checked = checkVal
            }
        }
    }

【问题讨论】:

  • 将 Form1 更改为 document.forms[0]?
  • @Ctznkane525 我现在可以进入循环,但我的“选择所有行”功能仍然无法正常工作。这里还缺少什么?谢谢
  • 给出复选框的客户端 ID 的示例...您可以在页面加载后通过“查看源代码”选项执行此操作...这将告诉您您的正则表达式是否为有效
  • IMT_cpDefaultContentPlaceHolder_dgRegions_ctl02_chkItem 是“查看源代码”中行的复选框的 ID。正则表达式是否有效?
  • 我认为我的正则表达式无效..请您指导我根据生成的 id 格式化正确的..谢谢

标签: javascript c# asp.net gridview checkbox


【解决方案1】:

执行此任务不需要正则表达式。改用这个:

if (elm.name.endsWith(aspCheckBoxID))

参考:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/endsWith

【讨论】:

  • 我已根据您的建议使用新的 Javascript 更新了我的帖子,但仍然无法正常工作。新脚本有什么问题?
  • aspCheckBoxID 的值不是 '__VIEWSTATE'
  • 这是您的第一个输入变量...您在表单上选择了所有这些...但您只想要带有该字符串的 endimg
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-11-23
  • 2015-04-11
  • 2015-12-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多