1

我有一个中继器控件来列出根文件夹中的所有子文件夹和文件。

<table border="1px solid " style="border-collapse: collapse;width: 90%;" cellspacing="3" cellpadding="3">
<asp:Repeater ID="Repeater1" runat="server" OnItemCommand="Repeater1_ItemCommand"
                        OnItemDataBound="Repeater1_ItemDataBound">

 <ItemTemplate>

     <tr>
       <td>
          <asp: Label ID="imagelabel"..../>// display image here
       </td>
       <td>
          <asp:LinkButton ID="lnkFolder" runat="server" Text='<%# Eval("Name") %>' CommandName="Select"></asp:LinkButton>
       </td>
       <td>
         <asp:LinkButton ID="LinkButton1" runat="server" Text="Delete" CommandArgument='<%# Eval("Path") +"," +  Eval("Type")%>'
                                            CommandName="Delete" OnClientClick="return confirm('Are you sure you want to delete?')"></asp:LinkButton>
        </td>
       <td>
          <asp:LinkButton ID="LinkButton2" runat="server" Text="Edit" CommandArgument='<%# Eval("Path") + "," +  Eval("Type") + "," + Eval("Name")%>'
                                            CommandName="Edit"></asp:LinkButton>
        </td>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>

在后面的代码中:

 private void FillFileAndFolders(string virtualDirPath)
    {
        DataTable filesAndFolders = new DataTable();
        filesAndFolders.Columns.Add("Name");
        filesAndFolders.Columns.Add("Path");
        filesAndFolders.Columns.Add("Type");

        string rootPath = virtualDirPath;
        if (Directory.Exists(rootPath))
        {
            string[] Directories = Directory.GetDirectories(rootPath);
            for (int i = 0; i < Directories.Length; i++)
            {
                DirectoryInfo drinfo = new DirectoryInfo(Directories[i]);
                DataRow dr = filesAndFolders.NewRow();
                dr["Name"] = drinfo.Name;
                dr["Path"] = drinfo.FullName;
                dr["Type"] = "D";
                filesAndFolders.Rows.Add(dr);
            }
            string[] files = Directory.GetFiles(rootPath);
            for (int i = 0; i < files.Length; i++)
            {
                FileInfo fInfo = new FileInfo(files[i]);
                DataRow dr = filesAndFolders.NewRow();
                dr["Name"] = fInfo.Name;
                dr["Path"] = fInfo.FullName;
                dr["Type"] = "F";

                filesAndFolders.Rows.Add(dr);
            }
        }
        Repeater1.DataSource = filesAndFolders;
        Repeater1.DataBind();
    }

我想要的是:当它是Folder--->时imagelabel.Text= "<img src='/folder.png'>"

否则当它是一个单一的file----> imagelabel.Text= "<img src='/file.png'>";

我已经尝试了很多次,但似乎我无法imagelabel通过中继器控件将该图像设置为。

帮助!!!

4

1 回答 1

1

您可以使用转发器控件的ItemDataBound事件。使用RepeaterItem.DataItem属性,您可以访问获取与Repeater 控件中的RepeaterItem 对象关联的数据项。

类似以下内容应设置 Text 属性:

protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    // Execute the following logic for Items and Alternating Items.
    if (e.Item.ItemType == ListItemType.Item ||
        e.Item.ItemType == ListItemType.AlternatingItem)
    {
        if (((DataRowView)e.Item.DataItem).Row["Type"].ToString() == "D")
        {
            ((Label)e.Item.FindControl("imagelabel")).Text = "<img src='/folder.png'>";
        }
        else
        {
            ((Label)e.Item.FindControl("imagelabel")).Text = "<img src='/file.png'>";
        }
    }
}
于 2013-10-24T16:41:06.350 回答