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

CSS and html

id0 is the class for the div that received the background as a sprite image and inside this div .. there is a list of links (in the repeater) .. when the user hovered over the links .. the background image div displays diff parts of the sprite respectively

Now I want the id1-id5 classes to be set as relay list classes ... now, how can I get away from it?

like the list of links inside the repeater, comes from the database. How can I create div tags inside this repeater

and how to install a class for each of the 5 sections that will be created, and install these classes on them?

as before, I had simple markup, but now I need to generate this list of links using a relay .. how do I apply CSS now?

Please give some ideas ..thnx

[EDIT] OK tried this .. added the div tag in the repeater after the label and in the code behind: - rpt1.FindControl ("myDiv"). Controls.Add (class = ??) // what to enter here to use for a loop or what? [edit] this does not work .. is something wrong? **

for(int i=1;i<6;i++) { rpt1.FindControl("myDiv").Controls.Add("class=id[i]"); } 

The above error: -

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

Now how to set classes for div?

pch..made dilly error..changed changes ..

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

still the same mistakes.

[edit]

tried the following .. doesn't work

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

[edit]

I tried the following:

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

he says: "Cannot apply indexing with [] to an expression like" method group "" ???

+4
source share
5 answers

ItemDataBinding Method

To get an element inside the ItemTemplate repeater, you must declare it runat = "Server" and set an identifier for it (id is not necessary, but simplifies). If you do this 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) { ((HtmlGenericControl)(e.Item.FindControl("myDiv"))). Attributes["class"] = "id" + index++; } } 

You need to declare an index variable and increment it when setting each class.


Alternative way

An even simpler and clearer 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> </asp:Repeater> 

and in the code behind the declaration of the data acquisition method

 private int index = 1; protected string GetDivClass() { return "div" + index++; } 
+13
source

If you create a div as an HTML control, then set the class as follows:

 HtmlControl div = new HtmlGenericControl("div"); div.Attributes.Add("class", "myClassName"); 

Otherwise, if you created it as a server control (by including the runat="server" attribute), you can find it inside your hierarchy and add the attribute.

+2
source

Try adding both ID and runat = "server" to your DIV tags. This will make each DIV a control:

 <li><a href=# class="mysprite id1">Text1<div id="myDiv" runat="server">&nbsp;</div></a><br /></li> 

Thus, the line of code you use to dynamically add a class (below) should work:

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

I'm not sure I understand what you are trying to achieve exactly, but .Controls.Add ("classblah") will not work.

This is intended if you want to place a control in that control, for example.

 HyperLink myHyperLinkControl; rpt1.FindControl("myDiv").Controls.Add(myHyperLinkControl); 

which will display approximately

 <div id="myDiv"> <a href="#">my hyperlink</a> </div> 

You want to capture li and manage its attributes.

 var li = nav.FindControl("name") as HtmlGenericControl; nav.attributes["class"] = "mysprite id" + Container.DataItemIndex 
+1
source

This is the best

 <asp:Repeater ID="rptDocuments" runat="server"> <ItemTemplate> <div class='element<%# Container.ItemIndex%>'/> </ItemTemplate> </asp:Repeater> 
0
source

All Articles