Question

I am working on a page that displays the pdf files in a specific directory in a grid, along with a link to the file.

I am modifying Scott Mitchell's example here: http://aspnet.4guysfromrolla.com/articles/052803-1.aspx

I converted the code from vb to c#.

<%@ Import Namespace="System.IO" %>
<script language="C#" runat="server">
public void Page_Load(object sender, EventArgs e)
{
DirectoryInfo dirInfo = new DirectoryInfo(Server.MapPath(""));

articleList.DataSource = dirInfo.GetFiles("*.pdf");
articleList.DataBind();
}
</script>

<asp:DataGrid runat="server" id="articleList" Font-Name="Verdana"
AutoGenerateColumns="False" AlternatingItemStyle-BackColor="#eeeeee"
HeaderStyle-BackColor="Navy" HeaderStyle-ForeColor="White"
HeaderStyle-Font-Size="15pt" HeaderStyle-Font-Bold="True">
<Columns>
<asp:HyperLinkColumn DataNavigateUrlField="Name" DataTextField="Name" 
       HeaderText="File Name" target="_blank"/>
<asp:BoundColumn DataField="LastWriteTime" HeaderText="Last Write Time"
    ItemStyle-HorizontalAlign="Center" DataFormatString="{0:d}" />
</Columns>
</asp:DataGrid>

The above code works in displaying the files. What I would like to do now is add grid filtering.

The file name is displayed in the grid as a link to the pdf. How can I add a text field that lets you filter/search for a specific file name, or a file name that begins with __?

Also, would it be possible to keep the browser from caching the pdf, since all my page does is provide a link to it?

Any help or ideas would be appreciated.

Thanks.

Was it helpful?

Solution

Try like Below it will help you....

In HTML Design View, Before the DataGridView add the Below code, It will create the Textbox and Button

HTML:

    Enter the Name of the file : <asp:TextBox ID="txtFilter" runat="server"></asp:TextBox>
        <asp:Button ID="btnShow"
            runat="server" Text="ShowData" onclick="btnShow_Click" />

Add Button Click event like below...

CS:

        protected void btnShow_Click(object sender, EventArgs e)
        {
         ShowData();
        }

        public void ShowData()
        {
            string FilterValue = txtFilter.Text.ToUpper();
            DirectoryInfo dirInfo = new DirectoryInfo(Server.MapPath(""));

            FileInfo[] info = dirInfo.GetFiles("*.zip");            //Get FileInfo and Save it a FileInfo[] Array

            List<Getfiles> _items = new List<Getfiles>();          // Define a List with Two coloums

            foreach (FileInfo file in info) //Loop the FileInfo[] Array
               _items.Add(new Getfiles { Name = file.Name, LastWriteTime = file.LastWriteTime.ToString("MM/dd/yyyy") });  // Save the Name and LastwriteTime to List


           //you can use Any one the Filtered list from the below...

            var tlistFiltered = _items.Where(item => item.Name.ToUpper() == FilterValue); // Find the File by their File Name
            var tlistFiltered1 = _items.Where(item => item.Name.ToUpper().Contains(FilterValue)); // Find the file that Contains Specific word in its File Name
            var tlistFiltered2 = _items.Where(item => item.Name.ToUpper().StartsWith(FilterValue));// Find tha File that StartsWith Some Specific Word


            articleList.DataSource = tlistFiltered; //Assign the DataSource to DataGrid
            articleList.DataBind();

        }

        public class Getfiles
        {
            public string Name { get; set; }
            public string LastWriteTime { get; set; }
        }

OutPut Screen :

enter image description here

OTHER TIPS

You could try using an ObjectDataSource and then passing that to your DataGrid. The Object Data Source would wrap the Directory Info calls.

Once you have an ObjectDataSource you should be able to use the DataGrid's built in filtering and sorting functionality.

Scott Mitchel has a few tutorials on this:

http://msdn.microsoft.com/en-us/library/aa581784.aspx

And here's one for using Sql, but you should be able to easily adapt it to pull a file list:

http://asp-net-example.blogspot.nl/2008/11/aspnet-gridview-and-objectdatasource.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top