Treeview的Web用户控制回发问题

时间:2013-11-18 23:37:33

标签: c# asp.net custom-controls code-behind

我正在尝试构建一个具有自定义文件资源管理器的网站。最终我希望进一步构建它并允许上传,但我面临一个问题,从后面的代码调用它。

我的用户控件包含Treeview,如果这样调用:

<fe:FileExplorer filePath="/Resources/" runat="server"></fe:FileExplorer>

一切正常,但是当我从后面的代码中调用它时:

FileExplorer uc = (FileExplorer)LoadControl("~/Controls/FileExplorer.ascx");
uc.filePath = "/Uploads/Newsletter/";
PAGECONTROLS.Controls.Add(uc);

我的帖子有问题。

问题似乎是在我获得所选节点时。当我使用.Net内联时,它会抓取所选节点,但是当我使用后面的代码时,似乎松开了帖子:

TreeNode nd = FolderTree.SelectedNode; <-- Always Null when called in code behind.

以下是Code Behind:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Troop_101.Controls
{
    public partial class FileExplorer : System.Web.UI.UserControl
    {
        public string FilePath;

        public string filePath{
            set { FilePath = value; }
            get { return FilePath; }

        }

        protected void Page_Load(object sender, EventArgs e)
        {
            string RootFilePath = Server.MapPath(FilePath);
            if (!IsPostBack)
            {
                DirectoryInfo rootDir = new DirectoryInfo(RootFilePath);
                TreeNode rootNode = new TreeNode(rootDir.Name, rootDir.FullName);
                FolderTree.Nodes.Add(rootNode);
                TraverseTree(rootDir, rootNode);
                FolderTree.CollapseAll();
            }

            string ReturnStr = "";
            TreeNode nd = FolderTree.SelectedNode;
            if (nd != null)
            {
                ReturnStr = getFolderContent(nd.Value);
            }
            else
            {
                ReturnStr = getFolderContent(RootFilePath);
            }
            RESOURCE_FolderContent.InnerHtml = ReturnStr;

            foreach (TreeNode tn in FolderTree.Nodes)
            {
                tn.Expand();
            }

        }
        private void TraverseTree(DirectoryInfo currentDir, TreeNode currentNode)
        {
            foreach (DirectoryInfo dir in currentDir.GetDirectories())
            {
                TreeNode node = new TreeNode(dir.Name, dir.FullName);
                currentNode.ChildNodes.Add(node);
                TraverseTree(dir, node);
            }
        }

        private string getFolderContent(string filePath)
        {
            string ReplacePath = Server.MapPath(FilePath);

            var info = new DirectoryInfo(filePath);
            var fileInfo = info.GetFiles();
            string LinkTemplate = "<table><tr><td><a href=\"{0}\"><img src=\"{1}\" height=\"25px\"></a></td><td style=\"vertical-align:center;\"><a href=\"{0}\">{2}</a></td></tr></table>";

            string ReturnFiles = "";

            if (fileInfo.Length <= 0)
            {
                ReturnFiles = "No Files In Folder<br />";
            }
            foreach (FileInfo file in fileInfo)
            {
                string FileExt = file.Extension.ToLower().Replace(".", "");
                if (!File.Exists(HttpContext.Current.Server.MapPath("/images/ExtensionIcons/" + FileExt + ".png")))
                    FileExt = "_blank";


                string fir = filePath.Replace(ReplacePath, "");

                ReturnFiles += String.Format(LinkTemplate, FilePath + fir + "/" + file.Name, "/images/ExtensionIcons/" + FileExt + ".png", file.Name);
            }
            return ReturnFiles;
        }
    }
}

这是.NET

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="FileExplorer.ascx.cs" Inherits="Troop_101.Controls.FileExplorer" %>

<div style="width:90%; border:thick solid #656565;margin:0 auto; background-color:#ffffff; padding:5px;" class="tableroundcorners">
    <style type="text/css">
        .node_left {
            padding:5px;
        }
    </style>
    <table style="width:100%;height:350px;">
        <tr>
        <td id="RESOURCE_FileTree" runat="server" style="width:150px;border-right:solid #757575 thin;overflow:auto;">

            <asp:TreeView ID="FolderTree" runat="server" NodeIndent="15" ShowLines="True" LineImagesFolder="~/Controls/FileExplorerTree">
                <HoverNodeStyle Font-Underline="True" ForeColor="#6666AA" />
                <NodeStyle Font-Names="Tahoma" Font-Size="8pt" Width="100%" CssClass="node_left" ForeColor="Black" HorizontalPadding="0px" NodeSpacing="0px" VerticalPadding="2px" />
                <ParentNodeStyle Font-Bold="False" />
                <SelectedNodeStyle Font-Underline="true" HorizontalPadding="0px" VerticalPadding="0px" BackColor="#cccccc" />
            </asp:TreeView>

        </td>
        <td id="RESOURCE_FolderContent" runat="server" style="text-align:left;overflow:auto;">
            &nbsp;
        </td>
        </tr>
    </table>
</div>

1 个答案:

答案 0 :(得分:0)

在后面的代码中确保您在OnInit方法中添加控件以确保在处理回发数据之前(在页面加载事件之前)存在控件

http://i.msdn.microsoft.com/dynimg/IC386473.png(ASP.NET LifeCycle)