Search code examples
c#htmlasp.netimagerepeater

Repeater asp.net tag not working


I have a problem with the Repeater tag of ASP.NET. It simply doesn't generate the html code when the page is running. I'm trying to use it to generate a list of tag for a slideshow, here is the asp code:

<asp:Repeater id="foto" runat="server">
<ItemTemplate>
<img src='<%# (string)Container.DataItem %>'/>
</ItemTemplate>
</asp:Repeater>

And here is the code behind:

protected void Page_Load(object sender, EventArgs e)
{    
    if (IsPostBack)
    {
        int num = Directory.GetFiles(@"C:\Users\Paolo\Desktop\Podisti\Slideshow\" + Request.QueryString["Cartella"]).Length;
        List<string> elencoUrl = new List<string>();
        for (int i = 1; i <= num; i++)               
            elencoUrl.Add(@"Slideshow\" + Request.QueryString["Cartella"] + "00" + i + "_jpg.jpg");
        foto.DataSource = elencoUrl; //può essere ad esempio un Array o una List di stringhe
        foto.DataBind();
    }
}

EDIT: Thank you for the answer, even if it isn't the solution of the problem: the page i've created is only a test which, with the click of 2 linkbuttons, redirect to the same page with a different querystring so that the repeater can create different src attributes for the img tag every time. The postback is in here only to prevent an error in the first load of the page (where the querystring is null), I will changed it when I will solve this problem.

The main problem is that the repeater doesn't do its work, if i check the code of the page during its execution:

   <div style="width:500px;height:400px">        
    <div class="fotorama">
        /*The repeater doesn't create the <img> tags*/
    </div>             
</div>

Solution

  • if (IsPostBack)
    

    means your code will be executed only if you just posted back the page, but you probably want to show it as a default, not during a postback. Fix it to:

    if (!IsPostBack)
    

    if that's the case.

    Also, you should never try to reach outside your web app folder to get folders/files, because those won't be accessible on the server. Never hardcode paths like this!