2017-10-20 136 views
1

我無法根據asp.net GridView中的文件擴展名顯示文件圖標。在GridView上顯示asp.net中的文件圖標

的GridView的結構nested

該教程是Displays file icons in asp.net

錯誤是:

對象引用不設置爲一個對象

在這種的一個實例代碼行

if (!String.IsNullOrEmpty(lnkDownload.Text)) 

我的代碼如下。

你能幫我嗎?

預先感謝您的任何幫助,真的很感激。

<asp:TemplateField> 
    <ItemTemplate> 
     <img alt="" style="cursor: pointer" src="images/plus.png" /> 
     <asp:Panel ID="pnlOrders" runat="server" Style="display: none"> 
      <asp:GridView ID="gvOrders" runat="server" 
      AutoGenerateColumns="false" CssClass="mGrid" Width="700" 
      HorizontalAlign="Center"> 
       <Columns> 
        <asp:TemplateField> 
         <ItemTemplate> 
          <img id="fileImage" runat="server" src="" /> 
          <asp:HiddenField ID="HiddenField1" 
        runat="server" Value='<%# Eval("Name") %>' /> 
         </ItemTemplate> 
        </asp:TemplateField> 
        <asp:TemplateField HeaderText="Download" ItemStyle- 
        HorizontalAlign="Justify"> 
         <ItemTemplate> 
          <asp:LinkButton ID="lnkDownload" Text='<%# 
          Eval("Name") %>' CommandArgument= 
          '<%# Eval("FullName") %>' runat="server" 
           OnClick="lnkDownload_Click" 
           OnClientClick="if (!confirm('Confirm ?')) 
           return false;"></asp:LinkButton> 
         </ItemTemplate> 
        </asp:TemplateField> 
       </Columns> 
      </asp:GridView> 
     </asp:Panel> 
    </ItemTemplate> 
</asp:TemplateField> 



protected void OnRowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    if (e.Row.RowType == DataControlRowType.DataRow) 
    { 
     string root = @FilePath; 
     string folder = GridView2.DataKeys[e.Row.RowIndex].Value.ToString(); 

     GridView gvOrders = (GridView)e.Row.FindControl("gvOrders"); 
     Label gvLabel = (Label)e.Row.FindControl("gvLabel"); 
     Label gvFolder = (Label)e.Row.FindControl("gvFolder"); 


     DirectoryInfo directory = new DirectoryInfo(root + "/" + folder); 
     FileInfo[] fileInfo = directory.GetFiles("*.*", 
     SearchOption.AllDirectories); 

     fCount = directory.GetFiles("*.*", 
     SearchOption.AllDirectories).Length; 
     gvLabel.Text = fCount.ToString(); 

     long size = 0; 
     foreach (string file in Directory.GetFiles(root + "/" + folder, 
     "*.*", SearchOption.AllDirectories)) 
     { 
      size += new FileInfo(file).Length; 
     } 

     gvFolder.Text = Math.Round((double)size/(double)(1024 * 1024), 
     2).ToString() + " MB"; 

     LinkButton lnkDownload = 
     (LinkButton)e.Row.FindControl("lnkDownload"); 
     HiddenField hf = (HiddenField)e.Row.FindControl("HiddenField1"); 

     if (!String.IsNullOrEmpty(lnkDownload.Text)) 
     { 
      HtmlImage image = (HtmlImage)e.Row.FindControl("fileImage"); 
      image.Attributes.Add("src", GetIconForFile(hf.Value)); 
     } 

     gvOrders.DataSource = fileInfo; 
     gvOrders.DataBind(); 
    } 
} 

private string GetIconForFile(string fileText) 
{ 
    string extension = Path.GetExtension(fileText); 
    extension = extension.Trim('.').ToLower(); 
    return "~/fileicons/" + extension + ".png"; 
} 
+1

你看過http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-我已經修好了嗎? '(LinkBut​​ton)e.Row.FindControl(「lnkDownload」)'如果控件不存在則返回null,然後在訪問它的'Text'屬性時拋出NRE。 –

+0

你正在使用哪種結構?嵌套或簡單? – AsifAli72090

+0

@ Asif.Ali嵌套 –

回答

1

你有一個嵌套的GridView結構和gvOrders是你的內部GridView。所以,你已經從內部GridView的行獲得的LinkBut​​ton像:

protected void OnRowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    if (e.Row.RowType == DataControlRowType.DataRow) 
    { 
     GridView gvOrders = (GridView)e.Row.FindControl("gvOrders"); 

     foreach (GridViewRow row in gvOrders.Rows) 
     { 
      LinkButton lnkDownload = (LinkButton)row.FindControl("lnkDownload"); 
      HiddenField hf = (HiddenField)row.FindControl("HiddenField1"); 

      if (!String.IsNullOrEmpty(lnkDownload.Text)) 
      { 
       //... some code 
      } 
     } 
    } 
}