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

本文通过实例演示了如何使用LinqDataSource控件实现组合字段数据显示。通过定义临时类并使用Select属性,可以在下拉列表中展示书籍的综合信息。

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

C# 代码
<!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --&gtusing System;
using System.Collections.Generic;
using System.Web;

///
///Books 的摘要说明
///
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 代码
<!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --&gt@ 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 代码
<!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --&gt<select name="DropDownList1" id="DropDownList1">
    
<option value="1">1--孟宪会--《ASP.NET 2.0 应用开发技术》--2008-07-31option>
    
<option value="2">2--孟宪会--《Eric Meyer谈CSS(卷2)》--2009-05-31option>
    
<option value="3">3--孟宪会--《Eric Meyer谈CSS(卷1)》--2010-03-31option>
select>
在此写出此文,目的在于示例一下 LinqDataSource 控件的 Select 属性的写法。

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/15723462/viewspace-604563/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/15723462/viewspace-604563/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值