LinqDataSource控件的Select属性进行数据字段的组合

来源:转载

今天下午有人在论坛咨询 LinqDataSource 控件如何进行组合字段数据显示在下列列表中,其实,很简单,就是使用new 重新生成一个临时类即可。下面是完整的代码。

C# 代码
using System;
using System.Collections.Generic;
using System.Web;

/// <summary>
///Books 的摘要说明
/// </summary>
public class Books
{
  public int Id { get; set; }
  public String Author { get; set; }
  public String Title { get; set; }
  public DateTime PunDate { get; set; }

  public Books() { }

  public List<Books> GetBooks
  {
    get
    {
      return this.GetInternalBooks();
    }
  }

  internal List<Books> GetInternalBooks()
  {
    List<Books> bs = new List<Books>();
    bs.Add(new Books { Id = 1, Author = "孟宪会", Title = "《ASP.NET 2.0 应用开发技术》", PunDate = System.DateTime.Now.AddMonths(-10) });
    bs.Add(new Books { Id = 2, Author = "孟宪会", Title = "《Eric Meyer谈CSS(卷2)》", PunDate = System.DateTime.Now });
    bs.Add(new Books { Id = 3, Author = "孟宪会", Title = "《Eric Meyer谈CSS(卷1)》", PunDate = System.DateTime.Now.AddMonths(+10) });
    return bs;
  }
}

ASPX 代码
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
  <title></title>
</head>
<body>
  <form id="form1" runat="server">
  <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="LinqDataSource1"
    DataTextField="DataText" DataValueField="Id">
  </asp:DropDownList>
  <asp:LinqDataSource ID="LinqDataSource1" runat="server" ContextTypeName="Books" TableName="GetBooks"
    Select='new(Id,Id.toString() + "--" + Author + "--" + Title + "--" + PunDate.ToString("yyyy-MM-dd") as DataText)'>
  </asp:LinqDataSource>
  </form>
</body>
</html>

最后的执行结果将是下面的样子:

[1] [2] 下一页

分享给朋友:
您可能感兴趣的文章:
随机阅读: