0

I have a page where I'm returning a data set. I have been binding it to a repeater control but this only allows for two item templates, so two styles. However I'd like to have multiple styles for different data.

The context is I'm returning a "top 10" (whether by latest, by likes etc,), and I'm looking to style it so number 1 has it's own style, 2&3 have a second style, 4-10 have a third style.

Is there a away to achieve this while still returning a single data set as I would with a repeater?

Dave
  • 167
  • 17

2 Answers2

0

You can create a custom control and manage different behaviors according to data. Put this custom control in repeater template.

Click here for example

Community
  • 1
  • 1
Sria Pathre
  • 182
  • 1
  • 10
  • Hi, i have done that, so that I have selected a top 7, and i have 3 types of user control (1 x "top story", 2 x "second story" and 4 x "small story", so that I can bind each of the top 7 results to the relevant control. But, it simply puts each result in every control, 7 times. How do i make it put the results in only 1 user control? – Dave Jan 26 '16 at 12:10
  • I guess you can use 1 User Control. Pass some variable which can decide respective CSS / Title or behavior on load. – Sria Pathre Jan 27 '16 at 10:37
0

Using the below method, you can declare a counter before the loop and increment it at the end of each loop to keep track of your row number.

The rest of the answer taken in full from from: https://stackoverflow.com/a/14732922/2617732

Rather than use a repeater, you can just loop through the list in a similar MVC type way using the <% %> and <%= %> tags.

<table>
  <% foreach (var myItem in g) { %>
    <tr><td><%= myItem.title %></td></tr>
  <% } %>
</table>

As long as the property you're looping through is acessible from the aspx/ascx page (e.g. declared as protected or public) you can loop through it. There is no other code in the code behind necessary.

<% %> will evaluate the code and <%= %> will output the result.

Here is the most basic example:

Declare this list at your class level in your code behind:

public List Sites = new List { "StackOverflow", "Super User", "Meta SO" }; That's just a simple list of strings, so then in your aspx file

<% foreach (var site in Sites) { %> <!-- loop through the list -->
  <div>
    <%= site %> <!-- write out the name of the site -->
  </div>
<% } %> <!--End the for loop -->
Community
  • 1
  • 1
rdans
  • 2,179
  • 22
  • 32