Answered by:
CSS Treeview Adapter losing checkbox state

Question
-
User753892747 posted
I am using the css treeview adapter. It appears that when I do a postback using a checkbox control and set the items programatically, the items do not stay checked. If I use a button to set the items, it works fine. If anybody has any idea, please let me know.
Here is a simple test:
HTML:
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
<script language='JavaScript' type='text/javascript' src='/scripts/CascadeCheckmarks.js'></script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:CheckBox ID="chkCheckItem" runat="server" Text="Check last two items" AutoPostBack="true" />
<br />
<asp:Button ID="btnCheckItem" runat="server" Text="Check first two items" />
<asp:TreeView ID="tvListProspect" OnClientClickedCheckbox="CascadeCheckmarks(event)"
runat="server" CssSelectorClass="SicTreeView" ShowCheckBoxes="All">
</asp:TreeView>
</div>
</form>
</body>
</html>vb:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
LoadList()
End If
End Sub
Private Sub LoadList()
Dim node As TreeNode
node = New TreeNode
node.Value = "0"
node.Text = "0"
node.ShowCheckBox = True
Me.tvListProspect.Nodes.Add(node)
node = New TreeNode
node.Value = "1"
node.Text = "1"
node.ShowCheckBox = True
Me.tvListProspect.Nodes.Add(node)
node = New TreeNode
node.Value = "2"
node.Text = "2"
node.ShowCheckBox = True
Me.tvListProspect.Nodes.Add(node)
node = New TreeNode
node.Value = "3"
node.Text = "3"
node.ShowCheckBox = True
Me.tvListProspect.Nodes.Add(node)
node = New TreeNode
node.Value = "4"
node.Text = "4"
node.ShowCheckBox = True
Me.tvListProspect.Nodes.Add(node)
node = New TreeNode
node.Value = "5"
node.Text = "5"
node.ShowCheckBox = True
Me.tvListProspect.Nodes.Add(node)
End Sub
Protected Sub btnCheckItem_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCheckItem.Click
Me.tvListProspect.Nodes(0).Checked = True
Me.tvListProspect.Nodes(1).Checked = True
End Sub
Protected Sub chkCheckItem_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles chkCheckItem.CheckedChanged
Me.tvListProspect.Nodes(4).Checked = chkCheckItem.Checked
Me.tvListProspect.Nodes(5).Checked = chkCheckItem.Checked
End Sub
Monday, March 31, 2008 2:42 PM
Answers
-
User753892747 posted
After some digging, it appears the problem is in LoadPostData and UpdateCheckmarks. I believe that LoadPostData wasn't ever implemented right at all.
Here is what I found out: It appears that UpdateCheckMarks only checks for objects that are in page.request.form, meaning either you checked them on the screen and hit a submit type, or you loaded the tree dynamically on page load and checked some items. If you were to dynamically check the nodes on a non submit postback, it wouldn't work, which was the original problem. Once I understood the problem, someone on the team I work with suggested to put the ones I want checked (on a non submit postback) in the context object. So I created an arraylist, added the items I wanted checked then modified UpdateCheckMarks like so:
Private Sub UpdateCheckmarks(ByVal items As TreeNodeCollection) Dim treeView As TreeView = CType(Control, TreeView) If ((Not (treeView) Is Nothing) AndAlso (Not (items) Is Nothing)) Then For Each item As TreeNode In items If IsCheckbox(treeView, item) Then Dim name As String = (treeView.UniqueID + ("n" _ + (_checkboxIndex.ToString + "CheckBox"))) Dim bIsNowChecked As Boolean = (Not (Page.Request.Form(name)) Is Nothing) If bIsNowChecked Then '<- added this, item is not in page.request.form If (item.Checked <> bIsNowChecked) Then item.Checked = bIsNowChecked Extender.RaiseAdaptedEvent("TreeNodeCheckChanged", New TreeNodeEventArgs(item)) End If Else If HttpContext.Current.Items.Contains("NewCheckedNodes") Then '<-check to see if context object contains my new arraylist, if it does, process Dim newCheckedNodes As ArrayList = HttpContext.Current.Items.Item("NewCheckedNodes") If newCheckedNodes.Contains(item) Then item.Checked = True Else item.Checked = False End If Else item.Checked = False End If item.ImageToolTip = "" End If _checkboxIndex = (_checkboxIndex + 1) End If If HasChildren(item) Then UpdateCheckmarks(item.ChildNodes) End If Next End If End Sub
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, April 1, 2008 10:34 AM