如何在ItemTemplate中将Eval值转换为字符串

时间:2015-02-11 16:02:55

标签: c# asp.net data-binding

我需要从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>

1 个答案:

答案 0 :(得分: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内部使用的是<%# %>,而不是<% %>