How to call a function inside a ItemTemplate at a ListView Control
I have a ListView Control and need to insert some pictures in the ItemTemplate.
<asp:ListView runat="server" ID="VareListView">
<LayoutTemplate>
<table cellpadding="2" runat="server" id="tblVarer" style="width:100%;border-collapse:collapse;" >
<tr runat="server" id="itemPlaceholder">
</tr>
</table>
</LayoutTemplate>
<ItemTemplate>
<tr id="Tr1" style="height:100px" class="tblRow" runat="server" >
<td valign="top">
<asp:Image ID="ProduktImage" runat="server" ImageUrl='<%# string.Format("images_produkt/{0}.jpg",Eval("Varenr"))%>' />
</td>
<td valign="top" class="">
<asp:Label ID="LastNameLabel" runat="server" Text='<%# Eval("Varenavn1")%>' />
<br />
<asp:Label ID="FirstNameLabel" runat="server" Text='<%# string.Format("Varenr: {0}",Eval("Varenr")) %>' />
<br />
<asp:Label ID="Varenavn2" runat="server" Text='<%#Eval("Varenavn2") %>' />
<br />
<asp:Label ID="Varenavn3" runat="server" Text='<%#Eval("Varenavn3") %>' />
<br /&开发者_运维百科gt;
at this point I need to insert pictures if a filename contains "Varenr" Something like this
DirectoryInfo myDir = new DirectoryInfo(Request.PhysicalPath.Substring(0, Request.PhysicalPath.LastIndexOf("\\")) + "/images_produkt/montering");
FileInfo[] files = myDir.GetFiles(varenr + "*");
if (files.Length != 0)
{
foreach (FileInfo fil in files)
{
Image img = new Image();
img.ImageUrl = "/images_produkt/montering/" + fil.Name;
img.Height = 20;
img.Width = 20;
}
}
But I downt now how to get this to work :) the rest of the ListView code is like this
</td>
<td valign="top" align="right" class="">
<asp:Panel ID="Enhet" runat="server" Visible='<%#Eval("Enhet").ToString() != String.Empty %>'>
<asp:Label ID="Pris" runat="server" Text='<%# string.Format("{0} kr",Eval("Pris2"))%>' />
<br />
</asp:Panel>
<asp:Label ID="Vekt" runat="server" Text='<%# string.Format("{0} Kg",Eval("Vekt"))%>' />
<br />
<asp:TextBox ID="Antall" runat="server" Text="1" Width="20"></asp:TextBox>
<asp:Image ID="Image1" runat="server" ImageUrl='style/shoping01.jpg' Width="22" Height="22" />
</td>
</tr>
Use the ItemDataBound event and place a PlaceHolder control where you want your images to get inserted, in your ItemDataBound event find the Placeholder control and add your images dynamically, your code behind should like this.
protected void yourdatalist_ItemDataBound(object sender, DataListItemEventArgs e)
{
var placeHolder = (PlaceHolder)e.Item.FindControl("YourPlaceholderIdHere")
//do what you want
placeHolder.Controls.Add(your images)
}
I have not tested that but hopefully can give you and idea.
If you want more control over the Html you can nest a DataList and assing it a Datasource
e.g.
<asp:DataList ID="nestedImages" runat="server"
DataSource="<%# GetDataSourceForImages(((ListViewItem)Container).DataItem) %>" >
<ItemTemplate>
your html here
</ItemTemplate>
GetDataSourceForImages is a protected method that do what you want and return a list that will be used as datasource, you have to refine this of course.
Hope it helps
You could handle the ItemDataBound event from your ListView control. In the event handler, examine the data item, and modify the content appropriately.
精彩评论