1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
|
//------------------------------------------------------------------------------
// <copyright file="HiddenField.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
//------------------------------------------------------------------------------
namespace System.Web.UI.WebControls {
using System.ComponentModel;
using System.Collections.Specialized;
/// <devdoc>
/// Inserts a hidden field into the web form.
/// </devdoc>
[
ControlValueProperty("Value"),
DefaultEvent("ValueChanged"),
DefaultProperty("Value"),
Designer("System.Web.UI.Design.WebControls.HiddenFieldDesigner, " + AssemblyRef.SystemDesign),
ParseChildren(true),
PersistChildren(false),
NonVisualControl(),
SupportsEventValidation,
]
public class HiddenField : Control, IPostBackDataHandler {
private static readonly object EventValueChanged = new object();
[
DefaultValue(false),
EditorBrowsable(EditorBrowsableState.Never),
]
public override bool EnableTheming {
get {
return false;
}
set {
throw new NotSupportedException(SR.GetString(SR.NoThemingSupport, this.GetType().Name));
}
}
[
DefaultValue(""),
EditorBrowsable(EditorBrowsableState.Never),
]
public override string SkinID {
get {
return String.Empty;
}
set {
throw new NotSupportedException(SR.GetString(SR.NoThemingSupport, this.GetType().Name));
}
}
/// <devdoc>
/// Gets or sets the value of the hidden field.
/// </devdoc>
[
Bindable(true),
WebCategory("Behavior"),
DefaultValue(""),
WebSysDescription(SR.HiddenField_Value)
]
public virtual string Value {
get {
string s = (string)ViewState["Value"];
return (s != null) ? s : String.Empty;
}
set {
ViewState["Value"] = value;
}
}
/// <devdoc>
/// Raised when the value of the hidden field is changed on the client.
/// </devdoc>
[
WebCategory("Action"),
WebSysDescription(SR.HiddenField_OnValueChanged)
]
public event EventHandler ValueChanged {
add {
Events.AddHandler(EventValueChanged, value);
}
remove {
Events.RemoveHandler(EventValueChanged, value);
}
}
protected override ControlCollection CreateControlCollection() {
return new EmptyControlCollection(this);
}
[
EditorBrowsable(EditorBrowsableState.Never),
]
public override void Focus() {
throw new NotSupportedException(SR.GetString(SR.NoFocusSupport, this.GetType().Name));
}
protected virtual bool LoadPostData(string postDataKey, NameValueCollection postCollection) {
ValidateEvent(UniqueID);
string current = Value;
string postData = postCollection[postDataKey];
if (!current.Equals(postData, StringComparison.Ordinal)) {
Value = postData;
return true;
}
return false;
}
protected internal override void OnPreRender(EventArgs e) {
base.OnPreRender(e);
if (SaveValueViewState == false) {
ViewState.SetItemDirty("Value", false);
}
}
protected virtual void OnValueChanged(EventArgs e) {
EventHandler handler = (EventHandler)Events[EventValueChanged];
if (handler != null) {
handler(this, e);
}
}
protected virtual void RaisePostDataChangedEvent() {
OnValueChanged(EventArgs.Empty);
}
protected internal override void Render(HtmlTextWriter writer) {
string uniqueID = UniqueID;
// Make sure we are in a form tag with runat=server.
if (Page != null) {
Page.VerifyRenderingInServerForm(this);
Page.ClientScript.RegisterForEventValidation(uniqueID);
}
writer.AddAttribute(HtmlTextWriterAttribute.Type, "hidden");
if (uniqueID != null) {
writer.AddAttribute(HtmlTextWriterAttribute.Name, uniqueID);
}
if (ID != null) {
writer.AddAttribute(HtmlTextWriterAttribute.Id, ClientID);
}
string s;
s = Value;
if (s.Length > 0) {
writer.AddAttribute(HtmlTextWriterAttribute.Value, s);
}
writer.RenderBeginTag(HtmlTextWriterTag.Input);
writer.RenderEndTag();
}
/// <devdoc>
/// Determines whether the Value must be stored in view state, to
/// optimize the size of the saved state.
/// </devdoc>
private bool SaveValueViewState {
get {
// Must be saved when
// 1. There is a registered event handler for EventValueChanged
// 2. Control is not visible, because the browser's post data will not include this control
// 3. The instance is a derived instance, which might be overriding the OnValueChanged method
if ((Events[EventValueChanged] != null) ||
(Visible == false) ||
(this.GetType() != typeof(HiddenField))) {
return true;
}
return false;
}
}
#region Implementation of IPostBackDataHandler
bool IPostBackDataHandler.LoadPostData(string postDataKey, NameValueCollection postCollection) {
return LoadPostData(postDataKey, postCollection);
}
void IPostBackDataHandler.RaisePostDataChangedEvent() {
RaisePostDataChangedEvent();
}
#endregion
}
}
|