我有一些想法让我的GridView控件表现出来的弊端。我有以下代码,它成功显示目录中的所有文件。但是我需要两个改变,我正在努力解决这两个问题:
a)目前,点击URL字段时获得的URL是 http://localhost/LBSExplorer/SharedUser.csv(即带有文件名的主目录)。
我需要的是“显示文本”只是文件名,URL是我想要的文本,后跟文件名,例如: http://mystuff/page.aspx?FileID=SharedUser.csv
b)我只想看到以某个前缀开头的文件,例如“付费”。我可以这样做: string [] filelist = Directory.GetFiles((@“C:\ MF \ Data \”,“Pay *。*”); 但是这不喜欢绑定到我的Gridview!
感谢您的帮助!
标记
const string DocumentFolderPhysicalPath = (@"C:\MF\Data\");
const string DocumentFolderUrl = (@"C:\MF\Data\"); //"http://localhost/virtualfoldernameyouexposed/"; ; // now it is hardcoded but you could retreive it automatically
HyperLinkField hyperLinkField = new HyperLinkField();
hyperLinkField.DataTextField = "Name";
hyperLinkField.DataNavigateUrlFields = new string[] { "Name" };
//Would like this to work!
//HyperLinkField hyperLinkField2 = new HyperLinkField();
//hyperLinkField2.DataTextField = "Destination";
//hyperLinkField2.DataNavigateUrlFields = new string[] { (@"C:\MF\Data\") + "Name" };
GridView1.DataSource = GetDocuments(DocumentFolderPhysicalPath);
GridView1.Columns.Add(hyperLinkField);
GridView1.DataBind();
private System.IO.FileInfo[] GetDocuments(string physicalPath)
{
System.IO.DirectoryInfo directory =
new System.IO.DirectoryInfo(physicalPath);
if (directory.Exists)
{
return directory.GetFiles();
}
else
{
throw new System.IO.DirectoryNotFoundException(physicalPath);
}
}
答案 0 :(得分:1)
您正在寻找的是DataNavigateUrlFormatString属性。
hyperLinkField.DataNavigateUrlFormatString = "http://mystuff/page.aspx?FileID={0}";
此处的{0}将替换为您的DataNavigateUrlFields值。