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.
31 using System.Collections;
32 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 public ControlParameter (string name, DbType dbType, string controlID, string propertyName) : base (name, dbType)
72 ControlID = controlID;
73 PropertyName = propertyName;
76 protected override Parameter Clone ()
78 return new ControlParameter (this);
81 override object Evaluate (HttpContext ctx, Control control)
85 if (control.Page == null)
88 if(String.IsNullOrEmpty(ControlID))
89 throw new ArgumentException ("The ControlID property is not set.");
91 Control c = null, namingContainer = control.NamingContainer;
93 while (namingContainer != null) {
94 c = namingContainer.FindControl(ControlID);
97 namingContainer = namingContainer.NamingContainer;
100 throw new InvalidOperationException ("Control '" + ControlID + "' not found.");
102 string propName = PropertyName;
103 if (String.IsNullOrEmpty (propName)) {
104 object [] attrs = c.GetType ().GetCustomAttributes (typeof (ControlValuePropertyAttribute), true);
106 throw new ArgumentException ("The PropertyName property is not set and the Control identified by the ControlID property is not decorated with a ControlValuePropertyAttribute attribute.");
107 ControlValuePropertyAttribute attr = (ControlValuePropertyAttribute) attrs [0];
108 propName = attr.Name;
111 return DataBinder.Eval (c, propName);
114 [WebCategoryAttribute ("Control")]
115 [RefreshPropertiesAttribute (RefreshProperties.All)]
116 [TypeConverterAttribute (typeof (ControlIDConverter))]
117 [DefaultValueAttribute ("")]
118 [IDReferencePropertyAttribute (typeof(System.Web.UI.Control))]
119 public string ControlID {
120 get { return ViewState.GetString ("ControlID", String.Empty); }
122 if (ControlID != value) {
123 ViewState ["ControlID"] = value;
124 OnParameterChanged ();
129 [DefaultValueAttribute ("")]
130 [TypeConverterAttribute (typeof (ControlPropertyNameConverter))]
131 [WebCategoryAttribute ("Control")]
132 public string PropertyName {
133 get { return ViewState.GetString ("PropertyName", String.Empty); }
136 if (PropertyName != value) {
137 ViewState ["PropertyName"] = value;
138 OnParameterChanged ();