1 // 2 // System.Web.UI.WebControls.Literal.cs 3 // 4 // Authors: 5 // Jackson Harper (jackson@ximian.com) 6 // 7 // (C) 2005 Novell, Inc (http://www.novell.com) 8 // 9 // Permission is hereby granted, free of charge, to any person obtaining 10 // a copy of this software and associated documentation files (the 11 // "Software"), to deal in the Software without restriction, including 12 // without limitation the rights to use, copy, modify, merge, publish, 13 // distribute, sublicense, and/or sell copies of the Software, and to 14 // permit persons to whom the Software is furnished to do so, subject to 15 // the following conditions: 16 // 17 // The above copyright notice and this permission notice shall be 18 // included in all copies or substantial portions of the Software. 19 // 20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 27 // 28 29 using System.Collections; 30 using System.ComponentModel; 31 using System.Drawing; 32 using System.Globalization; 33 using System.Collections.Specialized; 34 using System.Security.Permissions; 35 using System.Web.Util; 36 37 namespace System.Web.UI.WebControls { 38 39 // CAS 40 [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)] 41 [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)] 42 // attributes 43 [ValidationProperty("SelectedItem")] 44 [SupportsEventValidation] 45 public class ListBox : ListControl, IPostBackDataHandler { 46 ListBox()47 public ListBox () 48 { 49 } 50 51 [Browsable(false)] 52 #if HAVE_CONTROL_ADAPTERS 53 public virtual new 54 #else 55 public override 56 #endif 57 Color BorderColor { 58 get { return base.BorderColor; } 59 set { base.BorderColor = value; } 60 } 61 62 [Browsable(false)] 63 #if HAVE_CONTROL_ADAPTERS 64 public virtual new 65 #else 66 public override 67 #endif 68 BorderStyle BorderStyle { 69 get { return base.BorderStyle; } 70 set { base.BorderStyle = value; } 71 } 72 73 [Browsable(false)] 74 #if HAVE_CONTROL_ADAPTERS 75 public virtual new 76 #else 77 public override 78 #endif 79 Unit BorderWidth { 80 get { return base.BorderWidth; } 81 set { base.BorderWidth = value; } 82 } 83 84 [DefaultValue(4)] 85 [WebSysDescription ("")] 86 [WebCategory ("Appearance")] 87 public virtual int Rows { 88 get { 89 return ViewState.GetInt ("Rows", 4); 90 } 91 set { 92 if (value < 1 || value > 2000) 93 throw new ArgumentOutOfRangeException ("value"); 94 ViewState ["Rows"] = value; 95 } 96 } 97 98 [DefaultValue(ListSelectionMode.Single)] 99 [WebSysDescription ("")] 100 [WebCategory ("Behavior")] 101 public virtual ListSelectionMode SelectionMode { 102 get { 103 return (ListSelectionMode) ViewState.GetInt ("SelectionMode", 104 (int) ListSelectionMode.Single); 105 } 106 set { 107 if (!Enum.IsDefined (typeof (ListSelectionMode), value)) 108 throw new ArgumentOutOfRangeException ("value"); 109 ViewState ["SelectionMode"] = value; 110 } 111 } 112 113 GetSelectedIndices()114 public virtual int[] GetSelectedIndices () 115 { 116 return (int []) GetSelectedIndicesInternal ().ToArray (typeof (int)); 117 } 118 AddAttributesToRender(HtmlTextWriter writer)119 protected override void AddAttributesToRender (HtmlTextWriter writer) 120 { 121 if (Page != null) 122 Page.VerifyRenderingInServerForm (this); 123 124 if (ID != null) 125 writer.AddAttribute (HtmlTextWriterAttribute.Name, UniqueID); 126 127 if (AutoPostBack) { 128 string onchange = Page.ClientScript.GetPostBackEventReference (GetPostBackOptions (), true); 129 onchange = String.Concat ("setTimeout('", onchange.Replace ("\\", "\\\\").Replace ("'", "\\'"), "', 0)"); 130 writer.AddAttribute (HtmlTextWriterAttribute.Onchange, BuildScriptAttribute ("onchange", onchange)); 131 } 132 133 if (SelectionMode == ListSelectionMode.Multiple) 134 writer.AddAttribute (HtmlTextWriterAttribute.Multiple, 135 "multiple", false); 136 writer.AddAttribute (HtmlTextWriterAttribute.Size, 137 Rows.ToString (Helpers.InvariantCulture)); 138 139 base.AddAttributesToRender (writer); 140 } 141 GetPostBackOptions()142 PostBackOptions GetPostBackOptions () { 143 PostBackOptions options = new PostBackOptions (this); 144 options.ActionUrl = null; 145 options.ValidationGroup = null; 146 options.Argument = String.Empty; 147 options.RequiresJavaScriptProtocol = false; 148 options.ClientSubmit = true; 149 options.PerformValidation = CausesValidation && Page != null && Page.AreValidatorsUplevel (ValidationGroup); 150 if (options.PerformValidation) 151 options.ValidationGroup = ValidationGroup; 152 153 return options; 154 } 155 156 157 protected internal OnPreRender(EventArgs e)158 override void OnPreRender (EventArgs e) 159 { 160 base.OnPreRender (e); 161 Page page = Page; 162 if (page != null && IsEnabled) 163 page.RegisterRequiresPostBack (this); 164 } 165 166 protected virtual LoadPostData(string postDataKey, NameValueCollection postCollection)167 bool LoadPostData (string postDataKey, NameValueCollection postCollection) 168 { 169 EnsureDataBound (); 170 string [] values = postCollection.GetValues (postDataKey); 171 if (values == null || values.Length == 0) { 172 int prev_index = SelectedIndex; 173 SelectedIndex = -1; 174 return (prev_index != -1); 175 } 176 ValidateEvent (UniqueID, values [0]); 177 178 if (SelectionMode == ListSelectionMode.Single) 179 return SelectSingle (values); 180 return SelectMultiple (values); 181 } 182 SelectSingle(string [] values)183 bool SelectSingle (string [] values) 184 { 185 string val = values [0]; 186 int idx = Items.IndexOf (val); 187 int prev_index = SelectedIndex; 188 if (idx != prev_index) { 189 // This will set both the index value and the item.Selected property 190 SelectedIndex = idx; 191 return true; 192 } 193 return false; 194 } 195 SelectMultiple(string [] values)196 bool SelectMultiple (string [] values) 197 { 198 ArrayList prev_selected = GetSelectedIndicesInternal (); 199 ClearSelection (); 200 foreach (string val in values) { 201 ListItem item = Items.FindByValue (val); 202 if (item != null) 203 item.Selected = true; 204 } 205 206 ArrayList new_selection = GetSelectedIndicesInternal (); 207 int i = prev_selected.Count; 208 if (new_selection.Count != i) 209 return true; 210 211 while (--i >= 0) { 212 if ((int) prev_selected [i] != (int) new_selection [i]) 213 return true; 214 } 215 216 return false; 217 } 218 219 protected virtual RaisePostDataChangedEvent()220 void RaisePostDataChangedEvent () 221 { 222 if (CausesValidation) 223 Page.Validate (ValidationGroup); 224 OnSelectedIndexChanged (EventArgs.Empty); 225 } 226 IPostBackDataHandler.LoadPostData(string postDataKey, NameValueCollection postCollection)227 bool IPostBackDataHandler.LoadPostData (string postDataKey, 228 NameValueCollection postCollection) 229 { 230 return LoadPostData (postDataKey, postCollection); 231 } 232 IPostBackDataHandler.RaisePostDataChangedEvent()233 void IPostBackDataHandler.RaisePostDataChangedEvent () 234 { 235 RaisePostDataChangedEvent (); 236 } MultiSelectOk()237 internal override bool MultiSelectOk () 238 { 239 return this.SelectionMode == ListSelectionMode.Multiple; 240 } 241 } 242 } 243 244 245 246