本文向大家介绍LinqDataSource控件,可能好多人还不了解LinqDataSource控件,没有关系,看完本文你肯定有不少收获,希望本文能教会你更多东西。
今天下午有人在论坛咨询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>
***的执行结果将是下面的样子:
HTML 代码
- <select name="DropDownList1" id="DropDownList1">
- <option value="1">1--孟宪会--《ASP.NET 2.0 应用开发技术》--2008-07-31</option>
- <option value="2">2--孟宪会--《Eric Meyer谈CSS(卷2)》--2009-05-31</option>
- <option value="3">3--孟宪会--《Eric Meyer谈CSS(卷1)》--2010-03-31</option>
- </select>
本文目的在于示例一下 LinqDataSource控件的 Select 属性的写法。
【编辑推荐】