2 // System.Web.UI.WebControls.BulletedList.cs
5 // Ben Maurer (bmaurer@users.sourceforge.net)
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System.Collections;
33 using System.Globalization;
36 using System.ComponentModel;
37 using System.ComponentModel.Design;
38 using System.Security.Permissions;
39 using System.Web.Util;
41 namespace System.Web.UI.WebControls
44 [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
45 [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
47 [DesignerAttribute ("System.Web.UI.Design.WebControls.BulletedListDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
48 [DefaultEventAttribute ("Click")]
49 [DefaultPropertyAttribute ("BulletStyle")]
50 [SupportsEventValidation]
51 public class BulletedList : ListControl, IPostBackEventHandler
53 static readonly object ClickEvent = new object ();
54 PostBackOptions postBackOptions;
56 public event BulletedListEventHandler Click {
57 add { Events.AddHandler (ClickEvent, value); }
58 remove { Events.RemoveHandler (ClickEvent, value); }
61 [MonoTODO ("we are missing a new style enum, we should be using it")]
62 protected override void AddAttributesToRender (HtmlTextWriter writer)
64 const string ListStyleType = "list-style-type";
65 const string ListStyleImage = "list-style-image";
67 bool isNumeric = false;
68 switch (BulletStyle) {
69 case BulletStyle.NotSet:
72 case BulletStyle.Numbered:
73 writer.AddStyleAttribute (ListStyleType, "decimal");
77 case BulletStyle.LowerAlpha:
78 writer.AddStyleAttribute (ListStyleType, "lower-alpha");
82 case BulletStyle.UpperAlpha:
83 writer.AddStyleAttribute (ListStyleType, "upper-alpha");
87 case BulletStyle.LowerRoman:
88 writer.AddStyleAttribute (ListStyleType, "lower-roman");
92 case BulletStyle.UpperRoman:
93 writer.AddStyleAttribute (ListStyleType, "upper-roman");
97 case BulletStyle.Disc:
98 writer.AddStyleAttribute (ListStyleType, "disc");
101 case BulletStyle.Circle:
102 writer.AddStyleAttribute (ListStyleType, "circle");
105 case BulletStyle.Square:
106 writer.AddStyleAttribute (ListStyleType, "square");
109 case BulletStyle.CustomImage:
110 writer.AddStyleAttribute (ListStyleImage, "url(" + ResolveClientUrl (BulletImageUrl) + ")");
114 if (isNumeric && FirstBulletNumber != 1)
115 writer.AddAttribute ("start", FirstBulletNumber.ToString ());
117 base.AddAttributesToRender (writer);
120 protected virtual void RenderBulletText (ListItem item, int index, HtmlTextWriter writer)
122 string text = HttpUtility.HtmlEncode (item.Text);
124 switch (DisplayMode) {
125 case BulletedListDisplayMode.Text:
127 writer.AddAttribute (HtmlTextWriterAttribute.Disabled, "disabled", false);
128 writer.RenderBeginTag (HtmlTextWriterTag.Span);
134 writer.RenderEndTag ();
138 case BulletedListDisplayMode.HyperLink:
139 if (IsEnabled && item.Enabled) {
140 writer.AddAttribute (HtmlTextWriterAttribute.Href, item.Value);
141 if (Target.Length > 0)
142 writer.AddAttribute(HtmlTextWriterAttribute.Target, this.Target);
145 writer.AddAttribute (HtmlTextWriterAttribute.Disabled, "disabled", false);
147 writer.RenderBeginTag (HtmlTextWriterTag.A);
149 writer.RenderEndTag ();
152 case BulletedListDisplayMode.LinkButton:
153 if (IsEnabled && item.Enabled)
154 writer.AddAttribute (HtmlTextWriterAttribute.Href, Page.ClientScript.GetPostBackEventReference (GetPostBackOptions (index.ToString (Helpers.InvariantCulture)), true));
156 writer.AddAttribute (HtmlTextWriterAttribute.Disabled, "disabled", false);
157 writer.RenderBeginTag (HtmlTextWriterTag.A);
159 writer.RenderEndTag ();
164 PostBackOptions GetPostBackOptions (string argument)
166 if (postBackOptions == null) {
167 postBackOptions = new PostBackOptions (this);
168 postBackOptions.ActionUrl = null;
169 postBackOptions.ValidationGroup = null;
170 postBackOptions.RequiresJavaScriptProtocol = true;
171 postBackOptions.ClientSubmit = true;
172 postBackOptions.PerformValidation = CausesValidation && Page != null && Page.AreValidatorsUplevel (ValidationGroup);
173 if (postBackOptions.PerformValidation)
174 postBackOptions.ValidationGroup = ValidationGroup;
176 postBackOptions.Argument = argument;
177 return postBackOptions;
180 protected internal override void RenderContents (HtmlTextWriter writer)
184 ClientScriptManager scriptManager = page != null ? page.ClientScript : null;
186 foreach (ListItem i in Items) {
188 scriptManager.RegisterForEventValidation (UniqueID, i.Value);
191 i.Attributes.AddAttributes (writer);
193 writer.RenderBeginTag (HtmlTextWriterTag.Li);
194 this.RenderBulletText (i, idx ++, writer);
195 writer.RenderEndTag ();
199 protected internal override void Render (HtmlTextWriter writer)
201 base.Render (writer);
204 void IPostBackEventHandler.RaisePostBackEvent (string eventArgument)
206 RaisePostBackEvent (eventArgument);
209 protected virtual void RaisePostBackEvent (string eventArgument)
211 ValidateEvent (UniqueID, eventArgument);
212 if (CausesValidation)
213 Page.Validate (ValidationGroup);
215 this.OnClick (new BulletedListEventArgs (int.Parse (eventArgument, Helpers.InvariantCulture)));
218 [BrowsableAttribute (false)]
219 [EditorBrowsableAttribute (EditorBrowsableState.Never)]
220 public override bool AutoPostBack {
221 get { return base.AutoPostBack; }
222 set { throw new NotSupportedException (String.Format ("This property is not supported in {0}", GetType ())); }
226 [EditorBrowsableAttribute (EditorBrowsableState.Never)]
227 public override int SelectedIndex {
229 set { throw new NotSupportedException (String.Format ("This property is not supported in {0}", GetType ())); }
232 [EditorBrowsableAttribute (EditorBrowsableState.Never)]
233 public override ListItem SelectedItem {
237 [EditorBrowsable (EditorBrowsableState.Never)]
239 public override string SelectedValue {
240 get { return string.Empty; }
241 set { throw new NotSupportedException (); }
244 [DefaultValueAttribute ("")]
245 [EditorAttribute ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
246 [UrlPropertyAttribute]
247 public virtual string BulletImageUrl {
248 get { return ViewState.GetString ("BulletImageUrl", String.Empty); }
249 set { ViewState ["BulletImageUrl"] = value; }
252 [DefaultValueAttribute (BulletStyle.NotSet)]
253 public virtual BulletStyle BulletStyle {
254 get { return (BulletStyle) ViewState.GetInt ("BulletStyle", (int) BulletStyle.NotSet); }
256 if ((int) value < 0 || (int) value > 9)
257 throw new ArgumentOutOfRangeException ("value");
259 ViewState ["BulletStyle"] = value;
263 public override ControlCollection Controls { get { return new EmptyControlCollection (this); } }
265 [DefaultValueAttribute (BulletedListDisplayMode.Text)]
266 public virtual BulletedListDisplayMode DisplayMode {
267 get { return (BulletedListDisplayMode) ViewState.GetInt ("DisplayMode", (int)BulletedListDisplayMode.Text); }
269 if ((int) value < 0 || (int) value > 2)
270 throw new ArgumentOutOfRangeException ("value");
272 ViewState ["DisplayMode"] = value;
276 [DefaultValueAttribute (1)]
277 public virtual int FirstBulletNumber {
278 get { return ViewState.GetInt ("FirstBulletNumber", 1); }
279 set { ViewState ["FirstBulletNumber"] = value; }
282 protected override HtmlTextWriterTag TagKey {
284 switch (BulletStyle) {
285 case BulletStyle.Numbered:
286 case BulletStyle.LowerAlpha:
287 case BulletStyle.UpperAlpha:
288 case BulletStyle.LowerRoman:
289 case BulletStyle.UpperRoman:
290 return HtmlTextWriterTag.Ol;
292 case BulletStyle.NotSet:
293 case BulletStyle.Disc:
294 case BulletStyle.Circle:
295 case BulletStyle.Square:
296 case BulletStyle.CustomImage:
298 return HtmlTextWriterTag.Ul;
303 [DefaultValueAttribute ("")]
304 [TypeConverter (typeof (TargetConverter))]
305 public virtual string Target {
306 get { return ViewState.GetString ("Target", String.Empty); }
307 set { ViewState ["Target"] = value; }
310 [EditorBrowsable (EditorBrowsableState.Never)]
311 public override string Text {
312 get { return String.Empty; }
313 set { throw new NotSupportedException (); }
316 protected virtual void OnClick (BulletedListEventArgs e)
318 if (Events != null) {
319 BulletedListEventHandler eh = (BulletedListEventHandler) (Events [ClickEvent]);