Changing Repeater ItemTemplate details based on data from dataset

M

Mike

Hello all,

Is there a way to change the ItemTemplate based on the data that is
being shown?

For instance, I have a dataset, contains two fields (NAME, STATE). I
want to show all the records from the dataset using a standard item
template, but when STATE="TX", I want that row to show up in a certain
color.

Is there a way to accomplish this, any event that triggers as the
repeater is generating each item of the list?

Thanks in advance,
Mike
 
A

Alec MacLean

An approach I've taken to achieve this type of formatting with a datagrid
was to:
1. Bind data source to datagrid
2. Examine content of datagrid in a loop and apply style classes from the
css stylesheet.

I don't think it's possible to do both at the same time as you desire, but
both of the above actions will occur before the user gets to see the
results, so from a user experience perspective it would sem as if they are
occurring simultaneously.

Note that the custom styling required that I first remove any style applied
using the datagrid designer, because these add background colour statements
instead of being stylesheet class based and as such override style class
statements.

Having done that, you can then go to the code behind. After the method for
puling your data occurs and the list is databound, call the second method to
apply the custom highlighting, something like the following:

'#########
private subPage_Load()
'...
If not me.page.IsPostback then
'...
me.LoadDataGrid()
me.SetHighlight()
'...
end if

end sub

private sub LoadGridData()
'Connect to db, get data values.
'...
me.dgOuter.datasource = mySourceDataset
me.dgOuter.databind
'...
end sub

Private Sub SetHighlight()
Dim i As Integer
For i = 0 To Me.dgOuter.Items.Count - 1
If Me.dgOuter.Items.Item(i).Cells(1).Text = "TX" Then
Me.dgOuter.Items.Item(i).CssClass = "myTexasHighlightStyle"
End If
Next
End Sub

'#########

I've always used the datagrid by preference. However, there are some
features of the datalist that should support a similar approach to the
above, perhaps something like this:

Private Sub SetDataListHighlight()
Dim state As Label
For i = 0 To Me.DataList1.Items.Count - 1
state = Me.DataList1.FindControl("lblState")
If state.Text = "TX" Then
'Need to set the row style
Me.DataList1.Items.Item(i).CssClass = "myTexasHighlightStyle"
End If
Next
End Sub


Hope that helps.

Al
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top