Using ASP Container.ItemIndex in a C # if expression

I had a problem with ASP and C # without any experience working on a server on which I have limited access and where I do not see the correct error logs. So get ready for a potentially confusing question :)

We have a loop (ASP repeater) in the HTML template, and part of the HTML in the template should only be included in the first iteration of the loop. Unfortunately, I cannot just hide html for other iterations, I need to remove it.

Essentially, I want to do this:

<% if (Container.ItemIndex == 0) { %> Lots of HTML here <% } %> 

The problem is that this leads to an error (and I don’t see the logs, so I don’t know exactly why ...). I know that the if statement itself works (setting 0 == 0 instead works as an example), and I can get the index fine, just not here.

So the question is how to do something like this? Can I access the index in a C # if statement or is there something built into the relay that I can use?

+4
source share
1 answer

Instead of using the If statement, I would enter the content “0 only index” into the ASP.NET Placeholder web control and set visible=true only for the first element.

As @Amiram stated, you can do:

 <asp:Placeholder id="ph" runat="server" visible="<%# (bool)(Container.ItemIndex==0) %>"> ...0 Index only content </asp:Placeholder> 

Alternatively, set visible="false" and change this value in the ItemDataBound event:

 if (e.Item.ItemIndex == 0){ (placeholder)e.Item.FindControl("ph").visible = true; } 
+7
source

All Articles