Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Formatting the output of certain items bound to Repeater

For example in the backend I'm binding a datable to a repeater and in the front end I'm setting up my repeater as such:

<asp:Repeater ID="Repeater1" runat="server" onitemdatabound="Repeater1_ItemDataBound">
   <ItemTemplate>
     <div class="user">
         Name:   <%# DataBinder.Eval(Container, "DataItem.Name")%>
         Email:  <%# DataBinder.Eval(Container, "DataItem.Email")%>
         Active: <%# DataBinder.Eval(Container, "DataItem.Active")%>
         Status: <%# DataBinder.Eval(Container, "DataItem.Status")%>
     </div>
    </ItemTemplate>
</asp:Repeater>

So the output for "name" and "email" are fine. However "Active" and "Status" print out an integer code that I would like to change to a more descriptive string based on a reference table I have.

I'm guessing I can do this on the "ItemDataBound" event of the repeater, but I'm stuck on what my next step should be, namely checking the two fields that I need to modify and change them.

protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        //Do modifications here
    }
}
like image 521
PercivalMcGullicuddy Avatar asked Dec 09 '25 19:12

PercivalMcGullicuddy


1 Answers

You can either

  1. Handle the formatting in the ItemDataBound event
  2. Create public methods in your Page or WebUserControl class to handle the formatting.

Using option 1 will require you to declare a control such as a label to store the value for each field like so:

<asp:Repeater ID="Repeater1" runat="server" onitemdatabound="Repeater1_ItemDataBound">
   <ItemTemplate>
     <div class="user">
             <asp:Label ID="ActiveLabel" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Name")%>'></asp:Label>
     </div>
    </ItemTemplate>
</asp:Repeater>

Then in your ItemDataBound event you can find the control and set its value as required.

protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{

    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
            Label activeLabel = (Label)e.Item.FindControl("ActiveLabel");

            //Format label text as required
    }
}

Using option 2 will require you to create a server side publicly accessible method which you can call like so:

<asp:Repeater ID="Repeater1" runat="server" onitemdatabound="Repeater1_ItemDataBound">
   <ItemTemplate>
     <div class="user">
     Active: <%# FormatActive((string)DataBinder.Eval(Container, "DataItem.Active")) %>
     </div>
    </ItemTemplate>
</asp:Repeater>

Then define a method like so:

public string FormatActive(string input)
{
     //Format as required
     //Return formatted string
}
like image 119
jdavies Avatar answered Dec 12 '25 10:12

jdavies



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!