开发者

asp.net + C# - Format data bound in a repeater

开发者 https://www.devze.com 2022-12-30 09:04 出处:网络
I have an ASP.NET page that has a code-behind method that is defined as follows: public string GetStatusColor(int statusID)

I have an ASP.NET page that has a code-behind method that is defined as follows:

public string GetStatusColor(int statusID)
{
  stri开发者_JAVA技巧ng color = "White";
  if (statusID == 3)
    color = "Red";
  else if (statusID == 2)
    color = "Blue";
  return color;
}

In my ASP.NET page, I have a Repeater that is defined as follows:

<asp:Repeater ID="ticketRepeater" runat="server" OnLoad="ticketRepeater_Load">
  <HeaderTemplate>
    <table id="resultTable" cellpadding="0" cellspacing="0">    
      <tr>
        <th>Ticket #</th>
        <th>Status</th>
      </tr>
  </HeaderTemplate>
  <ItemTemplate>
    <tr>
       <td><%# DataBinder.Eval(Container.DataItem, "TicketID") %></td>
       <td><%# DataBinder.Eval(Container.DataItem, "TicketStatusID") %></td>
    </tr>
  </ItemTemplate>
  <FooterTemplate>
    </table>
  </FooterTemplate>
</asp:Repeater>

Essentially, when the TicketStatusID is bound, I want to call back to GetStatusColor and display the result of the method instead of the actual ID. How do I do this in a Repeater? Thank you!


page:

<td><%# GetStatusColor(Container.DataItem)%></td> 

codebehind:

public string GetStatusColor(object dataItem) 
{ 
  string color = "White";
  var ticket = dataItem as YourTicketClass;
  if(ticket != null)
  {
      if (ticket.TicketStatusID == 3) 
        color = "Red"; 
      else if (ticket.TicketStatusID == 2) 
        color = "Blue"; 
  }
  return color; 
} 


Simply

<td><%# GetStatusColor(DataBinder.Eval(Container.DataItem, "TicketStatusID")) %></td>

If TicketStatusID isnt int already, you need to cast or parse it

0

精彩评论

暂无评论...
验证码 换一张
取 消