Pregunta

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
    }
}
¿Fue útil?

Solución

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
}

Otros consejos

I prefer creating format methods called in the markup rather than handling ItemDataBound.

<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: <%# FormatActive((int)DataBinder.Eval(Container, "DataItem.Active"))%>
         Status: <%# FormatStatus((int)DataBinder.Eval(Container, "DataItem.Status"))%>
     </div>
    </ItemTemplate>
</asp:Repeater>

Then in your code behind:

protected static FormatActive(int active)
{
    return "Formated Active String...";
}

protected static FormatStatus(int status)
{
    return "Formated StatusString...";
}
<asp:Repeater ID="Repeater1" runat="server" onitemdatabound="Repeater1_ItemDataBound">
   <ItemTemplate>
     <div class="user">
         Active:   <asp:label id="lblActive" Text='<%# DataBinder.Eval(Container, "DataItem.Active")%>' runat="server" />        
     </div>
    </ItemTemplate>
</asp:Repeater>


protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        //Do modifications here
        YourObjectName person = (YourObjectName)e.Item.DataItem;
        //you can now ref the object this row is bound to
        //example find a dom element
        Label lblActive= (Label)e.Item.FindControl("lblActive");

        if(person.Active == 2)
        {
            lblActive.Text = "This is great";
        }

    }
}

You could do something like:

<%# (int)DataBinder.Eval(Container, "DataItem.Active") == 0 ? "Active" : "Inactive" %>

no need to use the itemdatabound. Just add a method in your itemtemplate to do the conversion with the dataitem.active as parameter. Add a label and do the following:

Text='<%# String.Format("{0}",Conversion(Eval("dataitem.active")))%>'

Conversion is then a method you leave in your code behind or utility class where you do the conversion.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top