如何调用的ItemTemplate里面的函数在ListView控件 [英] How to call a function inside a ItemTemplate at a ListView Control

查看:174
本文介绍了如何调用的ItemTemplate里面的函数在ListView控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ListView控件,需要插入一些图片在ItemTemplate。

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 />

在这一点上,我需要,如果文件名中包含Varenr插入图片
事情是这样的。

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

但我现在downt如何得到这个工作:)
在ListView code的其余部分是这样的。

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>


推荐答案

使用ItemDataBound事件,并把要被插入您的图片,在你的ItemDataBound事件的占位符控制找到占位符控制和动态添加您的图像,你code背后应该这样。

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.

如果你想在html可以嵌套DataList和assing更多的控制它的数据源

If you want more control over the Html you can nest a DataList and assing it a Datasource

例如

 <asp:DataList ID="nestedImages" runat="server" 
        DataSource="<%# GetDataSourceForImages(((ListViewItem)Container).DataItem) %>" >
   <ItemTemplate> 
         your html here
   </ItemTemplate>

GetDataSourceForImages是一个受保护的方法做你想做的,并返回将用作数据源列表,你必须细化当然这一点。

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.

希望它能帮助

这篇关于如何调用的ItemTemplate里面的函数在ListView控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆