Search code examples
asp.netrepeater

ASP.NET Repeater question


I have a repeater control and under the ItemTemplate, I have Image control. Anyway the old

How can I set the ImageUrl programatically?

Anyway, the old html code I have was like this:

<ItemTemplate>
   <img src="<%# Eval("ImageSource") %>" alt="" />
</ItemTemplate>

But I want to check if the image exists in directory or not then I can setup with temp image.

I have a code but .. it's not really working so there's no sense of showing it here. Can you guys help me? Should I use ItemCreated or ItemDataBound event?


Solution

  • In the xml side in the template, you need to call a method directly.

    <asp:Image runat="server" ID="myImg" ImageUrl='<%# MyImageUrlFunction(Eval("DataFieldName").ToString()); %>' />
    

    You need a corresponding method in the code behind defined publicly:

    public string MyImageUrlFunction(string field) 
    {
        // put some logic here to determine url
        return imageUrl;
    }