Merge pull request #2916 from ludovic-henry/fix-40306
[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 using System.Collections;
32 using System.Collections.Specialized;
33 using System.Data;
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                 public ControlParameter (string name, DbType dbType, string controlID, string propertyName) : base (name, dbType)
71                 {
72                         ControlID = controlID;
73                         PropertyName = propertyName;
74                 }
75                 
76                 protected override Parameter Clone ()
77                 {
78                         return new ControlParameter (this);
79                 }
80                 protected internal
81                 override object Evaluate (HttpContext ctx, Control control)
82                 {
83                         if (control == null)
84                                 return null;
85                         if (control.Page == null)
86                                 return null;
87                         
88                         if(String.IsNullOrEmpty(ControlID))
89                                 throw new ArgumentException ("The ControlID property is not set.");
90
91                         Control c = null, namingContainer = control.NamingContainer;
92                         
93                         while (namingContainer != null) {
94                                 c = namingContainer.FindControl(ControlID);
95                                 if (c != null)
96                                         break;
97                                 namingContainer = namingContainer.NamingContainer;
98                         }
99                         if (c == null)
100                                 throw new InvalidOperationException ("Control '" + ControlID + "' not found.");
101
102                         string propName = PropertyName;
103                         if (String.IsNullOrEmpty (propName)) {
104                                 object [] attrs = c.GetType ().GetCustomAttributes (typeof (ControlValuePropertyAttribute), true);
105                                 if(attrs.Length==0)
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;
109                         }
110                         
111                         return DataBinder.Eval (c, propName);
112                 }
113                 
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); }
121                         set {
122                                 if (ControlID != value) {
123                                         ViewState ["ControlID"] = value;
124                                         OnParameterChanged ();
125                                 }
126                         }
127                 }
128                 
129                 [DefaultValueAttribute ("")]
130                 [TypeConverterAttribute (typeof (ControlPropertyNameConverter))]
131                 [WebCategoryAttribute ("Control")]
132                 public string PropertyName {
133                         get { return ViewState.GetString ("PropertyName", String.Empty); }
134                         set {
135                                 
136                                 if (PropertyName != value) {
137                                         ViewState ["PropertyName"] = value;
138                                         OnParameterChanged ();
139                                 }
140                         }
141                 }
142         }
143 }
144
145