显示图像取决于中继器控件中的文件类型
本文关键字:文件 类型 控件 中继器 显示图 图像 取决于 显示 | 更新日期: 2023-09-27 18:36:37
我有一个中继器控件来列出根文件夹中的所有子文件夹和文件。
<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
。
帮助!!!
可以使用转发器控件的 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'>";
}
}
}