2015-02-11 71 views
0

我需要從Eval函數接收數據進行比較,所以我試過這個,但是無法從Eval或Bind接收任何東西。如何獲取評估值在ItemTemplate中的字符串

<ItemTemplate> 
    <% 
     string auc_id = ((string)Eval("Item_BelongToAuction")); 
     Guid id = Guid.Empty; 
     Guid.TryParse(auc_id, out id); 
     try 
     { 
      using (ModeloEntities modelo = new ModeloEntities()) 
      { 
       var auction_name = (from auctions in modelo.Auctions 
            where auctions.Auction_ID == id 
            select auctions).First(); 
       Response.Write(auction_name.Auction_Name); 
      } 
     } 
     catch { } 
     %> 
</ItemTemplate> 

回答

1

背後將你的邏輯代碼,像這樣:

protected string GetAuctionName(string auctionId) 
{ 
    Guid id = Guid.Empty; 
    Guid.TryParse(auctionId, out id); 

    using (ModeloEntities modelo = new ModeloEntities()) 
    { 
     var auction_name = (from auctions in modelo.Auctions 
           where auctions.Auction_ID == id 
           select auctions).First(); 
     return auction_name.Auction_Name; 
    } 
} 

<ItemTemplate> 
    <%# GetAuctionName((string)Eval("Item_BelongToAuction") %> 
</ItemTemplate> 

注意Eval在裏面<%# %>,不<% %>使用。