自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

詳解ASP.NET的Multi-ListBox控件

開(kāi)發(fā) 后端
本文向您介紹ASP.NET Multi-ListBox控件編程知識(shí):主要講述頁(yè)面的生命周期的理解以及Multi-ListBox控件的兩個(gè)主要方法。

開(kāi)發(fā)一個(gè)優(yōu)秀的數(shù)據(jù)綁定不是一件很容易的事情。剛開(kāi)始的時(shí)候走了一些彎路,一直緊緊咬著 DataBoundControl類不放。最終失望之后冷靜下來(lái)想到關(guān)于DataSource不就是一個(gè)數(shù)據(jù)集合嗎?明白之后,有關(guān)數(shù)據(jù)源的問(wèn)題基本上也解決了。在整個(gè)ASP.NET Multi-ListBox控件控件開(kāi)發(fā)中,我認(rèn)為最重要的實(shí)際上就是頁(yè)面的生命周期的理解,如果您基本上理解了它的話,那么,基本上,你以后開(kāi)發(fā)一款A(yù)SP.NET控件也不是一件很難的事情。我們還是簡(jiǎn)單了解開(kāi)發(fā)的思路吧。

在ASP.NET Multi-ListBox控件的生命周期中,我們主要需要解決用戶回發(fā)頁(yè)面的時(shí)候保留ListBox的數(shù)據(jù)源(因?yàn)槲覜](méi)有采用復(fù)合控件的方式來(lái)開(kāi)發(fā))。因此,我們需要重寫(xiě)控件的SaveViewState, LoadViewState二個(gè)方法。

  1. ViewStates   
  2.  
  3. 1 protected override void LoadViewState  
  4. (object savedState)   
  5. 2 {   
  6. 3 if (savedState != null)   
  7. 4 {   
  8. 5 Triplet triplet = (Triplet)savedState;   
  9. 6 base.LoadViewState(triplet.First);   
  10. 7 Reflector.InvokeMethod(this.FirstListBox.  
  11. Items, "LoadViewState", new object[]   
  12. { triplet.Second });   
  13. 8 Reflector.InvokeMethod(this.SecondListBox.Items,   
  14. "LoadViewState", new object[] { triplet.Third });   
  15. 9 }   
  16. 10 else   
  17. 11 {   
  18. 12 base.LoadViewState(null);   
  19. 13 }   
  20. 14 this._stateLoaded = true;   
  21. 15 }   
  22. 16   
  23. 17 protected override object SaveViewState()   
  24. 18 {   
  25. 19 if (EnableViewState == false)   
  26. 20 return null;   
  27. 21 //啟用控件視圖狀態(tài)   
  28. 22 object x = base.SaveViewState();   
  29. 23 object y = Reflector.InvokeMethod  
  30. (FirstListBox.Items, "SaveViewState"null);   
  31. 24 object z = Reflector.InvokeMethod  
  32. (SecondListBox.Items, "SaveViewState"null);   
  33. 25 if ((x == null) && (y == null) && (z == null))   
  34. 26 {   
  35. 27 return null;   
  36. 28 }   
  37. 29 return new Triplet(x, y, z);   
  38. 30 }   

為了省事,我沒(méi)有自定義ListItem類,改為直接使用ListItemCollection來(lái)存儲(chǔ)數(shù)據(jù)。因?yàn)镸S沒(méi)有提供ListItemCollection. SaveViewState和LoadViewState,我們必須采用反射的方式來(lái)調(diào)用這二個(gè)方法來(lái)保存數(shù)據(jù)。很讓人郁悶。每當(dāng)?shù)骄o要關(guān)頭,就會(huì)發(fā)現(xiàn)MS寫(xiě)的類,方法不是internal,就是sealed。無(wú)可奈何~當(dāng)然,你也可以自己寫(xiě)一個(gè)類來(lái)代替ListItem類.

我們?cè)陧?yè)面上進(jìn)行ListBox進(jìn)行左移,右移的數(shù)據(jù)全部需要按一定的格式臨時(shí)存儲(chǔ)在HiddenField控件中,這樣我們可以通過(guò)繼承IPostBackDataHandler 接口中的LoadPostData方法獲取我們臨時(shí)存儲(chǔ)的數(shù)據(jù),對(duì)ListBox的數(shù)據(jù)源進(jìn)行添加,移除等操作。

  1. IPostBackDataHandler   
  2.  
  3. public bool LoadPostData  
  4. (string postDataKey, NameVal  
  5. ueCollection postCollection)   
  6. 2 {   
  7. 3 bool resultValueFlag = false;   
  8. 4 //移除指定ListItem,  
  9. 并需要添加了Left ListBox列表框中   
  10. 5 string itemsRemoved =   
  11. postCollection[this.ClientID "_REMOVED"];   
  12. 6 string[] itemsRemovedCol =   
  13. itemsRemoved.Split(',');   
  14. 7 if (itemsRemovedCol != null)   
  15. 8 {   
  16. 9 if (itemsRemovedCol.Length 〉   
  17. 0 && itemsRemovedCol[0] != "")   
  18. 10 {   
  19. 11 for (int i = 0; i 〈   
  20. itemsRemovedCol.Length; i )   
  21. 12 {   
  22. 13 string[] itemsRemoveItems =   
  23. itemsRemovedCol[i].Split('|');   
  24. 14 ListItem item = this.SecondListBox.  
  25. Items.FindByValue(itemsRemoveItems[1]);   
  26. 15 if (item != null)   
  27. 16 {   
  28. 17 this.SecondListBox.Items.Remove(item);   
  29. 18 }   
  30. 19 item = this.FirstListBox.Items.  
  31. FindByValue(itemsRemoveItems[1]);   
  32. 20 if (item == null)   
  33. 21 {   
  34. 22   
  35. 23 this.FirstListBox.Items.Add 
  36. (new ListItem(itemsRemoveItems[0],   
  37. itemsRemoveItems[1]));   
  38. 24 }   
  39. 25 resultValueFlag = true;   
  40. 26 }   
  41. 27 }   
  42. 28 }   
  43. 29 //從客戶端添加指定的ListItem   
  44. 30 string itemsAdded = postCollection  
  45. [this.ClientID "_ADDED"];   
  46. 31 string[] itemsAddedCol = itemsAdded.  
  47. Split(',');   
  48. 32 if (itemsAddedCol != null)   
  49. 33 {   
  50. 34 if (itemsAddedCol.Length 〉   
  51. 0 && itemsAddedCol[0] != "")   
  52. 35 {   
  53. 36 int counter = -1;   
  54. 37 for (int i = 0; i 〈   
  55. itemsAddedCol.Length; i )   
  56. 38 {   
  57. 39 string[] itemsAddItems =   
  58. itemsAddedCol[i].Split('|');   
  59. 40 ListItem item = this.SecondListBox.  
  60. Items.FindByValue(itemsAddItems[1]);   
  61. 41 if (item == null)   
  62. 42 {   
  63. 43 this.SecondListBox.Items.Add(new   
  64. ListItem(itemsAddItems[0],itemsAddItems[1]));   
  65. 44 counter = 1;   
  66. 45 }   
  67. 46 item = this.FirstListBox.Items.  
  68. FindByValue(itemsAddItems[1]); 軟件開(kāi)發(fā)網(wǎng) www.mscto.com   
  69. 47 if (item != null)   
  70. 48 {   
  71. 49 this.FirstListBox.Items.Remove(item);   
  72. 50 }   
  73. 51 }   
  74. 52 resultValueFlag = counter 〉 -1 ? true : false;   
  75. 53 }   
  76. 54 }   
  77. 55   
  78. 56 //從客戶端中移除指定的ListItem   
  79. 57 return resultValueFlag;   
  80. 58 }   
  81. 59   
  82. 60 public void RaisePostDataChangedEvent()   
  83. 61 {   
  84. 62 //TODO::   
  85. 63 }   

一切就是這么簡(jiǎn)單,就是SaveViewaState,LoadViewState,LoadPostData順序。后面二個(gè)是頁(yè)面回發(fā)的時(shí)候才會(huì)觸發(fā)。只要解決這里,***不過(guò)就是呈現(xiàn)控件而已。 #p#

如果在頁(yè)面中使用ASP.NET Multi-ListBox控件?

  1. HTML   
  2.  
  3. 1〈asp:MultiListBox ID="ListBox1" 
  4.  runat="server" Rows="10" Width="250px"   
  5. Height="200px" DataTextField="UserName"   
  6. DataValueField="UserID"   
  7. SelectionMode="Multiple" 〉   
  8. 2 〈FirstListBox 〉  
  9. 〈StyleSheet Width="100px" / 〉  
  10. 〈/FirstListBox 〉   
  11. 3 〈SecondListBox 〉  
  12. 〈StyleSheet Width="100px" / 〉  
  13. 〈/SecondListBox 〉   
  14. 4 〈/asp:MultiListBox 〉   
  15. 5   
  16. Submit   
  17. 1protected void Page_Load  
  18. (object sender, EventArgs e)   
  19. 2 {   
  20. 3 if (Page.IsPostBack)   
  21. return;   
  22. 5 ListBox1.FirstListBox.  
  23. DataSource = LoadData(1, 5);   
  24. 6 ListBox1.SecondListBox.DataSource =   
  25. LoadData(6, 10);   
  26. 7 ListBox1.DataBind();   
  27. 8}   
  28. 9protected void Button1_Click(object   
  29. sender, EventArgs e)   
  30. 10 {   
  31. 11 Response.Write("您SecondList選擇的值為:  
  32. 〈br/ 〉");   
  33. 12 foreach (ListItem item in this.ListBox1.  
  34. SecondListBox.Items)   
  35. 13 {   
  36. 14 Response.Write(item.Text ":" item.Value   
  37. "〈br/ 〉");   
  38. 15 }   
  39. 16 Response.Write("您FirstList選擇的值為:  
  40. 〈br/ 〉");   
  41. 17 foreach (ListItem item in this.ListBox1.  
  42. FirstListBox.Items)   
  43. 18 {   
  44. 19 Response.Write(item.Text ":" item.Value   
  45. "〈br/ 〉");   
  46. 20 }   
  47. 21 }   

就像前面所說(shuō)那樣,目前只完成的基本的功能,像如果頁(yè)面放了多個(gè)控件之后的問(wèn)題,讓開(kāi)發(fā)人員自定義修改Control Panel的圖標(biāo),自定義JS路徑等都還沒(méi)有考慮完全(時(shí)間有限,只有等以后慢慢完善)。如何跟SqlDataSource控件結(jié)合?如何直接可編輯ListBox的Items屬性就能呈現(xiàn)?呵呵。需要挑戰(zhàn)的還有許多地方。

【編輯推薦】

  1. 創(chuàng)建ASP.NET 2.0應(yīng)用程序
  2. ASP.NET數(shù)據(jù)緩存四大方案
  3. ASP.NET用Post方式向網(wǎng)頁(yè)發(fā)送數(shù)據(jù)
  4. ASP.NET 2.0部署WEB應(yīng)用程序淺析
  5. ASP.NET中的HttpWorkerRequest對(duì)像
  6. 介紹ASP.NET MVC框架
責(zé)任編輯:冰荷 來(lái)源: th7
相關(guān)推薦

2009-08-19 13:44:00

ASP.NET Lis

2009-08-04 10:43:59

ASP.NET控件開(kāi)發(fā)

2009-08-07 14:42:02

ASP.NET控件開(kāi)發(fā)

2009-07-27 14:50:24

ChartAreas控ASP.NET 3.5

2009-07-24 15:07:56

ASP.NET上傳文件

2009-08-04 13:10:05

ASP.NET服務(wù)器控

2009-07-27 13:52:36

Panel控件ASP.NET

2009-07-29 16:08:07

ASP和ASP.NET

2009-09-11 09:09:00

ASP.NETAdRotator控件

2009-07-20 13:32:24

ScriptManagASP.NET

2009-08-04 11:29:14

HTML代碼ASP.NET控件

2009-07-24 15:35:00

ASP.NET Gri

2009-07-29 13:50:26

UpdatePanelASP.NET

2009-08-05 15:57:03

ASP.NET控件ID

2009-07-23 13:19:51

2011-04-13 15:13:01

ASP.NET

2011-04-19 10:33:16

ASP.NET自定義控

2009-08-17 09:24:25

ASP.NET控件

2009-07-27 16:19:59

ASP.NET報(bào)表控件

2009-08-03 15:08:00

SqlDataSour
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)