树视图的奇怪行为 [英] Strange behavior of a Tree View

查看:85
本文介绍了树视图的奇怪行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello People,



我会告诉我要做的事情的基本摘要。



我在`update panel`中有一个`tree view`,它在父页面的按钮上显示为弹出窗口。



用户将检查一些复选框,将单击确定,所选节点将填充在文本框中。



当用户再次单击该按钮时,在查看节点后,树视图应该再次可见,用户在前一种情况下选择了这些节点。



我已经通过服务器端方法填充树视图一切都很好,除了再次点击按钮时用户选择的节点不可见。



也许下面的代码会让事情更加清晰。



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">
                    <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:

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;
                        }
                    }
                }
    
            }
        }



代码流如下下面:



用户单击父页面上的按钮,控件进入``.aspx`页面,在`update panel`中有`tree view`。



代码进入`!IsPostBack`块,然后进入`PopulateTreeView()`方法的`if`块并填充树视图。其次,当用户再次单击该按钮时,控件再次进入`Page_Load`,进入`PopulateTreeView()`方法的`else`块,绑定`tree view`,并查找选择的节点并尝试检查这些节点。



问题出在这里,在第二种情况下,当我尝试显示所选节点的树视图时, `tree view`是空的,没有显示任何内容。



我知道这个问题不是通用的,也许我的解释方式也不好,但是看一眼这个问题就会让读者很好地理解这个问题。



专家请指导。



问候



Anurag


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

推荐答案

这篇关于树视图的奇怪行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆