Click here to Skip to main content
15,918,516 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.

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

The server side code:

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(" " + this.ClientScript.GetPostBackEventReference(this.updateSelection, "DialogArgumentsPostBack") + ";\n");
                    ClientScript.RegisterStartupScript(this.GetType(), "OnLoadScript", javaScript.ToString());  
                    string eventArgument = (this.Request["__EVENTARGUMENT"] == null) ? string.Empty : this.Request["__EVENTARGUMENT"];
                      if (eventArgument.Trim() == "DialogArgumentsPostBack")

    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.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)
                        foreach (DataRow ultimateRow in ds.Tables[0].Rows)
                            TreeNode ultimateNode = new TreeNode((string)ultimateRow["ultimateParentText"], Convert.ToString(ultimateRow["ultimateParentID"]));
                            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"]));
                                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"]));
                                    childNode.Value = Convert.ToString(childRow["Child"]);
                //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.



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