2008-03-13 Marek Habersack <mhabersack@novell.com>
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / HiddenField.cs
1 //
2 // System.Web.UI.WebControls.HiddenField.cs
3 //
4 // Authors:
5 //  Lluis Sanchez Gual (lluis@novell.com)
6 //
7 // (C) 2005 Novell, Inc. (http://www.novell.com)
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.Web.UI;
33 using System.ComponentModel;
34
35 namespace System.Web.UI.WebControls
36 {
37         public class HiddenField : Control, IPostBackDataHandler
38         {
39
40                 static readonly object ValueChangedEvent = new object ();
41
42                 public event EventHandler ValueChanged
43                 {
44                         add { Events.AddHandler (ValueChangedEvent, value); }
45                         remove { Events.RemoveHandler (ValueChangedEvent, value); }
46                 }
47
48                 [Bindable (true)]
49                 public virtual string Value {
50                         get {
51                                 return ViewState.GetString ("Value", "");
52                         }
53                         set {
54                                 ViewState ["Value"] = value;
55                         }
56                 }
57
58                 public override bool EnableTheming {
59                         get {
60                                 return false;
61                         }
62                         set {
63                                 throw new NotSupportedException ();
64                         }
65                 }
66
67                 public override string SkinID {
68                         get {
69                                 return String.Empty;
70                         }
71                         set {
72                                 throw new NotSupportedException ();
73                         }
74                 }
75
76                 public override void Focus ()
77                 {
78                         throw new NotSupportedException ();
79                 }
80
81                 protected virtual void OnValueChanged (EventArgs e)
82                 {
83                         EventHandler h = (EventHandler) Events [ValueChangedEvent];
84                         if (h != null)
85                                 h (this, e);
86                 }
87
88                 protected virtual bool LoadPostData (string postDataKey, System.Collections.Specialized.NameValueCollection postCollection)
89                 {
90                         if (Value != postCollection [postDataKey]) {
91                                 Value = postCollection [postDataKey];
92                                 return true;
93                         }
94                         return false;
95                 }
96
97                 protected virtual void RaisePostDataChangedEvent ()
98                 {
99                         OnValueChanged (EventArgs.Empty);
100                 }
101
102                 protected override ControlCollection CreateControlCollection ()
103                 {
104                         return new System.Web.UI.EmptyControlCollection (this);
105                 }
106
107                 protected internal override void OnPreRender (EventArgs e)
108                 {
109                         base.OnPreRender (e);
110                 }
111
112                 protected internal override void Render (HtmlTextWriter writer)
113                 {
114                         writer.AddAttribute (HtmlTextWriterAttribute.Type, "hidden", false);
115
116                         if (!String.IsNullOrEmpty (ClientID))
117                                 writer.AddAttribute (HtmlTextWriterAttribute.Id, ClientID);
118
119                         if (!String.IsNullOrEmpty (UniqueID))
120                                 writer.AddAttribute (HtmlTextWriterAttribute.Name, UniqueID);
121
122                         if (!String.IsNullOrEmpty (Value))
123                                 writer.AddAttribute (HtmlTextWriterAttribute.Value, Value);
124
125                         writer.RenderBeginTag (HtmlTextWriterTag.Input);
126                         writer.RenderEndTag (); 
127                 }
128
129                 #region IPostBackDataHandler Members
130
131                 bool IPostBackDataHandler.LoadPostData (string postDataKey, System.Collections.Specialized.NameValueCollection postCollection)
132                 {
133                         return LoadPostData (postDataKey, postCollection);
134                 }
135
136                 void IPostBackDataHandler.RaisePostDataChangedEvent ()
137                 {
138                         RaisePostDataChangedEvent ();
139                 }
140
141                 #endregion
142         }
143 }
144 #endif