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