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 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663
|
//------------------------------------------------------------------------------
// <copyright file="Parameter.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.Data;
using System.Diagnostics;
using System.Globalization;
/// <devdoc>
/// Represents a parameter to a DataSourceControl.
/// Parameters can be session variables, web request parameters, or of custom types.
/// </devdoc>
[
DefaultProperty("DefaultValue"),
]
public class Parameter : ICloneable, IStateManager {
private ParameterCollection _owner;
private bool _tracking;
private StateBag _viewState;
/// <devdoc>
/// Creates an instance of the Parameter class.
/// </devdoc>
public Parameter() {
}
/// <devdoc>
/// Creates an instance of the Parameter class with the specified parameter name.
/// </devdoc>
public Parameter(string name) {
Name = name;
}
/// <devdoc>
/// Creates an instance of the Parameter class with the specified parameter name and db type.
/// </devdoc>
public Parameter(string name, DbType dbType) {
Name = name;
DbType = dbType;
}
/// <devdoc>
/// Creates an instance of the Parameter class with the specified parameter name, db type, and default value.
/// </devdoc>
public Parameter(string name, DbType dbType, string defaultValue) {
Name = name;
DbType = dbType;
DefaultValue = defaultValue;
}
/// <devdoc>
/// Creates an instance of the Parameter class with the specified parameter name and type.
/// </devdoc>
public Parameter(string name, TypeCode type) {
Name = name;
Type = type;
}
/// <devdoc>
/// Creates an instance of the Parameter class with the specified parameter name, type, and default value.
/// </devdoc>
public Parameter(string name, TypeCode type, string defaultValue) {
Name = name;
Type = type;
DefaultValue = defaultValue;
}
/// <devdoc>
/// Used to clone a parameter.
/// </devdoc>
protected Parameter(Parameter original) {
DefaultValue = original.DefaultValue;
Direction = original.Direction;
Name = original.Name;
ConvertEmptyStringToNull = original.ConvertEmptyStringToNull;
Size = original.Size;
Type = original.Type;
DbType = original.DbType;
}
/// <devdoc>
/// Indicates whether the Parameter is tracking view state.
/// </devdoc>
protected bool IsTrackingViewState {
get {
return _tracking;
}
}
/// <devdoc>
/// Gets/sets the db type of the parameter's value.
/// When DbType is DbType.Object, the Type property will be used instead
/// </devdoc>
[
DefaultValue(DbType.Object),
WebCategory("Parameter"),
WebSysDescription(SR.Parameter_DbType),
]
public DbType DbType {
get {
object o = ViewState["DbType"];
if (o == null)
return DbType.Object;
return (DbType)o;
}
set {
if (value < DbType.AnsiString || value > DbType.DateTimeOffset) {
throw new ArgumentOutOfRangeException("value");
}
if (DbType != value) {
ViewState["DbType"] = value;
OnParameterChanged();
}
}
}
/// <devdoc>
/// The default value to use in GetValue() if it cannot obtain a value.
/// </devdoc>
[
DefaultValue(null),
WebCategory("Parameter"),
WebSysDescription(SR.Parameter_DefaultValue),
]
public string DefaultValue {
get {
object o = ViewState["DefaultValue"];
return (o as string);
}
set {
if (DefaultValue != value) {
ViewState["DefaultValue"] = value;
OnParameterChanged();
}
}
}
/// <devdoc>
/// Gets/sets the direction of the parameter.
/// </devdoc>
[
DefaultValue(ParameterDirection.Input),
WebCategory("Parameter"),
WebSysDescription(SR.Parameter_Direction),
]
public ParameterDirection Direction {
get {
object o = ViewState["Direction"];
if (o == null)
return ParameterDirection.Input;
return (ParameterDirection)o;
}
set {
if (Direction != value) {
ViewState["Direction"] = value;
OnParameterChanged();
}
}
}
/// <devdoc>
/// Gets/sets the name of the parameter.
/// </devdoc>
[
DefaultValue(""),
WebCategory("Parameter"),
WebSysDescription(SR.Parameter_Name),
]
public string Name {
get {
object o = ViewState["Name"];
if (o == null)
return String.Empty;
return (string)o;
}
set {
if (Name != value) {
ViewState["Name"] = value;
OnParameterChanged();
}
}
}
/// <devdoc>
/// Returns the value of parameter after converting it to the proper type.
/// </devdoc>
[
Browsable(false),
]
internal object ParameterValue {
get {
return GetValue(ViewState["ParameterValue"], false);
}
}
public DbType GetDatabaseType() {
DbType dbType = DbType;
if (dbType == DbType.Object) {
return ConvertTypeCodeToDbType(Type);
}
if (Type != TypeCode.Empty) {
throw new InvalidOperationException(SR.GetString(SR.Parameter_TypeNotSupported, Name));
}
return dbType;
}
internal object GetValue(object value, bool ignoreNullableTypeChanges) {
DbType dbType = DbType;
if (dbType == DbType.Object) {
return GetValue(value, DefaultValue, Type, ConvertEmptyStringToNull, ignoreNullableTypeChanges);
}
if (Type != TypeCode.Empty) {
throw new InvalidOperationException(SR.GetString(SR.Parameter_TypeNotSupported, Name));
}
return GetValue(value, DefaultValue, dbType, ConvertEmptyStringToNull, ignoreNullableTypeChanges);
}
internal static object GetValue(object value, string defaultValue, DbType dbType, bool convertEmptyStringToNull,
bool ignoreNullableTypeChanges) {
// use the TypeCode conversion logic for Whidbey types.
if ((dbType != DbType.DateTimeOffset) && (dbType != DbType.Time) && (dbType != DbType.Guid)) {
TypeCode type = ConvertDbTypeToTypeCode(dbType);
return GetValue(value, defaultValue, type, convertEmptyStringToNull, ignoreNullableTypeChanges);
}
value = HandleNullValue(value, defaultValue, convertEmptyStringToNull);
if (value == null) {
return null;
}
// For ObjectDataSource we special-case Nullable<T> and do nothing because these
// types will get converted when we actually call the method.
if (ignoreNullableTypeChanges && IsNullableType(value.GetType())) {
return value;
}
if (dbType == DbType.DateTimeOffset) {
if (value is DateTimeOffset) {
return value;
}
return DateTimeOffset.Parse(value.ToString(), CultureInfo.CurrentCulture);
}
else if (dbType == DbType.Time) {
if (value is TimeSpan) {
return value;
}
return TimeSpan.Parse(value.ToString(), CultureInfo.CurrentCulture);
}
else if (dbType == DbType.Guid) {
if (value is Guid) {
return value;
}
return new Guid(value.ToString());
}
Debug.Fail("Should never reach this point.");
return null;
}
internal static object GetValue(object value, string defaultValue, TypeCode type, bool convertEmptyStringToNull, bool ignoreNullableTypeChanges) {
// Convert.ChangeType() throws if you attempt to convert to DBNull, so we have to special case it.
if (type == TypeCode.DBNull) {
return DBNull.Value;
}
value = HandleNullValue(value, defaultValue, convertEmptyStringToNull);
if (value == null) {
return null;
}
if (type == TypeCode.Object || type == TypeCode.Empty) {
return value;
}
// For ObjectDataSource we special-case Nullable<T> and do nothing because these
// types will get converted when we actually call the method.
if (ignoreNullableTypeChanges && IsNullableType(value.GetType())) {
return value;
}
return value = Convert.ChangeType(value, type, CultureInfo.CurrentCulture);;
}
private static object HandleNullValue(object value, string defaultValue, bool convertEmptyStringToNull) {
// Get the value and convert it to the default value if it is null
if (convertEmptyStringToNull) {
string stringValue = value as string;
if ((stringValue != null) && (stringValue.Length == 0)) {
value = null;
}
}
if (value == null) {
// Attempt to use the default value, but if it is null too, just return null immediately
if (convertEmptyStringToNull && String.IsNullOrEmpty(defaultValue)) {
defaultValue = null;
}
if (defaultValue == null) {
return null;
}
value = defaultValue;
}
return value;
}
private static bool IsNullableType(Type type) {
return type.IsGenericType && (type.GetGenericTypeDefinition() == typeof(Nullable<>));
}
/// <devdoc>
/// Gets/sets the size of the parameter.
/// </devdoc>
[
DefaultValue(0),
WebCategory("Parameter"),
WebSysDescription(SR.Parameter_Size),
]
public int Size {
get {
object o = ViewState["Size"];
if (o == null)
return 0;
return (int)o;
}
set {
if (Size != value) {
ViewState["Size"] = value;
OnParameterChanged();
}
}
}
/// <devdoc>
/// Gets/sets the type of the parameter's value.
/// </devdoc>
[
DefaultValue(TypeCode.Empty),
WebCategory("Parameter"),
WebSysDescription(SR.Parameter_Type),
]
public TypeCode Type {
get {
object o = ViewState["Type"];
if (o == null)
return TypeCode.Empty;
return (TypeCode)o;
}
set {
if (value < TypeCode.Empty || value > TypeCode.String) {
throw new ArgumentOutOfRangeException("value");
}
if (Type != value) {
ViewState["Type"] = value;
OnParameterChanged();
}
}
}
/// <devdoc>
/// Gets/sets whether an empty string should be treated as a null value. If this property is set to true
/// and the value is an empty string, the default value will be used.
/// </devdoc>
[
DefaultValue(true),
WebCategory("Parameter"),
WebSysDescription(SR.Parameter_ConvertEmptyStringToNull),
]
public bool ConvertEmptyStringToNull {
get {
object o = ViewState["ConvertEmptyStringToNull"];
if (o == null)
return true;
return (bool)o;
}
set {
if (ConvertEmptyStringToNull != value) {
ViewState["ConvertEmptyStringToNull"] = value;
OnParameterChanged();
}
}
}
/// <devdoc>
/// Indicates a dictionary of state information that allows you to save and restore
/// the state of a Parameter across multiple requests for the same page.
/// </devdoc>
[
Browsable(false),
DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden),
]
protected StateBag ViewState {
get {
if (_viewState == null) {
_viewState = new StateBag();
if (_tracking)
_viewState.TrackViewState();
}
return _viewState;
}
}
/// <devdoc>
/// Creates a new Parameter that is a copy of this Parameter.
/// </devdoc>
protected virtual Parameter Clone() {
return new Parameter(this);
}
public static TypeCode ConvertDbTypeToTypeCode(DbType dbType) {
switch (dbType) {
case DbType.AnsiString:
case DbType.AnsiStringFixedLength:
case DbType.String:
case DbType.StringFixedLength:
return TypeCode.String;
case DbType.Boolean:
return TypeCode.Boolean;
case DbType.Byte:
return TypeCode.Byte;
case DbType.VarNumeric: // ???
case DbType.Currency:
case DbType.Decimal:
return TypeCode.Decimal;
case DbType.Date:
case DbType.DateTime:
case DbType.DateTime2: // new Katmai type
case DbType.Time: // new Katmai type - no TypeCode for TimeSpan
return TypeCode.DateTime;
case DbType.Double:
return TypeCode.Double;
case DbType.Int16:
return TypeCode.Int16;
case DbType.Int32:
return TypeCode.Int32;
case DbType.Int64:
return TypeCode.Int64;
case DbType.SByte:
return TypeCode.SByte;
case DbType.Single:
return TypeCode.Single;
case DbType.UInt16:
return TypeCode.UInt16;
case DbType.UInt32:
return TypeCode.UInt32;
case DbType.UInt64:
return TypeCode.UInt64;
case DbType.Guid: // ???
case DbType.Binary:
case DbType.Object:
case DbType.DateTimeOffset: // new Katmai type - no TypeCode for DateTimeOffset
default:
return TypeCode.Object;
}
}
public static DbType ConvertTypeCodeToDbType(TypeCode typeCode) {
// no TypeCode equivalent for TimeSpan or DateTimeOffset
switch (typeCode) {
case TypeCode.Boolean:
return DbType.Boolean;
case TypeCode.Byte:
return DbType.Byte;
case TypeCode.Char:
return DbType.StringFixedLength; // ???
case TypeCode.DateTime: // Used for Date, DateTime and DateTime2 DbTypes
return DbType.DateTime;
case TypeCode.Decimal:
return DbType.Decimal;
case TypeCode.Double:
return DbType.Double;
case TypeCode.Int16:
return DbType.Int16;
case TypeCode.Int32:
return DbType.Int32;
case TypeCode.Int64:
return DbType.Int64;
case TypeCode.SByte:
return DbType.SByte;
case TypeCode.Single:
return DbType.Single;
case TypeCode.String:
return DbType.String;
case TypeCode.UInt16:
return DbType.UInt16;
case TypeCode.UInt32:
return DbType.UInt32;
case TypeCode.UInt64:
return DbType.UInt64;
case TypeCode.DBNull:
case TypeCode.Empty:
case TypeCode.Object:
default:
return DbType.Object;
}
}
/// <devdoc>
/// Evaluates the parameter and returns the new value.
/// The control parameter is used to access the page's framework.
/// By default it returns the null, implying that the DefaultValue will
/// be the value.
/// </devdoc>
protected internal virtual object Evaluate(HttpContext context, Control control) {
return null;
}
/// <devdoc>
/// Loads view state.
/// </devdoc>
protected virtual void LoadViewState(object savedState) {
if (savedState != null) {
ViewState.LoadViewState(savedState);
}
}
/// <devdoc>
/// Raises the ParameterChanged event. This notifies a listener that it should re-evaluate the value.
/// </devdoc>
protected void OnParameterChanged() {
if (_owner != null) {
_owner.CallOnParametersChanged();
}
}
/// <devdoc>
/// Saves view state.
/// </devdoc>
protected virtual object SaveViewState() {
return (_viewState != null) ? _viewState.SaveViewState() : null;
}
/// <devdoc>
/// Tells the Parameter to record its entire state into view state.
/// </devdoc>
protected internal virtual void SetDirty() {
ViewState.SetDirty(true);
}
/// <devdoc>
/// Tells the Parameter the collection it belongs to
/// </devdoc>
internal void SetOwner(ParameterCollection owner) {
_owner = owner;
}
/// <devdoc>
/// Converts the Parameter to a string value.
/// </devdoc>
public override string ToString() {
return this.Name;
}
/// <devdoc>
/// Tells the Parameter to start tracking property changes.
/// </devdoc>
protected virtual void TrackViewState() {
_tracking = true;
if (_viewState != null) {
_viewState.TrackViewState();
}
}
/// <devdoc>
/// Updates the value of parameter.
/// If the value changed, this will raise the ParametersChanged event of the ParameterCollection it belongs to.
/// The control parameter is used to access the page's framework.
/// </devdoc>
internal void UpdateValue(HttpContext context, Control control) {
object oldValue = ViewState["ParameterValue"];
object newValue = Evaluate(context, control);
ViewState["ParameterValue"] = newValue;
// If you have chains of dependency, like one control with a control parameter on another, and then a third with a control
// parameter on the second, the order in which the evaluations take place is non-deterministic and may create incorrect
// evaluation of parameters because all our evaluation happens during LoadComplete. The correct solution is to call DataBind
// on the third control when the second control's selected value changes. Hacky, but we don't support specifying dependency
// chains on data sources.
if ((newValue == null && oldValue != null) || (newValue != null && !newValue.Equals(oldValue))) {
OnParameterChanged();
}
}
#region Implementation of ICloneable
/// <internalonly/>
object ICloneable.Clone() {
return Clone();
}
#endregion
#region Implementation of IStateManager
/// <internalonly/>
bool IStateManager.IsTrackingViewState {
get {
return IsTrackingViewState;
}
}
/// <internalonly/>
void IStateManager.LoadViewState(object savedState) {
LoadViewState(savedState);
}
/// <internalonly/>
object IStateManager.SaveViewState() {
return SaveViewState();
}
/// <internalonly/>
void IStateManager.TrackViewState() {
TrackViewState();
}
#endregion
}
}
|