Controlling what is regarded as Item/AlternatingItem

  • Thread starter Thread starter K Viltersten
  • Start date Start date
K

K Viltersten

I'm reworking a repeater that previously used

if (e.Item.ItemType == ListItemType.Item)
css += "plain";
else
css += "alter";

Some of the rows will now be hidden, which
creates the ugly appearance of several
consecutive and visible rows to share the
same css (e.g. alter).

I need to manipulate the repeater to
disregard certain rows upon binding the
data. So to speak - skip a line.

One way to do this would probably be a
static flag, the status of which would
change with every non-hidden row but that's
a) ugly,
b) non object oriented,
c) blah-ish.

Any suggestions?
 
One thing I would look into is using the AlternatingItemTemplate. This uses
the same DataSource as the ItemTemplate, but it allows you to specify a
different template. Also, what are the conditions that determine which rows
will be displayed and hidded? You may want to look into the ItemDataBound
event for this, and set the desired CSS from inside the ItemDataBound event
after determining whether you want to display or hide the row. Hopefully
this helps.
 
One thing I would look into is using the
AlternatingItemTemplate. This uses the
same DataSource as the ItemTemplate, but
it allows you to specify a different
template.

Well, that's what i'm using. Perhaps i should
clarify the code a bit more.

ListItemType t = e.Item.ItemType;
if (t == ListItemType.AlternatingItem)
css += "alter";
else if (t == ListItemType.Item)
css += "plain";
You may want to look into the ItemDataBound
event for this, and set the desired CSS
from inside the ItemDataBound event...

The setup above IS ALREADY in the event
handler for data binding. Where else could
it be placed, anyway?

So, the question is, as i expressed it in my
original question, how one can manipulate
the list item type of a given item, depending
on the outcome of their predecessors and
successors.

Thank you.
 
First of all, since your code will ALWAYS give your ItemTemplate the css
class "plain" and AlternatingItemTemplate the css class "alter", why are you
doing it in the codebehind at all? I would have done something like the
following:

<asp:Repeater ID="repSample" runat="server">
<ItemTemplate>
<asp:Label ID="lblItemContent" runat="server" Text="ItemTemplate Label"
CssClass="plain"/>
</ItemTemplate>
<AlternatingItemTemplate>
<asp:Label ID="lblAlternatingItemContent" runat="server"
Text="AlternatingItemTemplate Label" CssClass="alter"/>
</AlternatingItemTemplate>
</asp:Repeater>

I just used Labels as example content, but the point is that the styles are
different in the two templates without the need for anything in the
codebehind.

As for your question about manipulating an item based on other items, I
would suggest doing something that would involve the following:

In the PreRender event, use the Items property of the Repeater to view and
modify any items you wish to modify.

Another approach that you could use would be to, in the ItemDataBound use
the e.Item.ItemIndex property to get any necessary data from the Repeater's
DataSource. To do this, you will use when getting the data from the
Repeater's DataSource, you will do stuff like use e.Item.ItemIndex-1 as the
index to get data about the previous item or e.Item.ItemIndex+1 as the index
to get the next item.

Hopefully this helps. Because I have not seen your code and do not know what
the goal of your project is, I cannot say whether or not there is another or
better way, but when using databinding it is usually best to try to avoid
basing the formatting of one item on another item. Good Luck!
 
As i mentioned in the original post, some of
the rows will be hidden (regardless of them
being styled to plain/alter.

So, by only adding the style the way you
described, i might get to SEE only plain
rows (if the alternating happen to be hidden
because of the new conditions).

Index will not work either, for the same
reason.

I went for boolean flag that changes the
state for each data bind event fired, except
for those that are hidden. I'm not very
satisfied with that solution, though.

Any suggestion to that?

Thanks.

--
Regards
K Viltersten
----------------------------------------
May all spammers die an agonizing death;
have no burial places; their souls be
chased by demons in Gehenna from one room
to another for all eternity and beyond.
 
This might be way off, but could you not create a DataSet that's generated
from your data + a Hidden flag, and then DataBind from that, so the only
things that get sent to the Repeater are the actual things you want to show?

I'm not sure of the code to do it off-hand, but that's how I'd approach it.
 
What CSS are you adding to the items to specify whether they are hidden or
not? Regardless of what other styling there is, if you set the CSS display
property to none, then it should be hidden. But here is a question that I
just want to make sure I have a clear answer on right now: Is your problem
in finding/accessing the items you want to modify, or is the problem in
figuring out how to style them to get a final product that looks the way you
want? I think it might be useful to everyone here in the newsgroup (at least
I would find it useful) if you showed us a little more code, as well as a
sample of what you want the final output to look like. Good Luck!
 
Back
Top