2008-03-13 Marek Habersack <mhabersack@novell.com>
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / ControlParameter.cs
1 //
2 // System.Web.UI.WebControls.ControlParameter
3 //
4 // Authors:
5 //      Ben Maurer (bmaurer@users.sourceforge.net)
6 //
7 // (C) 2003 Ben Maurer
8 //
9
10 //
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:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
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.
29 //
30
31 #if NET_2_0
32 using System.Collections;
33 using System.Collections.Specialized;
34 using System.Text;
35 using System.ComponentModel;
36 using System.Reflection;
37
38 namespace System.Web.UI.WebControls {
39
40         [DefaultPropertyAttribute ("ControlID")]
41         public class ControlParameter : Parameter {
42
43                 public ControlParameter () : base ()
44                 {
45                 }
46
47                 protected ControlParameter (ControlParameter original) : base (original)
48                 {
49                         this.ControlID = original.ControlID;
50                         this.PropertyName = original.PropertyName;
51                 }
52                 
53                 public ControlParameter (string name, string controlID) : base (name)
54                 {
55                         ControlID = controlID;
56                 }
57                 
58                 public ControlParameter (string name, string controlID, string propertyName) : base (name)
59                 {
60                         ControlID = controlID;
61                         PropertyName = propertyName;
62                 }
63                 
64                 public ControlParameter (string name, TypeCode type, string controlID, string propertyName) : base (name, type)
65                 {
66                         ControlID = controlID;
67                         PropertyName = propertyName;
68                 }
69                 
70                 protected override Parameter Clone ()
71                 {
72                         return new ControlParameter (this);
73                 }
74                 
75                 protected override object Evaluate (HttpContext ctx, Control control)
76                 {
77                         if (control == null) return null;
78                         if (control.Page == null) return null;
79                         
80                         if(String.IsNullOrEmpty(ControlID))
81                                 throw new ArgumentException ("The ControlID property is not set.");
82
83                         Control c = null, namingContainer = control.NamingContainer;
84                         
85                         while (namingContainer != null) {
86                                 c = namingContainer.FindControl(ControlID);
87                                 if (c != null)
88                                         break;
89                                 namingContainer = namingContainer.NamingContainer;
90                         }
91                         if (c == null)
92                                 throw new InvalidOperationException ("Control '" + ControlID + "' not found.");
93
94                         string propName = PropertyName;
95                         if (String.IsNullOrEmpty (propName)) {
96                                 object [] attrs = c.GetType ().GetCustomAttributes (typeof (ControlValuePropertyAttribute), true);
97                                 if(attrs.Length==0)
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;
101                         }
102                         PropertyInfo prop = c.GetType ().GetProperty (propName);
103                         if (prop == null)
104                                 throw new InvalidOperationException ("Property '" + propName + "' not found in type '" + c.GetType () + "'.");
105                         
106                         return prop.GetValue (c, null);
107                 }
108                 
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", ""); }
116                         set {
117                                 if (ControlID != value) {
118                                         ViewState ["ControlID"] = value;
119                                         OnParameterChanged ();
120                                 }
121                         }
122                 }
123                 
124                 [DefaultValueAttribute ("")]
125                 [TypeConverterAttribute (typeof (ControlPropertyNameConverter))]
126                 [WebCategoryAttribute ("Control")]
127                 public string PropertyName {
128                         get { return ViewState.GetString ("PropertyName", ""); }
129                         set {
130                                 
131                                 if (PropertyName != value) {
132                                         ViewState ["PropertyName"] = value;
133                                         OnParameterChanged ();
134                                 }
135                         }
136                 }
137         }
138 }
139 #endif
140