在 GridView 上显示 asp.net 中的文件图标

Displays file icons in asp.net on GridView

我无法在 asp.net GridView 中根据文件扩展名显示文件图标。

GridView的结构是nested

教程是Displays file icons in asp.net

错误是:

Object reference not set to an instance of an object

代码隐藏这一行中:

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

您有一个嵌套的 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
            }
        }
    }
}