1

エラーは単純ですが、これを解決することはできません。皆さんが私を助けてくれることを願っています

これは私のaspxページコードです

 <asp:TreeView ID="PictureTree" runat="server" ShowLines="True">
                    <SelectedNodeStyle Font-Bold="True" />
                    <NodeStyle ImageUrl="~/Images/Folder.jpg" />
                </asp:TreeView>

            </td>
            <td style="width:auto;text-align:center;" valign="top">
                <asp:DataList ID="PicturesInFolder" runat="server" Width="100%" CellPadding="5">
                    <ItemTemplate>
                        <h3><asp:Label runat="server" ID="FileNameLabel" Text='<%# System.IO.Path.GetFilenameWithoutExtension(Eval("Name")) %>'></asp:Label></h3>

                        <asp:Image runat="server" ID="Picture" ImageUrl='<%# PictureTree.SelectedValue & Eval("Name").ToString() %>' />
                        <br /><br />
                    </ItemTemplate>
                    <AlternatingItemStyle BackColor="#E0E0E0" />
                </asp:DataList>
                <asp:Label runat="server" ID="NoPicturesInFolderMessage" Font-Italic="True" Visible="False" EnableViewState="False">
                    There are no pictures in the selected folder...
                </asp:Label>

これは私の.csコードです

private const object VirtualImageRoot = "~/Images/Departments/";


protected void Page_Load(object sender, System.EventArgs e)
{
    // On the first page visit, populate the photo tree and select the root node
    if (!Page.IsPostBack)
    {
        PopulateTree();
        PictureTree.Nodes[0].Select();
        PictureTree_SelectedNodeChanged(PictureTree, EventArgs.Empty);
    }
}

private void PopulateTree()
{
    // Populate the tree based on the subfolders of the specified VirtualImageRoot
    DirectoryInfo rootFolder = new DirectoryInfo(Server.MapPath(VirtualImageRoot));
    TreeNode root = AddNodeAndDescendents(rootFolder, null);
    // Add the root to the TreeView
    PictureTree.Nodes.Add(root);
}

private TreeNode AddNodeAndDescendents(DirectoryInfo folder, TreeNode parentNode)
{
    // Add the TreeNode, displaying the folder's name and storing the full path to the folder as the value...
    string virtualFolderPath;
    if ((parentNode == null))
    {
        virtualFolderPath = VirtualImageRoot;
    }
    else
    {
        virtualFolderPath = (parentNode.Value
                    + (folder.Name + "/"));
    }
    TreeNode node = new TreeNode(folder.Name, virtualFolderPath);
    // Recurse through this folder's subfolders
    DirectoryInfo[] subFolders = folder.GetDirectories();
    foreach (DirectoryInfo subFolder in subFolders)
    {
        TreeNode child = AddNodeAndDescendents(subFolder, node);
        node.ChildNodes.Add(child);
    }
    return node;
    // Return the new TreeNode
}

protected void PictureTree_SelectedNodeChanged(object sender, System.EventArgs e)
{
    // Refresh the DataList whenever a new node is selected
    DisplayPicturesInFolder(PictureTree.SelectedValue);
}

private void DisplayPicturesInFolder(string virtualFolderPath)
{
    // Security check: make sure folderPath starts with VirtualImageRoot and doesn't include any ".."
    if ((!virtualFolderPath.StartsWith(VirtualImageRoot)
                || (virtualFolderPath.IndexOf("..") >= 0)))
    {
        throw new ApplicationException("Attempting to view a folder outside of the public image folder!");
    }
    // Get information about the files in the specified folder
    DirectoryInfo folder = new DirectoryInfo(Server.MapPath(virtualFolderPath));
    FileInfo[] fileList = folder.GetFiles();
    PicturesInFolder.DataSource = fileList;
    PicturesInFolder.DataBind();
    // If there are no pictures in the folder, display the NoPicturesInFolderMessage Label
    PicturesInFolder.Visible = (PicturesInFolder.Items.Count > 0);
    NoPicturesInFolderMessage.Visible = !PicturesInFolder.Visible;
}
}

プライベート const オブジェクト VirtualImageRoot = "~/Images/Departments/"; でエラーが発生しました。

その VirtualImageRoot はオブジェクトです 文字列以外の参照型の const フィールドは null でのみ初期化できます

どうすればこれを解決できますか 事前に感謝します

4

1 に答える 1