当我选择一个节点时,为什么我的树视图会回到顶部?

时间:2015-12-31 14:23:21

标签: c# treeview

如果我向下滚动到某个特定节点并选择它来显示子节点,我的树视图会自行重置,以便第一个节点返回到页面顶部。如果我向下滚动到所选节点,我确实看到节点已打开并且显示了子节点,但是如何使树视图保持不变,以便用户不必向下滚动以查看其所选节点?

这是我的代码:

的asp:

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <ContentTemplate>
        <div id="divTreeViewScrollTo">
            <asp:Panel ID="pnl" runat="server" Height="370px" Width="760px" BackColor="#ffffff" ScrollBars="Auto">
                <asp:TreeView ID="TreeViewAccts" runat="server" ShowLines="true" PopulateNodesFromClient="false" OnSelectedNodeChanged="TreeView1_SelectedNodeChanged1" Height="118px" ShowExpandCollapse="true" Font-Size="X-Small" Width="645px" NodeIndent="10" Font-Bold="True" ForeColor="White" ExpandDepth="0">
                   <NodeStyle VerticalPadding="1" 
                              Font-Names="Courier" 
                              Font-Size="8pt" 
                              NodeSpacing="0" 
                              HorizontalPadding="5" 
                              BorderStyle="Solid"
                              BorderColor="DarkCyan"
                              BorderWidth="1"
                              BackColor="White"
                              Width="640">
                   </NodeStyle>        
                </asp:TreeView>   
                <asp:TextBox ID="txtTreeselect" runat="server" Enabled="False" Visible="False"></asp:TextBox>
            </asp:Panel>
        </div>
    </ContentTemplate>
</asp:Content>

C#:

public partial class frmManageAccts : System.Web.UI.Page
{
    public string stringSelectedValue { get; set; }
    string str = System.Configuration.ConfigurationManager.ConnectionStrings["PBRConnectionString"].ConnectionString;
    public string MySortVal;
    public string MySortVal2;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            FillTree_Parent();
        }

    }

    void FillTree_Parent()
    {  // fills the parent view of the Tree Action items
        //int RoleID = Convert.ToInt32(ddlRole.SelectedValue);
        using (SqlConnection con4 = new SqlConnection(ConfigurationManager.ConnectionStrings["PBRConnectionString"].ConnectionString))
        {
            try
            {
                SqlCommand cmd2 = new SqlCommand("SELECT [ACCT_GRP], [ACCT_GRP_PK], [ACTIVE_FLG], [LOAD_BY], [LOAD_TIMESTAMP] FROM [ACCT_GRP_LIST] ORDER BY [ACCT_GRP] ASC", con4);
                SqlDataAdapter da = new SqlDataAdapter(cmd2);
                DataSet PrSet = new DataSet();

                da.Fill(PrSet, "ACCT_GRP");
                TreeViewAccts.Nodes.Clear();
                foreach (DataRow dr in PrSet.Tables[0].Rows)
                {
                    DateTime date = DateTime.Parse(dr["LOAD_TIMESTAMP"].ToString());
                    string formatted = date.ToString("MM/dd/yyyy");
                    TreeNode tnParent = new TreeNode();
                    tnParent.Text = dr["ACCT_GRP"].ToString().Replace("'", "''").Trim() +
                        SpaceCount(dr["ACCT_GRP"].ToString().Replace("'", "''").Trim(), 55) + 
                        "Active: " + 
                        dr["ACTIVE_FLG"].ToString() +
                        SpaceCount("XXXXX", 10) + 
                        "Loaded On: " + formatted + "";
                    tnParent.Value = dr["ACCT_GRP_PK"].ToString();
                    //   tnParent.Tag = dr["Disp"].ToString();
                    tnParent.PopulateOnDemand = true;
                    //  tnParent.ToolTip = "You Must Choose a Reason";
                    tnParent.SelectAction = TreeNodeSelectAction.SelectExpand;
                    // tnParent.Expand();
                    //   tnParent.Selected = true;

                    //TreeViewAccts.BorderStyle = BorderStyle.Solid;
                    //TreeViewAccts.BorderColor = Color.Aquamarine;
                    //TreeViewAccts.BackColor = Color.Yellow;

                    TreeViewAccts.Nodes.Add(tnParent);
                    FillTree_Child(tnParent, tnParent.Value);
                }
            }
            catch (Exception ae)
            {
                Response.Write(ae.Message);
                //ErrorLogging.WriteToEventLog(ae);
            }
        }

    }

    public void FillTree_Child(TreeNode parent, string ParentId)
    {  // fills the child action items of the tree
        //int RoleID = Convert.ToInt32(ddlRole.SelectedValue);
        using (SqlConnection con5 = new SqlConnection(ConfigurationManager.ConnectionStrings["PBRConnectionString"].ConnectionString))
        {
            try
            {
                SqlCommand cmd2 = new SqlCommand("SELECT [ACCT_NUM], [ACCT_PK], [ACTIVE_FLG], [DOS] FROM [ACCT_LIST] where [ACCT_GRP_FK] = '" + ParentId + "' ORDER BY ACCT_NUM ASC", con5);
                SqlDataAdapter da = new SqlDataAdapter(cmd2);
                DataSet PDataset = new DataSet();
                da.Fill(PDataset, "ACCT_NUM");
                DataSet ds = PDataset;
                parent.ChildNodes.Clear();
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    DateTime date = DateTime.Parse(dr["DOS"].ToString());
                    string formatted = date.ToString("MM/dd/yyyy");
                    TreeNode child = new TreeNode();

                    child.Text = dr["ACCT_NUM"].ToString().Trim() +
                        " &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" +
                        "Active: " +
                        dr["ACTIVE_FLG"].ToString() +
                        " &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" +
                        "Service Date: " + formatted + "";
                    child.Value = dr["ACCT_PK"].ToString().Trim();

                    //  if (child.Node.Count == 0)
                    // child.PopulateOnDemand = true;
                    //child.ToolTip = "Click to get Child";
                    // child.SelectAction =TreeNodeSelectAction.SelectExpand;
                    child.CollapseAll();
                    parent.ChildNodes.Add(child);

                    //TreeViewAccts.BorderStyle = BorderStyle.Solid;
                    //TreeViewAccts.BorderColor = Color.Aquamarine;
                    //TreeViewAccts.BackColor = Color.Yellow;
                }

            }
            catch (Exception ae)
            {
                Response.Write(ae.Message);
                //ErrorLogging.WriteToEventLog(ae);
            }

        }

    }

    protected void TreeView1_SelectedNodeChanged1(object sender, EventArgs e)
    {  // handles action items from the tree when selected

        ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "selectNode", "var elem = document.getElementById('" + TreeViewAccts.ClientID + "_SelectedNode');var node = document.getElementById(elem.value);node.scrollIntoView(true);elem.scrollLeft=0;", true);


        //    txtTreeselect.Text = TreeView1.SelectedNode.Text.ToString();
        if (TreeViewAccts.SelectedNode.Text == "External Pend")
        {
            txtTreeselect.Text = TreeViewAccts.SelectedValue.ToString();
            //Module.Text = TreeView1.SelectedNode.Text.ToString();
        }
        else if (TreeViewAccts.SelectedNode.Text == "Internal Pend")
        {
            txtTreeselect.Text = TreeViewAccts.SelectedValue.ToString();
            // Module.Text = TreeView1.SelectedNode.Text.ToString();
        }
        else
        {
            TreeViewAccts.CollapseAll();
            txtTreeselect.Text = TreeViewAccts.SelectedValue.ToString();

            // txtTreeselect.Text = TreeView1.SelectedValue.ToString();
            // Module.Text = TreeView1.SelectedNode.Text.ToString();
        }

    }

    public static string SpaceCount(string Incoming, int TSpace)
    {
        // Cached length.
        int cl = TSpace - Incoming.Length;
        string Spacer = "";
        int i = 0;
        do
        {
            Spacer = Spacer + "&nbsp;";
            i++;
        } while (i < cl);

        return Spacer;

    }


}

1 个答案:

答案 0 :(得分:0)

在展开节点时,您的网页似乎正在回发。如果是这种情况,那么您需要使用JavaScript解决方案来通过回发来维护页面的滚动位置。这个网站有一个很好的教程:http://www.4guysfromrolla.com/articles/111704-1.aspx