[C#] Displays file icons in asp.net on GridView RRS feed

  • Question

  • User1872364149 posted

    Hi all,

    I can't to display file icons according to file extensions in asp.net GridView.

    The tutorial is Displays file icons in asp.net

    The structure of GridView is nested 

    I don't have error but the column of gridview for show icons is empty.

    My code below.

    Can you help me?

    Thank you in advance for any help, really appreciated.

            <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" 
                                <img id="fileImage" runat="server" src="" />

    <asp:Label ID="lblFileName" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
    <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"; foreach (GridViewRow row in gvOrders.Rows) { LinkButton lnkDownload = (LinkButton)e.Row.FindControl("lnkDownload"); Label FileName = (Label)e.Row.FindControl("lblFileName"); HiddenField hf = (HiddenField)e.Row.FindControl("HiddenField1"); if (!String.IsNullOrEmpty(FileName.Text)) { HtmlImage image = (HtmlImage)e.Row.FindControl("fileImage"); image.Attributes.Add("src", GetIconForFile(FileName.Text)); } } gvOrders.DataSource = fileInfo; gvOrders.DataBind(); } } private string GetIconForFile(string fileText) { string extension = Path.GetExtension(fileText); extension = extension.Trim('.').ToLower(); return "~/fileicons/" + extension + ".png"; }

    Friday, October 20, 2017 8:42 AM


  • User475983607 posted

    The design has many nested layers GridView(?)->Template->Panel->GridView->template->label.  FindControl looks in the current branch.  If FindControl cannot find the control is returns null.   I believe you need to Find the Panel first then find the the GridView as the GridView is within the panel.

    Use the VS debugger to find the correct path.  Set a break point, open the immediate window, and execute different FindControl paths until you find the correct syntax for your layered design.  Or drill into the child objects from a watch window.  You should be able to figure this out pretty quick using the debugger tools.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, October 20, 2017 9:54 AM