tags:

views:

122

answers:

1

Hi, I'm having some evils trying to get my GridView control to behave. I have the below code, which successfully displays all the files in the directory. However I require two changes, both of which I am struggling with:

a) Currently the URL you get when clicking on the URL field is http://localhost/LBSExplorer/SharedUser.csv (ie my home directory with the filename).

What I require is that the 'Display Text' be the filename only, and the URL be my desired text followed by the filename eg: http://mystuff/page.aspx?FileID=SharedUser.csv

b) I want only to see the files that start with a certain prefix eg "Pay". I can do that with something like: string[] filelist = Directory.GetFiles((@"C:\MF\Data\","Pay*.*"); but this doesn't like to bind to my Gridview!

I'd appreciate your help!

Mark

        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);
    }
}
+1  A: 

What you are looking for is the DataNavigateUrlFormatString property.

hyperLinkField.DataNavigateUrlFormatString = "http://mystuff/page.aspx?FileID={0}";

The {0} here is replaced with your DataNavigateUrlFields value.

Bryan
Yeeeeees! I spent hours looking for that! You are a genius and I'll be erecting a small shrine in your honour.
Glinkot