Click here to Skip to main content
15,878,748 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello People,

I will tell the basic summary as to what I am trying to do.

I have a `tree view` inside an `update panel`, which is shown as a pop-up on a button click of a parent page.

The user will check some `checkboxes`, will click OK and the selected nodes will be populated in a text box.

When, the user clicks the button again, the `tree view` should be visible again with the nodes checked, which the user had selected in the previous case.

I have populated the tree view via a server side method, everything happens fine, except that the nodes, which the user selected are not visible when the button is clicked again.

Maybe the codes below, will make things more clearer.

C#
<asp:UpdatePanel ID="updateSelection" runat="server" UpdateMode="Conditional">
                    <ContentTemplate>
                    <asp:TreeView ID="tv" runat="server" EnableClientScript="true"  AutoGenerateDataBindings="False"
                                  ImageSet="Arrows" ShowCheckBoxes="All" EnableViewState="true"
                                  OnTreeNodeCheckChanged="TV_TreeNodeCheckChanged">
                     </asp:TreeView>
                    </ContentTemplate>
                    <Triggers>
                        <asp:PostBackTrigger ControlID="buttonOK" />
                        <asp:PostBackTrigger ControlID="buttonCancel" />
                    </Triggers>
                </asp:UpdatePanel>

The server side code:

C#
protected void Page_Load(object sender, EventArgs e)
            {
                tv.Attributes.Add("onclick", "TreeViewClick(event)");
                if (!IsPostBack)
                {
                    StringBuilder javaScript = new StringBuilder();
                    javaScript.Append("<script type=text/javascript>\n");
                    javaScript.Append("setvalue();\n");
                    javaScript.Append("\n");
                   javaScript.Append(" " + this.ClientScript.GetPostBackEventReference(this.updateSelection, "DialogArgumentsPostBack") + ";\n");
                    javaScript.Append("</script>\n");
                    ClientScript.RegisterStartupScript(this.GetType(), "OnLoadScript", javaScript.ToString());  
                }
                else
                {
                    string eventArgument = (this.Request["__EVENTARGUMENT"] == null) ? string.Empty : this.Request["__EVENTARGUMENT"];
                      if (eventArgument.Trim() == "DialogArgumentsPostBack")
                    {
                        PopulateTreeView();
                    }
                  }
                 }

    public void PopulateTreeView()
            {
                    if(string.IsNullOrEmpty(hiddenFirst.Value)&& string.IsNullOrEmpty(hiddenSecond.Value))
                   {
                    DataSet ds = new DataSet();
                    DataTable dtUltimateParent = GetUltimateParent();
                    DataTable dtA = GetParent();
                    DataTable dtB = GetChildren();
                    DataTable dt1 = new DataTable();
                    DataTable dt2 = new DataTable();
                    DataTable dt3 = new DataTable();
                    dt1 = dtUltimateParent.Copy();
                    dt2 = dtA.Copy();
                    dt3 = dtB.Copy();
                    ds.Tables.Add(dt1);
                    ds.Tables.Add(dt2);
                    ds.Tables.Add(dt3);
                    ds.Relations.Add("FirstHierarchy", dt1.Columns["ultimateParentID"], dt2.Columns["ParentID"]);
                    ds.Relations.Add("SecondHierarchy", dt2.Columns["ParentID"], dt3.Columns["ChildID"]);
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        tv.Nodes.Clear();
                        foreach (DataRow ultimateRow in ds.Tables[0].Rows)
                        {
                            TreeNode ultimateNode = new TreeNode((string)ultimateRow["ultimateParentText"], Convert.ToString(ultimateRow["ultimateParentID"]));
                            tv.Nodes.Add(ultimateNode);
                            ultimateNode.Expanded = true;
                            ultimateNode.SelectAction = TreeNodeSelectAction.None;
                            foreach (DataRow masterRow in ultimateRow.GetChildRows("FirstHierarchy"))
                            {
                                TreeNode masterNode = new TreeNode((string)masterRow["ParentText"], Convert.ToString(masterRow["ParentID"]));
                                ultimateNode.ChildNodes.Add(masterNode);
                                masterNode.Value = Convert.ToString(masterRow["ParentID"]);
                                masterNode.Expanded = false;
                                masterNode.SelectAction = TreeNodeSelectAction.None;
                                foreach (DataRow childRow in masterRow.GetChildRows("SecondHierarchy"))
                                {
                                    TreeNode childNode = new TreeNode((string)childRow["ChildText"], Convert.ToString(childRow["ChildID"]));
                                    masterNode.ChildNodes.Add(childNode);
                                    childNode.Value = Convert.ToString(childRow["Child"]);
                                }
                            }
                        }
                    }
                }
              }
            else
             {
                //Populate the Tree View as in the first case
    // Now get the nodes which were checked in the first instant and try to check them in the tree view
                string x = string.Empty;
                string y = string.Empty;
                string[] first = hiddenFirst.Value.Split('|');
                foreach (string s in first)
                {
                    string[] firstSplit = s.Split(',');
                    x = x + "," + firstSplit[1];
    
                }
                string[] second = hiddenSecond.Value.Split('|');
                foreach (string s in second)
                {
                    string[] secondSplit = s.Split(',');
                    y = y + "," + secondSplit[1];
                }
                foreach (TreeNode node in tv.Nodes[0].ChildNodes[0].ChildNodes)
                {
                    string[] a = x.Split(',');
                    foreach (string s in a)
                    {
                        if (s == node.Text)
                        {
                            node.Checked = true;
                        }
                    }
                }
    
            }
        }

The code-flow is as below:

User clicks a button on the parent page, the control comes to the `.aspx` page having the `tree view` inside the `update panel`.

Code goes inside the `!IsPostBack` block, then goes inside the `if` block of the `PopulateTreeView()` method and populates the tree view. Secondly, when the user clicks the button again, the control goes to the `Page_Load` again, comes to the `else` block of the `PopulateTreeView()` method, binds the `tree view`, and looks for which nodes were selected, and tries to check those nodes.

The problem lies here, in the second case, when I try to display the `tree view` with selected nodes checked, the `tree view` is empty, doesn't show anything.

I know that this question is not a generic one and maybe my way of explanation is also not good, but a glance at the question will make the readers understand the problem well.

Experts please guide.

Regards

Anurag
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900