LinqDataSource控件剖析
本文向大家介紹LinqDataSource控件,可能好多人還不了解LinqDataSource控件,沒(méi)有關(guān)系,看完本文你肯定有不少收獲,希望本文能教會(huì)你更多東西。
今天下午有人在論壇咨詢(xún)LinqDataSource控件如何進(jìn)行組合字段數(shù)據(jù)顯示在下列列表中,其實(shí),很簡(jiǎn)單,就是使用new 重新生成一個(gè)臨時(shí)類(lèi)即可。下面是完整的代碼。
C# 代碼
- using System;
- using System.Collections.Generic;
- using System.Web;
- /// <summary>
- ///Books 的摘要說(shuō)明
- /// </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 = "孟憲會(huì)", Title = "《ASP.NET 2.0 應(yīng)用開(kāi)發(fā)技術(shù)》",
PunDate = System.DateTime.Now.AddMonths(-10) });- bs.Add(new Books { Id = 2, Author = "孟憲會(huì)", Title = "《Eric Meyer談CSS(卷2)》",
PunDate = System.DateTime.Now });- bs.Add(new Books { Id = 3, Author = "孟憲會(huì)", 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>
***的執(zhí)行結(jié)果將是下面的樣子:
HTML 代碼
- <select name="DropDownList1" id="DropDownList1">
- <option value="1">1--孟憲會(huì)--《ASP.NET 2.0 應(yīng)用開(kāi)發(fā)技術(shù)》--2008-07-31</option>
- <option value="2">2--孟憲會(huì)--《Eric Meyer談CSS(卷2)》--2009-05-31</option>
- <option value="3">3--孟憲會(huì)--《Eric Meyer談CSS(卷1)》--2010-03-31</option>
- </select>
本文目的在于示例一下 LinqDataSource控件的 Select 屬性的寫(xiě)法。
【編輯推薦】