tags:

views:

64

answers:

4

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 list...now how do I go abut it?

like the list of links inside of repeater is coming from the DB..how 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 repeater..so 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++)
                {
            rpt1.FindControl("myDiv").Controls.Add("class=id[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..

[edit]

tried the following..doesnt work

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

[edit]

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'" ???

+1  A: 

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

That's intended for when you wish to put a control within that control eg.

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

which would render approximately

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

You want to grab the li and manipulate it's attributes.

var li = nav.FindControl("name") as HtmlGenericControl; 
nav.attributes["class"] = "mysprite id" + Container.DataItemIndex
danswain
all i want is to apply those five css classes to the DIVS of the list items in the repeater..plz tell me whats confusing..I ll clarify..also could u plz check my updates? why wont that work ?
Serenity
From what I see above the DIV id="div1" is outside the repeater?
danswain
ah i get it now... others have given what I'd do
danswain
+1  A: 

If you create the div as an HTML control, then set the class this way:

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

Otherwise if you have created it as a server control (by including the runat="server" attribute) then you can search for it within you control heirarchy and add the attribute.

slugster
tried that..its not giving compilation error f any sort now but its not that i hover mouse over links..the sprite image in the back ground is not changing :(
Serenity
@Happy - when you right click->view source on the rendered page, is you class attribute on the div as you expect? If so then it is a problem with your css class. If not then it is a problem with the way you are adding the attribue. Have a look and let us know what is going on.
slugster
+1  A: 

Try adding and 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>

That way, the line of code you are using to dynamically add the class (below) should work:

rpt1.FindControl("myDiv").Attributes.Add("class","id" +i);
BradB
I did add runtat attribute
Serenity
its giving me thsi error "'System.Xml.Linq.Extensions.Attributes(System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement>, System.Xml.Linq.XName)' is a 'method', which is not valid in the given context"
Serenity
I need to write these code lines inside of repeater's ItemDataBound method ..right ?
Serenity
If you add the runat="server" part to your div, then the line of quote you already had (rpt1.Findcontrol....) in the code behind should now be able to access the properties of that div. The reason it didn't work before is that for "FindControl" to work, the item you are trying to find must be a web control.
BradB
+1  A: 

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)   
  {
    ((HtmlGenericControl)(e.Item.FindControl("myDiv"))).
       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>
</asp:Repeater>

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

private int index = 1;

protected string GetDivClass()
{
  return "div" + index++;
}
PHeiberg
I did declare i :|
Serenity
I forgot to cast the return value. In order to access the Attributes collection you need to cast the control to HtmlGenericControl.
PHeiberg
Updated my answer with correct cast and added an alternative way that I think is cleaner for simple data binding of attributes
PHeiberg