溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

LinqDataSource控件怎么用

發(fā)布時(shí)間:2021-12-01 16:44:00 來源:億速云 閱讀:160 作者:小新 欄目:編程語(yǔ)言

這篇文章主要為大家展示了“LinqDataSource控件怎么用”,內(nèi)容簡(jiǎn)而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“LinqDataSource控件怎么用”這篇文章吧。

有人在論壇咨詢LinqDataSource控件如何進(jìn)行組合字段數(shù)據(jù)顯示在下列列表中,其實(shí),很簡(jiǎn)單,就是使用new 重新生成一個(gè)臨時(shí)類即可。下面是完整的代碼。

C# 代碼

  1. using System;  

  2. using System.Collections.Generic;  

  3. using System.Web;  

  4.  

  5. /// <summary> 

  6. ///Books 的摘要說明  

  7. /// </summary> 

  8. public class Books  

  9. {  

  10. public int Id { get; set; }  

  11. public String Author { get; set; }  

  12. public String Title { get; set; }  

  13. public DateTime PunDate { get; set; }  

  14.  

  15. public Books() { }  

  16.  

  17. public List<Books> GetBooks  

  18. {  

  19. get  

  20. {  

  21. return this.GetInternalBooks();  

  22. }  

  23. }  

  24.  

  25. internal List<Books> GetInternalBooks()  

  26. {  

  27. List<Books> bs = new List<Books>();  

  28. bs.Add(new Books { Id = 1Author = "孟憲會(huì)"Title = "《ASP.NET 2.0 應(yīng)用開發(fā)技術(shù)》"
    PunDate = System.DateTime.Now.AddMonths(-10) });  

  29. bs.Add(new Books { Id = 2Author = "孟憲會(huì)"Title = "《Eric Meyer談CSS(卷2)》"
    PunDate = System.DateTime.Now });  

  30. bs.Add(new Books { Id = 3Author = "孟憲會(huì)"Title = "《Eric Meyer談CSS(卷1)》"
    PunDate = System.DateTime.Now.AddMonths(+10) });  

  31. return bs;  

  32. }  

ASPX 代碼

  1. <%@ Page Language="C#" %> 

  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

  3. <html xmlns="http://www.w3.org/1999/xhtml"> 

  4. <head runat="server"> 

  5. <title></title> 

  6. </head> 

  7. <body> 

  8. <form id="form1" runat="server"> 

  9. <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="LinqDataSource1" 

  10. DataTextField="DataText" DataValueField="Id"> 

  11. </asp:DropDownList> 

  12. <asp:LinqDataSource ID="LinqDataSource1" runat="server" 
    ContextTypeName="Books" TableName="GetBooks" 

  13. Select='new(Id,Id.toString() + "--" + Author + "--" + 
    Title + "--" + PunDate.ToString("yyyy-MM-dd") as DataText)'
    > 

  14. </asp:LinqDataSource> 

  15. </form> 

  16. </body> 

  17. </html> 

***的執(zhí)行結(jié)果將是下面的樣子:

HTML 代碼

<select name="DropDownList1" id="DropDownList1"> <option value="1">1--孟憲會(huì)--《ASP.NET 2.0 應(yīng)用開發(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控件怎么用”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI