错误很简单,但我无法解决这个问题,我希望你们能帮助我
这是我的 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 初始化
我该如何解决这个问题提前谢谢