473,385 Members | 1,769 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Alternating CssClass without repeating code in AlternatingItemTemplate?

Hi all,

Is there a way in something like a DataList, to have a different colour
background (via CssClass) without repeating all my code in both the
ItemTemplate and AlternatingItemTemplate blocks?

I have quite a lot of stuff in the ItemTemplate block, but because so much
of it is bound fields, creating a seperate user control mapping all of these
fields across would be a nightmare to maintain. Instead, I'd like something
like:
<ItemTemplate>
<div class="<% SomeExpress that returns True/False or Odd/Even etc. %>">
// all my stuff here
</div>
</ItemTemplate>

If there was a sort of "row number" I could get at, I could do "(row) Mod 2"
to do what I want, but I can't see anything obvious to do that.

Any ideas?

Thanks,

Danny Tuppeny
Nov 19 '05 #1
3 2143
"Danny Tuppeny" <gr****@dannytuppeny.commmmmm> wrote in message
news:43***********************@ptn-nntp-reader04.plus.net...
If there was a sort of "row number" I could get at, I could do "(row) Mod
2" to do what I want, but I can't see anything obvious to do that.


It's going to be one of those days... The solution, is this:

<%# Container.ItemIndex %>

I tried it before posting and it didn't work, but I stupid had put it in the
wrong place. I have a FormView at the top of my page displaying the
"current" record. Then a DataList that displays "children". I'd put it in
the ItemTemplate of the first one, which of course, only displays one record
anyway. Doh!
Nov 19 '05 #2
: Is there a way in something like a DataList, to have a different colour
: background (via CssClass) without repeating all my code in both the
: ItemTemplate and AlternatingItemTemplate blocks?

Not directly.

One ugly way may be to use a variable with the "common" code. So,
finally, the templates may look like:

<ItemTemplate>
<%= common %> CssClass=... <%= remaining %>
</ItemTemplate>
<AlternatingItemTemplate>
<%= common %> CssClass=... <%= remaining %>
</AlternatingItemTemplate>

Or better still, you may like to make a CustomDataList as a sub-class of
DataList... tougher way but will give you exactly what you want with finer
control.

--
Cheers,
Gaurav Vaish
http://mastergaurav.org
------------------------
Nov 19 '05 #3
"Gaurav Vaish" <g_***********************************@gmail.com > wrote in
message news:Ot**************@TK2MSFTNGP12.phx.gbl...
: Is there a way in something like a DataList, to have a different colour
: background (via CssClass) without repeating all my code in both the
: ItemTemplate and AlternatingItemTemplate blocks?

Not directly.

One ugly way may be to use a variable with the "common" code. So,
finally, the templates may look like:


<snip>

I managed it with the ItemIndex. Doing ItemIndex % 2 gives me an
alternativing variable I can use in class names :)
Nov 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Lynn | last post by:
Hello, Sorry for the double post...I meant for this to go into this forum. I have built a datalist programatically using an article from MSDN...the article was written specifically for the...
1
by: Eirik Eldorsen | last post by:
I'm trying to set alternating bgcolor for a datalist with 2 columns. My problem is that its the alternating cell that get the bgcolor, not the row. Is it possible to set alternating color of rows?...
0
by: MattC | last post by:
Hi, I have place the following code in my ItemDataBound event: The problem I have is that when I select a row for editing using the edit command it no longer changes the CSSClass to the one...
9
by: Max Weebler | last post by:
Hi, I have a datagrid built that has an alternating item style that sets the backcolor and ForeColor of its rows. I have 4 template columns. One of them has a LinkButton embedded in it to...
1
by: rockdale | last post by:
Hi, All I have an datagrid with alternating color, I also want the back-color of my textbox in ItemTemplate also appears alternating, how could I do that? <asp:datagrid id="dgridEmpHours"...
1
by: mavrick_101 | last post by:
Hi, I am running into this problem. I have a repeater and trying to use 'AlternatingItemTemplate'. If I use 'AlternatingItemTemplate' along with 'ItemTemplate' only two out of several row are...
3
by: =?Utf-8?B?V2ViQnVpbGRlcjQ1MQ==?= | last post by:
on the rowcreated or rowdatabound i was looking for the alternateing row like existed in the datagrid. I need to set a style depending on which row is current" alternating or row. Anyone know...
4
by: mike | last post by:
how can I change the font color for an alternating row where the column data is formatted as a link? setting a style in the stylesheet for a { color:white; }
1
by: Sachin | last post by:
Hi All, I am using ASP.NET Repeater Web Control. I want to use different stylesheets for alternating rows similar to DataGrid contorl. How can I do this? Thanks In Advance Sachin
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.