How to set class on div that is inside of repeater in code behind?

Tags: c#,,html,css

Problem :

CSS & html

id0 is the class for the div that's got background as a sprite image and inside of this div..there's a list of links (in repeater)..when the user hovers over links..the background image of div displays diff parts of sprite image accordingly

Now I want that the classes id1 to id5 be set as the classes of the repeater's how do I go abut it?

like the list of links inside of repeater is coming from the do I create div tags inside of this repeater

and how do I set the class for each of the 5 divs that will be created and set these classes on them ?

like earlier I had simple markup but now I have to generate that list of links using a how do I apply the CSS now ??

Please give some ideas..thnx

[EDIT] ok tried this.. added div tag in repeater after label and in code behind :- rpt1.FindControl("myDiv").Controls.Add(class= ??) //what to type here use for loop or what ? [edit] this doesnt work..whats wrong ?**

for(int i=1;i<6;i++)


The above's giving the following error:-

The best overloaded method match for 'System.Web.UI.ControlCollection.Add(System.Web.UI.Control)' has some invalid arguments

Now how do I set classes for the divs?

pch..made dilly mistake..made changes ..

 for (int i = 1; i < 6; i++)
                        string divClass = "id";
                        rpt1.FindControl("myDiv").Controls.Add("class=id" + i);


still the same error..


tried the following..doesnt work

rpt1.FindControl("myDiv").Attributes.Add("class","id" +i);


I tried the following now..

rpt1.FindControl("myDiv").Attributes["class"] = "id" + i;

it says "Cannot apply indexing with [] to an expression of type 'method group'" ???

Solution :

The ItemDataBinding way

In order to get hold of an element inside the repeater's ItemTemplate you have to declare it runat="Server" and set an id for it (id isn't really neccessary, but simplifies things). If you do that with your div you can get it inside your ItemDataBound event and set the class.

void Repeater_ItemDataBound(Object Sender, RepeaterItemEventArgs e) {
  if (e.Item.ItemType == ListItemType.Item
    || e.Item.ItemType == ListItemType.AlternatingItem)   
       Attributes["class"] = "id" + index++;

You need to declare the index variable and increment it as you set each class.

Alternative way

An even simpler and cleaner way IMHO is to set the class directly in the View using data binding

<asp:Repeater ID="rpt" runat="server">
  <ItemTemplate><div class="<%# GetDivClass() %>">a div</div></ItemTemplate>

and in the code behind declare the method for getting the data

private int index = 1;

protected string GetDivClass()
  return "div" + index++;

    CSS Howto..

    How can i make this border to the end?CSS

    How does css inheritance work exactly?

    The UI Designers and Marketing people at my company don't want “awkward line breaks”. How to do with CSS or JS? [closed]

    How to control number of items per row using media queries in Flexbox?

    How to get a frame off animated png using CSS?

    How To Insert Some Content Before An HTML Element In The Runtime Using CSS

    Bootstrap: Only Show Fixed Footer In Larger Screens?

    How style DIV when the id inclue strings in php?

    How to cancel CSS effect in Responsive Website Footer menu?

    How to extend a wrapped element background both left and right with different color?

    How to prevent default title behavior with CSS or JavaScript [duplicate]

    How to get my .toggleClass() menu to work properly?

    How to have jquery add a panel inside a panel in Bootstrap v3

    How to indent text in a select drop down menu using PHP & CSS? [REPHRASED]

    How to solve css double hover issue? Making an image map

    How can i remove all attributes from any css selector? To override and reset any css id selectors?

    How to make a header bar fold over the webpage css

    How to create both-sided dotted line around a title? [duplicate]

    How can I write in CSS if I'm using list tag for this? [closed]

    Slideshow flickering image on :hover

    How to calculate gradient color replacements?

    “ul li a:link, a:visited” and “ul li a, visited” how they different?

    CSS: how to keep image never modified with same “offset” all the time?

    How to replace fixed values in responsible form css

    How can i stop my text css from over-riding my form on my blog?

    How to use custom underline with breakline in CSS?

    How to use CSS attribute selector for an SVG element with namespaced attribute href?

    How to make the search form without the search button?

    How to make animation smooth

    How to pause a CSS keyframe animation when it ends the first cycle?