如何将 Eval 值获取到 ItemTemplate 中的字符串

how to get Eval value to string in ItemTemplate

我需要从 Eval 函数接收数据来进行比较,所以我试过了但是无法从 Eval 或 Bind 接收到任何数据。

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

将你的逻辑移到后面的代码中,像这样:

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用在<%# %>里面,不是[​​=13=]。