2013-03-18 209 views
0

我正在一個頁面上顯示網格中的特定目錄中的pdf文件以及指向該文件的鏈接。DirectoryInfo GetFiles()過濾器網格

我在這裏修改斯科特·米切爾的例子:http://aspnet.4guysfromrolla.com/articles/052803-1.aspx

我從VB到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> 

上述代碼在顯示文件時起作用。我現在想要做的是添加網格過濾。

文件名顯示在網格中作爲pdf的鏈接。我如何添加一個文本字段,以便過濾/搜索特定的文件名或以__開頭的文件名?

另外,是否有可能讓瀏覽器無法緩存pdf,因爲我的所有頁面都提供了一個鏈接到它?

任何幫助或想法,將不勝感激。

謝謝。

回答

1

請嘗試下圖,它會幫助你......

在HTML設計視圖中,在DataGridView之前添加下面的代碼,它會創建文本框和按鈕

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" /> 

添加按鈕單擊事件像下面...

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; } 
     } 

輸出屏幕:

enter image description here

+0

這會有所幫助。該過濾器的工作原理,但我怎樣才能過濾使用輸入文本字段?感謝您的示例代碼。很有幫助。 – Jack 2013-03-18 16:34:18

+0

@Jack:我改變了我的代碼...現在就試試吧... – Pandian 2013-03-18 16:50:33

+0

我想我錯過了一些東西。根據你推薦的改變,頁面不使用文本框輸入,而是使用'var tlistFiltered1 = _items.Where中的'FilterValue'(item => item.Name.Contains(「FilterValue」));'我怎樣才能從文本框中定義「FilterValue」? – Jack 2013-03-18 17:21:21

0

您可以嘗試使用ObjectDataSource,然後將其傳遞給您的DataGrid。對象數據源將包裝目錄信息調用。

一旦你有一個ObjectDataSource,你應該可以使用DataGrid的內置過濾和排序功能。

斯科特·米切爾有幾個教程在此:

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

下面是一個使用SQL,但你應該能夠很容易地適應它拉一個文件列表:

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