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
|
//------------------------------------------------------------------------------
// <copyright file="ControlParameter.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
//------------------------------------------------------------------------------
namespace System.Web.UI.WebControls {
using System;
using System.ComponentModel;
using System.Data;
using System.Web.UI.WebControls;
/// <devdoc>
/// Represents a Parameter that gets its value from a control's property.
/// </devdoc>
[
DefaultProperty("ControlID"),
]
public class ControlParameter : Parameter {
/// <devdoc>
/// Creates an instance of the ControlParameter class.
/// </devdoc>
public ControlParameter() {
}
/// <devdoc>
/// Creates an instance of the ControlParameter class with the specified parameter name and control ID.
/// </devdoc>
public ControlParameter(string name, string controlID) : base(name) {
ControlID = controlID;
}
/// <devdoc>
/// Creates an instance of the ControlParameter class with the specified parameter name, control ID, and property name.
/// </devdoc>
public ControlParameter(string name, string controlID, string propertyName) : base(name) {
ControlID = controlID;
PropertyName = propertyName;
}
/// <devdoc>
/// Creates an instance of the ControlParameter class with the specified parameter name, database type,
/// control ID, and property name.
/// </devdoc>
public ControlParameter(string name, DbType dbType, string controlID, string propertyName)
: base(name, dbType) {
ControlID = controlID;
PropertyName = propertyName;
}
/// <devdoc>
/// Creates an instance of the ControlParameter class with the specified parameter name, type, control ID, and property name.
/// </devdoc>
public ControlParameter(string name, TypeCode type, string controlID, string propertyName) : base(name, type) {
ControlID = controlID;
PropertyName = propertyName;
}
/// <devdoc>
/// Used to clone a parameter.
/// </devdoc>
protected ControlParameter(ControlParameter original) : base(original) {
ControlID = original.ControlID;
PropertyName = original.PropertyName;
}
/// <devdoc>
/// The ID of the control to get the value from.
/// </devdoc>
[
DefaultValue(""),
IDReferenceProperty(),
RefreshProperties(RefreshProperties.All),
TypeConverter(typeof(ControlIDConverter)),
WebCategory("Control"),
WebSysDescription(SR.ControlParameter_ControlID),
]
public string ControlID {
get {
object o = ViewState["ControlID"];
if (o == null)
return String.Empty;
return (string)o;
}
set {
if (ControlID != value) {
ViewState["ControlID"] = value;
OnParameterChanged();
}
}
}
/// <devdoc>
/// The name of the control's property to get the value from.
/// If none is specified, the ControlValueProperty attribute of the control will be examined to determine the default property name.
/// </devdoc>
[
DefaultValue(""),
TypeConverter(typeof(ControlPropertyNameConverter)),
WebCategory("Control"),
WebSysDescription(SR.ControlParameter_PropertyName),
]
public string PropertyName {
get {
object o = ViewState["PropertyName"];
if (o == null)
return String.Empty;
return (string)o;
}
set {
if (PropertyName != value) {
ViewState["PropertyName"] = value;
OnParameterChanged();
}
}
}
/// <devdoc>
/// Creates a new ControlParameter that is a copy of this ControlParameter.
/// </devdoc>
protected override Parameter Clone() {
return new ControlParameter(this);
}
/// <devdoc>
/// Returns the updated value of the parameter.
/// </devdoc>
protected internal override object Evaluate(HttpContext context, Control control) {
if (control == null) {
return null;
}
string controlID = ControlID;
string propertyName = PropertyName;
if (controlID.Length == 0) {
throw new ArgumentException(SR.GetString(SR.ControlParameter_ControlIDNotSpecified, Name));
}
Control foundControl = DataBoundControlHelper.FindControl(control, controlID);
if (foundControl == null) {
throw new InvalidOperationException(SR.GetString(SR.ControlParameter_CouldNotFindControl, controlID, Name));
}
ControlValuePropertyAttribute controlValueProp = (ControlValuePropertyAttribute)TypeDescriptor.GetAttributes(foundControl)[typeof(ControlValuePropertyAttribute)];
// If no property name is specified, use the ControlValuePropertyAttribute to determine which property to use.
if (propertyName.Length == 0) {
if ((controlValueProp != null) && (!String.IsNullOrEmpty(controlValueProp.Name))) {
propertyName = controlValueProp.Name;
}
else {
throw new InvalidOperationException(SR.GetString(SR.ControlParameter_PropertyNameNotSpecified, controlID, Name));
}
}
// Get the value of the property
object value = DataBinder.Eval(foundControl, propertyName);
// Convert the value to null if this is the default property and the value is the property's default value
if (controlValueProp != null &&
String.Equals(controlValueProp.Name, propertyName, StringComparison.OrdinalIgnoreCase) &&
controlValueProp.DefaultValue != null &&
controlValueProp.DefaultValue.Equals(value)) {
return null;
}
return value;
}
}
}
|