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;
40 namespace System.Web.UI.WebControls {
43 [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
44 [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
46 [DesignerAttribute ("System.Web.UI.Design.WebControls.BulletedListDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
47 [DefaultEventAttribute ("Click")]
48 [DefaultPropertyAttribute ("BulletStyle")]
49 [SupportsEventValidation]
50 public class BulletedList : ListControl, IPostBackEventHandler {
52 PostBackOptions postBackOptions;
54 [MonoTODO ("we are missing a new style enum, we should be using it")]
55 protected override void AddAttributesToRender (HtmlTextWriter writer)
57 const string ListStyleType = "list-style-type";
58 const string ListStyleImage = "list-style-image";
60 bool isNumeric = false;
63 case BulletStyle.NotSet:
66 case BulletStyle.Numbered:
67 writer.AddStyleAttribute (ListStyleType, "decimal");
71 case BulletStyle.LowerAlpha:
72 writer.AddStyleAttribute (ListStyleType, "lower-alpha");
76 case BulletStyle.UpperAlpha:
77 writer.AddStyleAttribute (ListStyleType, "upper-alpha");
81 case BulletStyle.LowerRoman:
82 writer.AddStyleAttribute (ListStyleType, "lower-roman");
86 case BulletStyle.UpperRoman:
87 writer.AddStyleAttribute (ListStyleType, "upper-roman");
91 case BulletStyle.Disc:
92 writer.AddStyleAttribute (ListStyleType, "disc");
95 case BulletStyle.Circle:
96 writer.AddStyleAttribute (ListStyleType, "circle");
99 case BulletStyle.Square:
100 writer.AddStyleAttribute (ListStyleType, "square");
103 case BulletStyle.CustomImage:
104 writer.AddStyleAttribute (ListStyleImage, "url(" + ResolveClientUrl (BulletImageUrl) + ")");
108 if (isNumeric && FirstBulletNumber != 1)
109 writer.AddAttribute ("start", FirstBulletNumber.ToString ());
111 base.AddAttributesToRender (writer);
114 protected virtual void RenderBulletText (ListItem item, int index, HtmlTextWriter writer)
116 string text = HttpUtility.HtmlEncode (item.Text);
118 switch (DisplayMode) {
119 case BulletedListDisplayMode.Text:
121 writer.AddAttribute (HtmlTextWriterAttribute.Disabled, "disabled", false);
122 writer.RenderBeginTag (HtmlTextWriterTag.Span);
128 writer.RenderEndTag ();
132 case BulletedListDisplayMode.HyperLink:
133 if (Enabled && item.Enabled) {
134 writer.AddAttribute (HtmlTextWriterAttribute.Href, item.Value);
136 writer.AddAttribute(HtmlTextWriterAttribute.Target, this.Target);
140 writer.AddAttribute (HtmlTextWriterAttribute.Disabled, "disabled", false);
142 writer.RenderBeginTag (HtmlTextWriterTag.A);
144 writer.RenderEndTag ();
147 case BulletedListDisplayMode.LinkButton:
148 if (Enabled && item.Enabled)
149 writer.AddAttribute (HtmlTextWriterAttribute.Href, Page.ClientScript.GetPostBackEventReference (GetPostBackOptions (index.ToString (CultureInfo.InvariantCulture)), true));
151 writer.AddAttribute (HtmlTextWriterAttribute.Disabled, "disabled", false);
152 writer.RenderBeginTag (HtmlTextWriterTag.A);
154 writer.RenderEndTag ();
159 PostBackOptions GetPostBackOptions (string argument) {
160 if (postBackOptions == null) {
161 postBackOptions = new PostBackOptions (this);
162 postBackOptions.ActionUrl = null;
163 postBackOptions.ValidationGroup = null;
164 postBackOptions.RequiresJavaScriptProtocol = true;
165 postBackOptions.ClientSubmit = true;
166 postBackOptions.PerformValidation = CausesValidation && Page != null && Page.AreValidatorsUplevel (ValidationGroup);
167 if (postBackOptions.PerformValidation)
168 postBackOptions.ValidationGroup = ValidationGroup;
170 postBackOptions.Argument = argument;
171 return postBackOptions;
174 protected internal override void RenderContents (HtmlTextWriter writer)
178 bool havePage = Page != null;
180 foreach (ListItem i in Items) {
183 Page.ClientScript.RegisterForEventValidation (this.UniqueID, i.Value.ToString ());
186 i.Attributes.AddAttributes (writer);
188 writer.RenderBeginTag (HtmlTextWriterTag.Li);
189 this.RenderBulletText (i, idx ++, writer);
190 writer.RenderEndTag ();
194 protected internal override void Render (HtmlTextWriter writer)
196 base.Render (writer);
199 void IPostBackEventHandler.RaisePostBackEvent (string eventArgument)
201 RaisePostBackEvent (eventArgument);
204 protected virtual void RaisePostBackEvent (string eventArgument)
206 if (CausesValidation)
207 Page.Validate (ValidationGroup);
209 this.OnClick (new BulletedListEventArgs (int.Parse (eventArgument, CultureInfo.InvariantCulture)));
212 [BrowsableAttribute (false)]
213 [EditorBrowsableAttribute (EditorBrowsableState.Never)]
214 public override bool AutoPostBack {
215 get { return base.AutoPostBack; }
216 set { throw new NotSupportedException (String.Format ("This property is not supported in {0}", GetType ())); }
220 [EditorBrowsableAttribute (EditorBrowsableState.Never)]
221 public override int SelectedIndex {
223 set { throw new NotSupportedException (String.Format ("This property is not supported in {0}", GetType ())); }
226 [EditorBrowsableAttribute (EditorBrowsableState.Never)]
227 public override ListItem SelectedItem {
231 [EditorBrowsable (EditorBrowsableState.Never)]
233 public override string SelectedValue
235 get { return string.Empty; }
236 set { throw new NotSupportedException (); }
239 [DefaultValueAttribute ("")]
240 [EditorAttribute ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
241 [UrlPropertyAttribute]
242 public virtual string BulletImageUrl {
243 get { return ViewState.GetString ("BulletImageUrl", ""); }
244 set { ViewState ["BulletImageUrl"] = value; }
247 [DefaultValueAttribute (BulletStyle.NotSet)]
248 public virtual BulletStyle BulletStyle {
249 get { return (BulletStyle) ViewState.GetInt ("BulletStyle", (int) BulletStyle.NotSet); }
251 if ((int) value < 0 || (int) value > 9)
252 throw new ArgumentOutOfRangeException ("value");
254 ViewState ["BulletStyle"] = value;
258 public override ControlCollection Controls { get { return new EmptyControlCollection (this); } }
260 [DefaultValueAttribute (BulletedListDisplayMode.Text)]
261 public virtual BulletedListDisplayMode DisplayMode {
262 get { return (BulletedListDisplayMode) ViewState.GetInt ("DisplayMode", (int)BulletedListDisplayMode.Text); }
264 if ((int) value < 0 || (int) value > 2)
265 throw new ArgumentOutOfRangeException ("value");
267 ViewState ["DisplayMode"] = value;
271 [DefaultValueAttribute (1)]
272 public virtual int FirstBulletNumber {
273 get { return ViewState.GetInt ("FirstBulletNumber", 1); }
274 set { ViewState ["FirstBulletNumber"] = value; }
278 protected override HtmlTextWriterTag TagKey {
280 switch (BulletStyle) {
281 case BulletStyle.Numbered:
282 case BulletStyle.LowerAlpha:
283 case BulletStyle.UpperAlpha:
284 case BulletStyle.LowerRoman:
285 case BulletStyle.UpperRoman:
286 return HtmlTextWriterTag.Ol;
288 case BulletStyle.NotSet:
289 case BulletStyle.Disc:
290 case BulletStyle.Circle:
291 case BulletStyle.Square:
292 case BulletStyle.CustomImage:
294 return HtmlTextWriterTag.Ul;
299 [DefaultValueAttribute ("")]
300 [TypeConverter (typeof (TargetConverter))]
301 public virtual string Target {
302 get { return ViewState.GetString ("Target", ""); }
303 set { ViewState ["Target"] = value; }
306 [EditorBrowsable (EditorBrowsableState.Never)]
307 public override string Text
309 get { return string.Empty; }
310 set { throw new NotSupportedException (); }
314 static readonly object ClickEvent = new object ();
315 public event BulletedListEventHandler Click
318 Events.AddHandler (ClickEvent, value);
321 Events.RemoveHandler (ClickEvent, value);
325 protected virtual void OnClick (BulletedListEventArgs e)
327 if (Events != null) {
328 BulletedListEventHandler eh = (BulletedListEventHandler) (Events [ClickEvent]);