2 // System.Web.UI.WebControls.HiddenField.cs
5 // Lluis Sanchez Gual (lluis@novell.com)
7 // (C) 2005-2010 Novell, Inc. (http://www.novell.com)
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.
32 using System.ComponentModel;
34 namespace System.Web.UI.WebControls
36 [DefaultEvent ("ValueChanged")]
37 [DefaultProperty ("Value")]
38 [Designer ("System.Web.UI.Design.WebControls.HiddenFieldDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
39 [ControlValueProperty ("Value")]
42 [PersistChildren (false)]
43 [SupportsEventValidation]
44 public class HiddenField : Control, IPostBackDataHandler
46 static readonly object ValueChangedEvent = new object ();
48 public event EventHandler ValueChanged {
49 add { Events.AddHandler (ValueChangedEvent, value); }
50 remove { Events.RemoveHandler (ValueChangedEvent, value); }
55 public virtual string Value {
56 get { return ViewState.GetString ("Value", String.Empty); }
57 set { ViewState ["Value"] = value; }
60 [DefaultValue (false)]
61 [EditorBrowsable (EditorBrowsableState.Never)]
62 public override bool EnableTheming {
64 set { throw new NotSupportedException (); }
68 [EditorBrowsable (EditorBrowsableState.Never)]
69 public override string SkinID {
70 get { return String.Empty; }
71 set { throw new NotSupportedException (); }
74 [EditorBrowsable (EditorBrowsableState.Never)]
75 public override void Focus ()
77 throw new NotSupportedException ();
80 protected virtual void OnValueChanged (EventArgs e)
82 EventHandler h = (EventHandler) Events [ValueChangedEvent];
87 protected virtual bool LoadPostData (string postDataKey, System.Collections.Specialized.NameValueCollection postCollection)
89 if (Value != postCollection [postDataKey]) {
90 Value = postCollection [postDataKey];
96 protected virtual void RaisePostDataChangedEvent ()
98 ValidateEvent (UniqueID, String.Empty);
99 OnValueChanged (EventArgs.Empty);
102 protected override ControlCollection CreateControlCollection ()
104 return new System.Web.UI.EmptyControlCollection (this);
107 protected internal override void OnPreRender (EventArgs e)
109 base.OnPreRender (e);
112 protected internal override void Render (HtmlTextWriter writer)
115 string uniqueid = UniqueID;
117 page.ClientScript.RegisterForEventValidation (uniqueid);
119 writer.AddAttribute (HtmlTextWriterAttribute.Type, "hidden", false);
121 if (!String.IsNullOrEmpty (ClientID))
122 writer.AddAttribute (HtmlTextWriterAttribute.Id, ClientID);
124 if (!String.IsNullOrEmpty (uniqueid))
125 writer.AddAttribute (HtmlTextWriterAttribute.Name, uniqueid);
127 if (!String.IsNullOrEmpty (Value))
128 writer.AddAttribute (HtmlTextWriterAttribute.Value, Value);
130 writer.RenderBeginTag (HtmlTextWriterTag.Input);
131 writer.RenderEndTag ();
134 #region IPostBackDataHandler Members
136 bool IPostBackDataHandler.LoadPostData (string postDataKey, System.Collections.Specialized.NameValueCollection postCollection)
138 return LoadPostData (postDataKey, postCollection);
141 void IPostBackDataHandler.RaisePostDataChangedEvent ()
143 RaisePostDataChangedEvent ();