错误:除string之外的引用类型的const字段只能用null初始化

时间:2013-06-21 11:43:04

标签: c# asp.net image

错误很简单,但我无法解决这个问题,希望你们帮助我这个

这是我的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是一个对象一个除了string之外的引用类型的const字段只能用null初始化

我怎么能提前解决这个问题

1 个答案:

答案 0 :(得分:14)

const支持极少数类型;如果你的不是其中之一,那么 在一般情况下 你应该使用static readonly代替:

private static readonly object VirtualImageRoot = "~/Images/Departments/";

然而,非常不清楚为什么在你的情况下这不是string

private const string VirtualImageRoot = "~/Images/Departments/";
相关问题