详解ASP.NET的Multi-ListBox控件

开发 后端
本文向您介绍ASP.NET Multi-ListBox控件编程知识:主要讲述页面的生命周期的理解以及Multi-ListBox控件的两个主要方法。

开发一个优秀的数据绑定不是一件很容易的事情。刚开始的时候走了一些弯路,一直紧紧咬着 DataBoundControl类不放。最终失望之后冷静下来想到关于DataSource不就是一个数据集合吗?明白之后,有关数据源的问题基本上也解决了。在整个ASP.NET Multi-ListBox控件控件开发中,我认为最重要的实际上就是页面的生命周期的理解,如果您基本上理解了它的话,那么,基本上,你以后开发一款ASP.NET控件也不是一件很难的事情。我们还是简单了解开发的思路吧。

在ASP.NET Multi-ListBox控件的生命周期中,我们主要需要解决用户回发页面的时候保留ListBox的数据源(因为我没有采用复合控件的方式来开发)。因此,我们需要重写控件的SaveViewState, LoadViewState二个方法。

  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 //启用控件视图状态   
  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 }   

为了省事,我没有自定义ListItem类,改为直接使用ListItemCollection来存储数据。因为MS没有提供ListItemCollection. SaveViewState和LoadViewState,我们必须采用反射的方式来调用这二个方法来保存数据。很让人郁闷。每当到紧要关头,就会发现MS写的类,方法不是internal,就是sealed。无可奈何~当然,你也可以自己写一个类来代替ListItem类.

我们在页面上进行ListBox进行左移,右移的数据全部需要按一定的格式临时存储在HiddenField控件中,这样我们可以通过继承IPostBackDataHandler 接口中的LoadPostData方法获取我们临时存储的数据,对ListBox的数据源进行添加,移除等操作。

  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]); 软件开发网 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 }   

一切就是这么简单,就是SaveViewaState,LoadViewState,LoadPostData顺序。后面二个是页面回发的时候才会触发。只要解决这里,***不过就是呈现控件而已。 #p#

如果在页面中使用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 }   

就像前面所说那样,目前只完成的基本的功能,像如果页面放了多个控件之后的问题,让开发人员自定义修改Control Panel的图标,自定义JS路径等都还没有考虑完全(时间有限,只有等以后慢慢完善)。如何跟SqlDataSource控件结合?如何直接可编辑ListBox的Items属性就能呈现?呵呵。需要挑战的还有许多地方。

【编辑推荐】

  1. 创建ASP.NET 2.0应用程序
  2. ASP.NET数据缓存四大方案
  3. ASP.NET用Post方式向网页发送数据
  4. ASP.NET 2.0部署WEB应用程序浅析
  5. ASP.NET中的HttpWorkerRequest对像
  6. 介绍ASP.NET MVC框架
责任编辑:冰荷 来源: th7
相关推荐

2009-08-19 13:44:00

ASP.NET Lis

2009-08-04 10:43:59

ASP.NET控件开发

2009-08-07 14:42:02

ASP.NET控件开发

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服务器控

2009-07-27 13:52:36

Panel控件ASP.NET

2009-07-24 15:35:00

ASP.NET Gri

2009-07-29 16:08:07

ASP和ASP.NET

2009-08-04 11:29:14

HTML代码ASP.NET控件

2009-09-11 09:09:00

ASP.NETAdRotator控件

2009-07-20 13:32:24

ScriptManagASP.NET

2011-04-13 15:13:01

ASP.NET

2009-08-17 09:24:25

ASP.NET控件

2009-08-03 15:08:00

SqlDataSour

2009-07-27 17:25:53

ASP.NET验证控件

2009-07-27 16:19:59

ASP.NET报表控件

2009-07-29 13:50:26

UpdatePanelASP.NET

2009-08-05 15:57:03

ASP.NET控件ID

2011-04-19 10:33:16

ASP.NET自定义控
点赞
收藏

51CTO技术栈公众号