2 // System.Web.UI.WebControls.ControlParameter
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.Collections.Specialized;
35 using System.ComponentModel;
36 using System.Reflection;
38 namespace System.Web.UI.WebControls {
40 [DefaultPropertyAttribute ("ControlID")]
41 public class ControlParameter : Parameter {
43 public ControlParameter () : base ()
47 protected ControlParameter (ControlParameter original) : base (original)
49 this.ControlID = original.ControlID;
50 this.PropertyName = original.PropertyName;
53 public ControlParameter (string name, string controlID) : base (name)
55 ControlID = controlID;
58 public ControlParameter (string name, string controlID, string propertyName) : base (name)
60 ControlID = controlID;
61 PropertyName = propertyName;
64 public ControlParameter (string name, TypeCode type, string controlID, string propertyName) : base (name, type)
66 ControlID = controlID;
67 PropertyName = propertyName;
70 protected override Parameter Clone ()
72 return new ControlParameter (this);
75 protected override object Evaluate (HttpContext ctx, Control control)
77 if (control == null) return null;
78 if (control.Page == null) return null;
80 if(String.IsNullOrEmpty(ControlID))
81 throw new ArgumentException ("The ControlID property is not set.");
83 Control c = null, namingContainer = control.NamingContainer;
85 while (namingContainer != null) {
86 c = namingContainer.FindControl(ControlID);
89 namingContainer = namingContainer.NamingContainer;
92 throw new InvalidOperationException ("Control '" + ControlID + "' not found.");
94 string propName = PropertyName;
95 if (String.IsNullOrEmpty (propName)) {
96 object [] attrs = c.GetType ().GetCustomAttributes (typeof (ControlValuePropertyAttribute), true);
98 throw new ArgumentException ("The PropertyName property is not set and the Control identified by the ControlID property is not decorated with a ControlValuePropertyAttribute attribute.");
99 ControlValuePropertyAttribute attr = (ControlValuePropertyAttribute) attrs [0];
100 propName = attr.Name;
102 PropertyInfo prop = c.GetType ().GetProperty (propName);
104 throw new InvalidOperationException ("Property '" + propName + "' not found in type '" + c.GetType () + "'.");
106 return prop.GetValue (c, null);
109 [WebCategoryAttribute ("Control")]
110 [RefreshPropertiesAttribute (RefreshProperties.All)]
111 [TypeConverterAttribute (typeof (ControlIDConverter))]
112 [DefaultValueAttribute ("")]
113 [IDReferencePropertyAttribute (typeof(System.Web.UI.Control))]
114 public string ControlID {
115 get { return ViewState.GetString ("ControlID", ""); }
117 if (ControlID != value) {
118 ViewState ["ControlID"] = value;
119 OnParameterChanged ();
124 [DefaultValueAttribute ("")]
125 [TypeConverterAttribute (typeof (ControlPropertyNameConverter))]
126 [WebCategoryAttribute ("Control")]
127 public string PropertyName {
128 get { return ViewState.GetString ("PropertyName", ""); }
131 if (PropertyName != value) {
132 ViewState ["PropertyName"] = value;
133 OnParameterChanged ();