Search code examples
asp.netvb.netarraylistdata-bindingrepeater

Display Special Character in Repeater Based on the DataItem Value


I'm calling a stored procedure by passing the input from the webpage and saving the results in ArrayList. Each record in the ArrayList has column with name: type. If type is "A", then I need display a special character on web page for the column value for the associated row.For the remaining type records in the results ArrayList, i don't need to display anything for the associated rows..

in code behind file:

Dim array As New ArrayList

            array = outPutFromTheSporedProcedure
              repeater1.DataSource = array
            repeater1.DataBind()

On aspx.page:

<asp:Repeater ID="repeater1" runat="server">
                            <HeaderTemplate>
                                            <tr>
                                                <th> type  </th>
                                            </tr>
                            </HeaderTemplate>
                            <ItemTemplate>
                                <tr>
                                       <td>
                                        <asp:Label ID="lbl123" runat="server"><%#DataBinder.Eval(Container.DataItem, "type")%></asp:Label></td>
                                </tr>
                            </ItemTemplate>
                            <FooterTemplate>
                                </table></td></tr>
                            </FooterTemplate>
                        </asp:Repeater>

On Aspx Page I have a repeater and binding a data item to repeater in itemTemplate.
Now i need to display special character(i.e *, $, &, basically to differentiate from other type of rows) for type of rows "A", for remaining rows, i need to display nothing for the records for the associated column.

Please let me know how to solve this one. Thanks in Advance!


Solution

  • Since you want to display nothing for the non-special conditions you must first change this:

    <asp:Label ID="lbl123" runat="server"><%#DataBinder.Eval(Container.DataItem, "type")%></asp:Label></td>
    

    To This:

    <asp:Label ID="lbl123" runat="server"></asp:Label></td>
    

    Now, set your special character in the Repeater_ItemDataBound Event Handler if the conditions are met:

    Private Sub repeater1_ItemDataBound(sender As Object, e As RepeaterItemEventArgs) Handles repeater1.ItemDataBound
    
        If e.Item.ItemType = ListItemType.AlternatingItem OrElse e.Item.ItemType = ListItemType.Item Then
    
            If CType(e.Item.DataItem, UnderlyingTypeOfArrayList).type = "A" Then
    
                CType(e.Item.FindControl("lbl123"), Label).Text = "*" 'Special Character
    
            End If
    
        End If
    
    End Sub
    

    You see above that I check for the correct RepeaterItemType, if tghe DataItem's type Property = A, and if so, I get an instance of the label using FindControl and Casting to set its Text to the special character.

    Note: You'll have to change UnderlyingTypeOfArrayList to the Type contained in the ArrayList.