LinqDataSource控件怎么用
这篇文章主要为大家展示了"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#" %>
"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 代码
以上是"LinqDataSource控件怎么用"这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注行业资讯频道!