Link to home
Start Free TrialLog in
Avatar of Andy Green
Andy GreenFlag for United Kingdom of Great Britain and Northern Ireland

asked on

ASP Repeater control - onl;y show if flag is true

Hi

I have an asp repeater control, and want to programmatically hide data rows based on a flag. I cant do this on the sql server without a lot more work. Is it possible to conditional show a row base on the flag in the data source.

I have tried adding an if statement in the itemdatabound event but it always show the record.

Andy
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

I have tried adding an if statement in the itemdatabound event but it always show the record.
can you post your codes here?
Avatar of Andy Green

ASKER

    Protected Sub repEvents_ItemDataBound(sender As Object, e As RepeaterItemEventArgs) Handles repEvents.ItemDataBound

        Dim drv As DataRowView = TryCast(e.Item.DataItem, DataRowView)

        If drv IsNot Nothing Then

            If Not drv("Isprivate") Then
                Dim StartTime As Label = TryCast(e.Item.FindControl("lblStartTime"), Label)
                Dim EndTime As Label = TryCast(e.Item.FindControl("lblEndTime"), Label)
                Dim Separator As Literal = TryCast(e.Item.FindControl("ltlSeparator"), Literal)

                Dim sd As String = drv("StartDate").ToString
                Dim ed As String = drv("EndDate").ToString
                Dim st As String = drv("StartTime").ToString
                Dim et As String = drv("EndTime").ToString

                If st = "00:00" Then
                    StartTime.Text = String.Empty
                End If

                If et = "00:00" Then
                    EndTime.Text = String.Empty
                End If

                If ed.ToString = "" Then
                    Separator.Visible = False
                End If

                If sd = ed Then
                    EndTime.Visible = False
                    Separator.Visible = False
                End If
            End If
        Else
            Return
        End If
    End Sub

Open in new window

from your codes, it seems that you're doing nothing to "hide" a row.

you may add:

drv.Delete();

Open in new window

in your condition to "hide" the row if it met the criterion.

and you can also refer to this PAQ:

Hide Gridview row if a column value is blank.
https://www.experts-exchange.com/questions/26991722/Hide-Gridview-row-if-a-column-value-is-blank.html

see if these helps?
Good point, I was relying on the return, but that too was in the wrong place.

This is my re done code, and I can hit a breakpoint and see the condition changing, but it still shows all rows.

Protected Sub repEvents_ItemDataBound(sender As Object, e As RepeaterItemEventArgs) Handles repEvents.ItemDataBound

        Dim drv As DataRowView = TryCast(e.Item.DataItem, DataRowView)

        If drv IsNot Nothing Then

            If drv("Isprivate") = "0" Then
                Dim StartTime As Label = TryCast(e.Item.FindControl("lblStartTime"), Label)
                Dim EndTime As Label = TryCast(e.Item.FindControl("lblEndTime"), Label)
                Dim Separator As Literal = TryCast(e.Item.FindControl("ltlSeparator"), Literal)

                Dim sd As String = drv("StartDate").ToString
                Dim ed As String = drv("EndDate").ToString
                Dim st As String = drv("StartTime").ToString
                Dim et As String = drv("EndTime").ToString

                If st = "00:00" Then
                    StartTime.Text = String.Empty
                End If

                If et = "00:00" Then
                    EndTime.Text = String.Empty
                End If

                If ed.ToString = "" Then
                    Separator.Visible = False
                End If

                If sd = ed Then
                    EndTime.Visible = False
                    Separator.Visible = False
                End If
            Else
                drv.Delete()
            End If

        End If
    End Sub

Open in new window

I'm using a repeater control, not a grid.

Andy
ASKER CERTIFIED SOLUTION
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Well thought out, works a treat. Thank you