Changing Repeater ItemTemplate details based on data from dataset

Discussion in 'ASP .Net' started by Mike, Feb 3, 2006.

  1. Mike

    Mike Guest

    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
     
    Mike, Feb 3, 2006
    #1
    1. Advertising

  2. Mike

    Alec MacLean Guest

    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



    "Mike" <> wrote in message
    news:...
    > 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
    >
     
    Alec MacLean, Feb 4, 2006
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bennett Haselton
    Replies:
    1
    Views:
    5,384
    Joe Fallon
    Sep 24, 2004
  2. Harry Simpson

    Dynamic DataList ItemTemplate based on data

    Harry Simpson, Nov 18, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    4,073
    Harry Simpson
    Nov 19, 2004
  3. Shimon Sim

    Repeater.ItemTemplate =?

    Shimon Sim, Jan 27, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    971
    Karl Seguin
    Jan 27, 2005
  4. =?Utf-8?B?Sm9l?=

    Show Details/Hide Details link button

    =?Utf-8?B?Sm9l?=, Mar 13, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    938
    dkode
    Mar 13, 2006
  5. Replies:
    2
    Views:
    861
Loading...

Share This Page