2010-05-13 45 views
1

我有了代碼隱藏被定義爲方法的ASP.NET頁面如下:asp.net + C# - 在一箇中繼器綁定格式數據

public string GetStatusColor(int statusID) 
{ 
    string color = "White"; 
    if (statusID == 3) 
    color = "Red"; 
    else if (statusID == 2) 
    color = "Blue"; 
    return color; 
} 

以我ASP.NET頁面,我具有如下定義一箇中繼器:

<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> 

本質上,當TicketStatusID結合,我想調用回GetStatusColor並顯示方法,而不是實際的ID的結果。我如何在中繼器中執行此操作?謝謝!

回答

2

頁:

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

代碼隱藏:

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; 
} 
1

只需

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

如果TicketStatusID心不是INT已經,你需要轉換或解析它