Asp.Net树视图控件折叠后未选中子复选框.如何处理这个问题

Asp.Net Treeview control collapsed does not select the child checkbox. How to handle this?

本文关键字:何处理 处理 问题 复选框 控件 视图 Net 折叠 Asp      更新时间:2023-09-26

我有一个ASP。具有复选框的。NET树视图控件。

使用这个jQuery检查所有的子复选框,当父被选中。这段代码运行良好。

<script type="text/javascript"> 
     function CheckboxGroupSelection() { 
         $('.tree :checkbox').on('change', function () {
             var checked = this.checked;
             var $elem = $(this).closest('table');
             var depth = $elem.find('div').length;
             var $childs = $elem.nextAll('table');
             $childs.each(function () {
                 var $child = $(this);
                 var d = $child.find('div').length;
                 if (d == depth) {
                     return false;
                 }
                 $child.find(':checkbox').prop('checked', checked);
             });
         }); 
     }
</script>

现在,问题在这里:当父节点被折叠,然后选中父节点复选框,逻辑上所有的子复选框都应该被选中。但是这个jQuery不能用于此目的,因为jQuery使用HTML标记来查找和检查复选框,并且,当父节点折叠时,我无法看到子节点标记。

如何处理?或者是否有任何方法可以使用c#或vb.net背后的代码来实现这一点?

折叠时的HTML标记:

<div style="font-size: 11px; font-family: Tahoma; font-weight: bold; text-align: left;" class="tree" id="ctl00_cphMain_ctlEsnSearchByServices_Treecontrol_Left_0_Tree_Index">
    <table cellspacing="0" cellpadding="0" style="border-width:0;">
        <tbody><tr>
            <td><a href="javascript:__doPostBack('ctl00$cphMain$ctlEsnSearchByServices$Treecontrol_Left_0$Tree_Index','t1730b784-94ca-42d3-80aa-1c9c064e271c')"><img style="border-width:0;" alt="Collapse Clinics" src="../Uploadedimages/System/Static_Images/TreeLineImages/dashminus.gif"></a></td><td style="width:295px;"><input type="checkbox" id="ctl00_cphMain_ctlEsnSearchByServices_Treecontrol_Left_0_Tree_Indexn0CheckBox" name="ctl00_cphMain_ctlEsnSearchByServices_Treecontrol_Left_0_Tree_Indexn0CheckBox"><span style="text-decoration:none;" id="ctl00_cphMain_ctlEsnSearchByServices_Treecontrol_Left_0_Tree_Indext0">Clinics</span></td>
        </tr>
    </tbody></table><table cellspacing="0" cellpadding="0" style="border-width:0;">
        <tbody><tr>
            <td><div style="width:20px;height:1px"></div></td><td><a href="javascript:__doPostBack('ctl00$cphMain$ctlEsnSearchByServices$Treecontrol_Left_0$Tree_Index','t1730b784-94ca-42d3-80aa-1c9c064e271c''726a5894-6495-4bd2-90fa-1c0eb60f9406')"><img style="border-width:0;" alt="Expand GP Clinics" src="../Uploadedimages/System/Static_Images/TreeLineImages/tplus.gif"></a></td><td style="width:295px;"><input type="checkbox" id="ctl00_cphMain_ctlEsnSearchByServices_Treecontrol_Left_0_Tree_Indexn1CheckBox" name="ctl00_cphMain_ctlEsnSearchByServices_Treecontrol_Left_0_Tree_Indexn1CheckBox"><span style="text-decoration:none;" id="ctl00_cphMain_ctlEsnSearchByServices_Treecontrol_Left_0_Tree_Indext1">GP Clinics</span></td>
        </tr>
    </tbody></table><table cellspacing="0" cellpadding="0" style="border-width:0;">
        <tbody><tr>
            <td><div style="width:20px;height:1px"></div></td><td><img alt="" src="../Uploadedimages/System/Static_Images/TreeLineImages/t.gif"></td><td style="width:295px;"><input type="checkbox" id="ctl00_cphMain_ctlEsnSearchByServices_Treecontrol_Left_0_Tree_Indexn7CheckBox" name="ctl00_cphMain_ctlEsnSearchByServices_Treecontrol_Left_0_Tree_Indexn7CheckBox"><span style="text-decoration:none;" id="ctl00_cphMain_ctlEsnSearchByServices_Treecontrol_Left_0_Tree_Indext7">Other Clinics</span></td>
        </tr>
    </tbody></table><table cellspacing="0" cellpadding="0" style="border-width:0;">
        <tbody><tr>
            <td><div style="width:20px;height:1px"></div></td><td><img alt="" src="../Uploadedimages/System/Static_Images/TreeLineImages/l.gif"></td><td style="width:295px;"><input type="checkbox" id="ctl00_cphMain_ctlEsnSearchByServices_Treecontrol_Left_0_Tree_Indexn8CheckBox" name="ctl00_cphMain_ctlEsnSearchByServices_Treecontrol_Left_0_Tree_Indexn8CheckBox"><span style="text-decoration:none;" id="ctl00_cphMain_ctlEsnSearchByServices_Treecontrol_Left_0_Tree_Indext8">Polyclinics</span></td>
        </tr>
    </tbody></table>
</div>
Public Sub tree_TreeNodeCheckChanged(ByVal sender As Object, ByVal e As TreeNodeEventArgs) 
    If e IsNot Nothing AndAlso e.Node IsNot Nothing Then 
        If e.Node.ChildNodes.Count > 0 Then
            CheckTreeNodeRecursive(e.Node, e.Node.Checked)
        End If
    End If
End Sub
Private Sub CheckTreeNodeRecursive(ByVal parent As TreeNode, ByVal fCheck As Boolean) 
    For Each child As TreeNode In parent.ChildNodes
        If child.Checked <> fCheck Then
            child.Checked = fCheck
        End If
        If child.ChildNodes.Count > 0 Then
            CheckTreeNodeRecursive(child, fCheck)
        End If
    Next
End Sub