1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
|
//------------------------------------------------------------------------------
// <copyright file="listitem.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
//------------------------------------------------------------------------------
namespace System.Web.UI.WebControls {
using System;
using System.Collections;
using System.ComponentModel;
using System.Globalization;
using AttributeCollection = System.Web.UI.AttributeCollection;
using System.Web.Util;
/// <devdoc>
/// <para>Interacts with the parser to build a <see cref='System.Web.UI.WebControls.ListItem'/> control.</para>
/// </devdoc>
public class ListItemControlBuilder : ControlBuilder {
public override bool AllowWhitespaceLiterals() {
return false;
}
public override bool HtmlDecodeLiterals() {
// ListItem text gets rendered as an encoded attribute value.
// At parse time text specified as an attribute gets decoded, and so text specified as a
// literal needs to go through the same process.
return true;
}
}
/// <devdoc>
/// <para>Constructs a list item control and defines
/// its properties. This class cannot be inherited.</para>
/// </devdoc>
[
ControlBuilderAttribute(typeof(ListItemControlBuilder)),
TypeConverterAttribute(typeof(ExpandableObjectConverter)),
ParseChildren(true, "Text"),
]
public sealed class ListItem : IStateManager, IParserAccessor, IAttributeAccessor {
private bool selected;
private bool marked;
private bool textisdirty;
private bool valueisdirty;
private bool enabled;
private bool enabledisdirty;
private string text;
private string value;
private AttributeCollection _attributes;
/// <devdoc>
/// <para>Initializes a new instance of the <see cref='System.Web.UI.WebControls.ListItem'/> class.</para>
/// </devdoc>
public ListItem() : this(null, null) {
}
/// <devdoc>
/// <para>Initializes a new instance of the <see cref='System.Web.UI.WebControls.ListItem'/> class with the specified text data.</para>
/// </devdoc>
public ListItem(string text) : this(text, null) {
}
/// <devdoc>
/// <para>Initializes a new instance of the <see cref='System.Web.UI.WebControls.ListItem'/> class with the specified text data.</para>
/// </devdoc>
public ListItem(string text, string value) : this(text, value, true) {
}
/// <devdoc>
/// <para>Initializes a new instance of the <see cref='System.Web.UI.WebControls.ListItem'/> class with the
/// specified text and value data.</para>
/// </devdoc>
public ListItem(string text, string value, bool enabled) {
this.text = text;
this.value = value;
this.enabled = enabled;
}
/// <devdoc>
/// <para>Gets the collection of attribute name/value pairs expressed on the list item
/// control but not supported by the control's strongly typed properties.</para>
/// </devdoc>
[
Browsable(false),
DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)
]
public AttributeCollection Attributes {
get {
if (_attributes == null)
_attributes = new AttributeCollection(new StateBag(true));
return _attributes;
}
}
/// <devdoc>
/// Internal property used to manage dirty state of ListItem.
/// </devdoc>
internal bool Dirty {
get {
return (textisdirty || valueisdirty || enabledisdirty);
}
set {
textisdirty = value;
valueisdirty = value;
enabledisdirty = value;
}
}
[
DefaultValue(true)
]
public bool Enabled {
get {
return enabled;
}
set {
enabled = value;
if (((IStateManager)this).IsTrackingViewState)
enabledisdirty = true;
}
}
internal bool HasAttributes {
get {
return _attributes != null && _attributes.Count > 0;
}
}
/// <devdoc>
/// <para>Specifies a value indicating whether the
/// item is selected.</para>
/// </devdoc>
[
DefaultValue(false),
TypeConverter(typeof(MinimizableAttributeTypeConverter))
]
public bool Selected {
get {
return selected;
}
set {
selected = value;
}
}
/// <devdoc>
/// <para>Gets or sets the display text of the list
/// item control.</para>
/// </devdoc>
[
Localizable(true),
DefaultValue(""),
PersistenceMode(PersistenceMode.EncodedInnerDefaultProperty)
]
public string Text {
get {
if (text != null)
return text;
if (value != null)
return value;
return String.Empty;
}
set {
text = value;
if (((IStateManager)this).IsTrackingViewState)
textisdirty = true;
}
}
/// <devdoc>
/// <para>Gets or sets the value content of the list item control.</para>
/// </devdoc>
[
Localizable(true),
DefaultValue("")
]
public string Value {
get {
if (value != null)
return value;
if (text != null)
return text;
return String.Empty;
}
set {
this.value = value;
if (((IStateManager)this).IsTrackingViewState)
valueisdirty =true;
}
}
/// <internalonly/>
public override int GetHashCode() {
return HashCodeCombiner.CombineHashCodes(Value.GetHashCode(), Text.GetHashCode());
}
/// <internalonly/>
/// <devdoc>
/// </devdoc>
public override bool Equals(object o) {
ListItem other = o as ListItem;
if (other != null) {
return Value.Equals(other.Value) && Text.Equals(other.Text);
}
return false;
}
/// <devdoc>
/// <para>Creates a <see cref='System.Web.UI.WebControls.ListItem'/> from the specified string.</para>
/// </devdoc>
public static ListItem FromString(string s) {
return new ListItem(s);
}
/// <internalonly/>
/// <devdoc>
/// </devdoc>
public override string ToString() {
return this.Text;
}
/// <internalonly/>
/// <devdoc>
/// Return true if tracking state changes.
/// Method of private interface, IStateManager.
/// </devdoc>
bool IStateManager.IsTrackingViewState {
get {
return marked;
}
}
/// <internalonly/>
void IStateManager.LoadViewState(object state) {
LoadViewState(state);
}
internal void LoadViewState(object state) {
if (state != null) {
if (state is Triplet) {
Triplet t = (Triplet) state;
if (t.First != null) {
Text = (string) t.First;
}
if (t.Second != null) {
Value = (string) t.Second;
}
if (t.Third != null) {
try {
Enabled = (bool) t.Third;
}
catch {
}
}
}
else if (state is Pair) {
Pair p = (Pair) state;
if (p.First != null)
Text = (string) p.First;
Value = (string) p.Second;
}
else
Text = (string) state;
}
}
/// <internalonly/>
void IStateManager.TrackViewState() {
TrackViewState();
}
internal void TrackViewState() {
marked = true;
}
internal void RenderAttributes(HtmlTextWriter writer) {
if (_attributes != null) {
_attributes.AddAttributes(writer);
}
}
/// <internalonly/>
object IStateManager.SaveViewState() {
return SaveViewState();
}
internal object SaveViewState() {
string text = null;
string value = null;
if(textisdirty) {
text = Text;
}
if(valueisdirty) {
value = Value;
}
if(enabledisdirty) {
return new Triplet(text, value, Enabled);
}
else if(valueisdirty) {
return new Pair(text, value);
}
else if(textisdirty) {
return text;
}
return null;
}
/// <internalonly/>
/// <devdoc>
/// Returns the attribute value of the list item control
/// having the specified attribute name.
/// </devdoc>
string IAttributeAccessor.GetAttribute(string name) {
return Attributes[name];
}
/// <internalonly/>
/// <devdoc>
/// <para>Sets an attribute of the list
/// item control with the specified name and value.</para>
/// </devdoc>
void IAttributeAccessor.SetAttribute(string name, string value) {
Attributes[name] = value;
}
/// <internalonly/>
/// <devdoc>
/// <para> Allows the <see cref='System.Web.UI.WebControls.ListItem.Text'/>
/// property to be persisted as inner content.</para>
/// </devdoc>
void IParserAccessor.AddParsedSubObject(object obj) {
if (obj is LiteralControl) {
Text = ((LiteralControl)obj).Text;
}
else {
if (obj is DataBoundLiteralControl) {
throw new HttpException(SR.GetString(SR.Control_Cannot_Databind, "ListItem"));
}
else {
throw new HttpException(SR.GetString(SR.Cannot_Have_Children_Of_Type, "ListItem", obj.GetType().Name.ToString(CultureInfo.InvariantCulture)));
}
}
}
/// <devdoc>
/// Do not change the signature or remove this method. It is called via reflection
/// to support correct serialization behavior for Text and Value which are
/// implemented as dependent properties.
/// </devdoc>
private void ResetText() {
Text = null;
}
/// <devdoc>
/// Do not change the signature or remove this method. It is called via reflection
/// to support correct serialization behavior for Text and Value which are
/// implemented as dependent properties.
/// </devdoc>
private void ResetValue() {
Value = null;
}
/// <devdoc>
/// Do not change the signature or remove this method. It is called via reflection
/// to support correct serialization behavior for Text and Value which are
/// implemented as dependent properties.
/// </devdoc>
private bool ShouldSerializeText() {
return (text != null) && (text.Length != 0);
}
/// <devdoc>
/// Do not change the signature or remove this method. It is called via reflection
/// to support correct serialization behavior for Text and Value which are
/// implemented as dependent properties.
/// </devdoc>
private bool ShouldSerializeValue() {
return (value != null) && (value.Length != 0);
}
}
}
|