2 // System.Web.UI.Page.cs
5 // Duncan Mak (duncan@ximian.com)
6 // Gonzalo Paniagua (gonzalo@ximian.com)
7 // Andreas Nahr (ClassDevelopment@A-SoftTech.com)
9 // (C) 2002,2003 Ximian, Inc. (http://www.ximian.com)
10 // (c) 2003 Novell, Inc. (http://www.novell.com)
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35 using System.Collections;
36 using System.Collections.Specialized;
37 using System.ComponentModel;
38 using System.ComponentModel.Design;
39 using System.ComponentModel.Design.Serialization;
40 using System.Globalization;
42 using System.Security.Principal;
44 using System.Threading;
46 using System.Web.Caching;
47 using System.Web.SessionState;
48 using System.Web.Util;
50 namespace System.Web.UI
53 [DefaultEvent ("Load"), DesignerCategory ("ASPXCodeBehind")]
55 [Designer ("System.Web.UI.Design.ControlDesigner, " + Consts.AssemblySystem_Design, typeof (IDesigner))]
56 [RootDesignerSerializer ("Microsoft.VSDesigner.WebForms.RootCodeDomSerializer, " + Consts.AssemblyMicrosoft_VSDesigner, "System.ComponentModel.Design.Serialization.CodeDomSerializer, " + Consts.AssemblySystem_Design, true)]
57 public class Page : TemplateControl, IHttpHandler
59 private bool _viewState = true;
60 private bool _viewStateMac;
61 private string _errorPage;
62 private bool _isValid;
63 private bool _smartNavigation;
64 private int _transactionMode;
65 private HttpContext _context;
66 private ValidatorCollection _validators;
67 private bool renderingForm;
68 private object _savedViewState;
69 private ArrayList _requiresPostBack;
70 private ArrayList _requiresPostBackCopy;
71 private ArrayList requiresPostDataChanged;
72 private IPostBackEventHandler requiresRaiseEvent;
73 private NameValueCollection secondPostData;
74 private bool requiresPostBackScript;
75 private bool postBackScriptRendered;
77 string viewStateUserKey;
78 NameValueCollection _requestValueCollection;
80 ClientScriptManager scriptManager = new ClientScriptManager ();
82 [EditorBrowsable (EditorBrowsableState.Never)]
83 protected const string postEventArgumentID = "__EVENTARGUMENT";
84 [EditorBrowsable (EditorBrowsableState.Never)]
85 protected const string postEventSourceID = "__EVENTTARGET";
88 private const string callbackArgumentID = "__CALLBACKARGUMENT";
89 private const string callbackSourceID = "__CALLBACKTARGET";
102 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
104 public HttpApplicationState Application
106 get { return _context.Application; }
109 [EditorBrowsable (EditorBrowsableState.Never)]
110 protected bool AspCompatMode
112 set { throw new NotImplementedException (); }
115 [EditorBrowsable (EditorBrowsableState.Never)]
116 protected bool Buffer
118 set { Response.BufferOutput = value; }
121 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
125 get { return _context.Cache; }
128 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
129 [Browsable (false), DefaultValue ("")]
130 [WebSysDescription ("Value do override the automatic browser detection and force the page to use the specified browser.")]
131 public string ClientTarget
133 get { return (clientTarget == null) ? "" : clientTarget; }
135 clientTarget = value;
141 [EditorBrowsable (EditorBrowsableState.Never)]
142 protected int CodePage
144 set { Response.ContentEncoding = Encoding.GetEncoding (value); }
147 [EditorBrowsable (EditorBrowsableState.Never)]
148 protected string ContentType
150 set { Response.ContentType = value; }
153 protected override HttpContext Context
156 if (_context == null)
157 return HttpContext.Current;
163 [EditorBrowsable (EditorBrowsableState.Never)]
164 protected string Culture
166 set { Thread.CurrentThread.CurrentCulture = new CultureInfo (value); }
170 public override bool EnableViewState
172 get { return _viewState; }
173 set { _viewState = value; }
176 [EditorBrowsable (EditorBrowsableState.Never)]
177 protected bool EnableViewStateMac
179 get { return _viewStateMac; }
180 set { _viewStateMac = value; }
183 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
184 [Browsable (false), DefaultValue ("")]
185 [WebSysDescription ("The URL of a page used for error redirection.")]
186 public string ErrorPage
188 get { return _errorPage; }
191 if (_context != null)
192 _context.ErrorPage = value;
196 [EditorBrowsable (EditorBrowsableState.Never)]
197 protected ArrayList FileDependencies
200 if (Response != null)
201 Response.AddFileDependencies (value);
206 public override string ID
208 get { return base.ID; }
209 set { base.ID = value; }
212 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
214 public bool IsPostBack
217 return _requestValueCollection != null;
221 [EditorBrowsable (EditorBrowsableState.Never), Browsable (false)]
222 public bool IsReusable {
223 get { return false; }
226 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
230 get { return _isValid; }
233 [EditorBrowsable (EditorBrowsableState.Never)]
235 set { Thread.CurrentThread.CurrentCulture = new CultureInfo (value); }
238 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
240 public HttpRequest Request
242 get { return _context.Request; }
245 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
247 public HttpResponse Response
249 get { return _context.Response; }
252 [EditorBrowsable (EditorBrowsableState.Never)]
253 protected string ResponseEncoding
255 set { Response.ContentEncoding = Encoding.GetEncoding (value); }
258 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
260 public HttpServerUtility Server
263 return Context.Server;
267 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
269 public virtual HttpSessionState Session
272 if (_context.Session == null)
273 throw new HttpException ("Session state can only be used " +
274 "when enableSessionState is set to true, either " +
275 "in a configuration file or in the Page directive.");
277 return _context.Session;
282 public bool SmartNavigation
284 get { return _smartNavigation; }
285 set { _smartNavigation = value; }
288 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
290 public TraceContext Trace
292 get { return Context.Trace; }
295 [EditorBrowsable (EditorBrowsableState.Never)]
296 protected bool TraceEnabled
298 set { Trace.IsEnabled = value; }
301 [EditorBrowsable (EditorBrowsableState.Never)]
302 protected TraceMode TraceModeValue
304 set { Trace.TraceMode = value; }
307 [EditorBrowsable (EditorBrowsableState.Never)]
308 protected int TransactionMode
310 set { _transactionMode = value; }
313 [EditorBrowsable (EditorBrowsableState.Never)]
314 protected string UICulture
316 set { Thread.CurrentThread.CurrentUICulture = new CultureInfo (value); }
319 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
321 public IPrincipal User
323 get { return _context.User; }
326 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
328 public ValidatorCollection Validators
331 if (_validators == null)
332 _validators = new ValidatorCollection ();
337 [MonoTODO ("Use this when encrypting/decrypting ViewState")]
339 public string ViewStateUserKey {
340 get { return viewStateUserKey; }
341 set { viewStateUserKey = value; }
345 public override bool Visible
347 get { return base.Visible; }
348 set { base.Visible = value; }
355 [EditorBrowsable (EditorBrowsableState.Never)]
356 protected IAsyncResult AspCompatBeginProcessRequest (HttpContext context,
360 throw new NotImplementedException ();
363 [EditorBrowsable (EditorBrowsableState.Never)]
364 protected void AspCompatEndProcessRequest (IAsyncResult result)
366 throw new NotImplementedException ();
369 [EditorBrowsable (EditorBrowsableState.Advanced)]
370 protected virtual HtmlTextWriter CreateHtmlTextWriter (TextWriter tw)
372 return new HtmlTextWriter (tw);
375 [EditorBrowsable (EditorBrowsableState.Never)]
376 public void DesignerInitialize ()
378 InitRecursive (null);
381 [EditorBrowsable (EditorBrowsableState.Advanced)]
382 protected virtual NameValueCollection DeterminePostBackMode ()
384 if (_context == null)
387 HttpRequest req = _context.Request;
391 NameValueCollection coll = null;
392 if (0 == String.Compare (Request.HttpMethod, "POST", true))
395 coll = req.QueryString;
398 if (coll == null || coll ["__VIEWSTATE"] == null)
404 [EditorBrowsable (EditorBrowsableState.Advanced)]
405 public string GetPostBackClientEvent (Control control, string argument)
407 return GetPostBackEventReference (control, argument);
410 [EditorBrowsable (EditorBrowsableState.Advanced)]
411 public string GetPostBackClientHyperlink (Control control, string argument)
413 return "javascript:" + GetPostBackEventReference (control, argument);
416 [EditorBrowsable (EditorBrowsableState.Advanced)]
417 public string GetPostBackEventReference (Control control)
419 return GetPostBackEventReference (control, "");
422 [EditorBrowsable (EditorBrowsableState.Advanced)]
423 public string GetPostBackEventReference (Control control, string argument)
425 RequiresPostBackScript ();
426 return String.Format ("__doPostBack('{0}','{1}')", control.UniqueID, argument);
429 internal void RequiresPostBackScript ()
431 requiresPostBackScript = true;
434 [EditorBrowsable (EditorBrowsableState.Never)]
435 public virtual int GetTypeHashCode ()
440 [EditorBrowsable (EditorBrowsableState.Never)]
441 protected virtual void InitOutputCache (int duration,
444 OutputCacheLocation location,
447 HttpCachePolicy cache = _context.Response.Cache;
448 bool set_vary = false;
451 case OutputCacheLocation.Any:
452 cache.SetCacheability (HttpCacheability.Public);
453 cache.SetMaxAge (new TimeSpan (0, 0, duration));
454 cache.SetLastModified (_context.Timestamp);
457 case OutputCacheLocation.Client:
458 cache.SetCacheability (HttpCacheability.Private);
459 cache.SetMaxAge (new TimeSpan (0, 0, duration));
460 cache.SetLastModified (_context.Timestamp);
462 case OutputCacheLocation.Downstream:
463 cache.SetCacheability (HttpCacheability.Public);
464 cache.SetMaxAge (new TimeSpan (0, 0, duration));
465 cache.SetLastModified (_context.Timestamp);
467 case OutputCacheLocation.Server:
468 cache.SetCacheability (HttpCacheability.Server);
471 case OutputCacheLocation.None:
476 if (varyByCustom != null)
477 cache.SetVaryByCustom (varyByCustom);
479 if (varyByParam != null && varyByParam.Length > 0) {
480 string[] prms = varyByParam.Split (';');
481 foreach (string p in prms)
482 cache.VaryByParams [p.Trim ()] = true;
483 cache.VaryByParams.IgnoreParams = false;
485 cache.VaryByParams.IgnoreParams = true;
488 if (varyByHeader != null && varyByHeader.Length > 0) {
489 string[] hdrs = varyByHeader.Split (';');
490 foreach (string h in hdrs)
491 cache.VaryByHeaders [h.Trim ()] = true;
495 cache.Duration = duration;
496 cache.SetExpires (_context.Timestamp.AddSeconds (duration));
499 [EditorBrowsable (EditorBrowsableState.Advanced)]
500 public bool IsClientScriptBlockRegistered (string key)
502 return scriptManager.IsClientScriptBlockRegistered (key);
505 [EditorBrowsable (EditorBrowsableState.Advanced)]
506 public bool IsStartupScriptRegistered (string key)
508 return scriptManager.IsStartupScriptRegistered (key);
511 public string MapPath (string virtualPath)
513 return Request.MapPath (virtualPath);
516 private void RenderPostBackScript (HtmlTextWriter writer, string formUniqueID)
518 writer.WriteLine ("<input type=\"hidden\" name=\"{0}\" value=\"\" />", postEventSourceID);
519 writer.WriteLine ("<input type=\"hidden\" name=\"{0}\" value=\"\" />", postEventArgumentID);
521 writer.WriteLine ("<script language=\"javascript\">");
522 writer.WriteLine ("<!--");
524 if (Request.Browser.Browser == ("Netscape") && Request.Browser.MajorVersion == 4)
525 writer.WriteLine ("\tvar theForm = document.{0};", formUniqueID);
527 writer.WriteLine ("\tvar theForm = document.getElementById ('{0}');", formUniqueID);
529 writer.WriteLine ("\tfunction __doPostBack(eventTarget, eventArgument) {");
530 writer.WriteLine ("\t\ttheForm.{0}.value = eventTarget;", postEventSourceID);
531 writer.WriteLine ("\t\ttheForm.{0}.value = eventArgument;", postEventArgumentID);
532 writer.WriteLine ("\t\ttheForm.submit();");
533 writer.WriteLine ("\t}");
534 writer.WriteLine ("// -->");
535 writer.WriteLine ("</script>");
538 internal void OnFormRender (HtmlTextWriter writer, string formUniqueID)
541 throw new HttpException ("Only 1 HtmlForm is allowed per page.");
543 renderingForm = true;
545 scriptManager.WriteHiddenFields (writer);
546 if (requiresPostBackScript) {
547 RenderPostBackScript (writer, formUniqueID);
548 postBackScriptRendered = true;
551 if (handleViewState) {
552 string vs = GetViewStateString ();
553 writer.Write ("<input type=\"hidden\" name=\"__VIEWSTATE\" ");
554 writer.WriteLine ("value=\"{0}\" />", vs);
557 scriptManager.WriteClientScriptBlocks (writer);
560 internal string GetViewStateString ()
562 if (_savedViewState == null)
564 StringWriter sr = new StringWriter ();
565 LosFormatter fmt = new LosFormatter ();
566 fmt.Serialize (sr, _savedViewState);
567 return sr.GetStringBuilder ().ToString ();
570 internal void OnFormPostRender (HtmlTextWriter writer, string formUniqueID)
572 scriptManager.WriteArrayDeclares (writer);
574 if (!postBackScriptRendered && requiresPostBackScript)
575 RenderPostBackScript (writer, formUniqueID);
577 scriptManager.WriteHiddenFields (writer);
578 scriptManager.WriteClientScriptIncludes (writer);
579 scriptManager.WriteStartupScriptBlocks (writer);
580 renderingForm = false;
581 postBackScriptRendered = false;
584 private void ProcessPostData (NameValueCollection data, bool second)
589 if (_requiresPostBackCopy == null && _requiresPostBack != null)
590 _requiresPostBackCopy = (ArrayList) _requiresPostBack.Clone ();
592 Hashtable used = new Hashtable ();
593 foreach (string id in data.AllKeys){
594 if (id == "__VIEWSTATE" || id == postEventSourceID || id == postEventArgumentID)
598 int dot = real_id.IndexOf ('.');
600 real_id = real_id.Substring (0, dot);
602 if (real_id == null || used.ContainsKey (real_id))
605 used.Add (real_id, real_id);
607 Control ctrl = FindControl (real_id);
609 IPostBackDataHandler pbdh = ctrl as IPostBackDataHandler;
610 IPostBackEventHandler pbeh = ctrl as IPostBackEventHandler;
614 RegisterRequiresRaiseEvent (pbeh);
618 if (pbdh.LoadPostData (real_id, data) == true) {
619 if (requiresPostDataChanged == null)
620 requiresPostDataChanged = new ArrayList ();
621 requiresPostDataChanged.Add (pbdh);
622 if (_requiresPostBackCopy != null)
623 _requiresPostBackCopy.Remove (ctrl.UniqueID);
625 } else if (!second) {
626 if (secondPostData == null)
627 secondPostData = new NameValueCollection ();
628 secondPostData.Add (real_id, data [id]);
632 ArrayList list1 = null;
633 if (_requiresPostBackCopy != null && _requiresPostBackCopy.Count > 0) {
634 string [] handlers = (string []) _requiresPostBackCopy.ToArray (typeof (string));
635 foreach (string id in handlers) {
636 IPostBackDataHandler pbdh = FindControl (id) as IPostBackDataHandler;
638 _requiresPostBackCopy.Remove (id);
639 if (pbdh.LoadPostData (id, data)) {
640 if (requiresPostDataChanged == null)
641 requiresPostDataChanged = new ArrayList ();
643 requiresPostDataChanged.Add (pbdh);
647 list1 = new ArrayList ();
651 _requiresPostBack = list1;
655 [EditorBrowsable (EditorBrowsableState.Never)]
656 public void ProcessRequest (HttpContext context)
659 if (clientTarget != null)
660 Request.ClientTarget = clientTarget;
662 WireupAutomaticEvents ();
663 //-- Control execution lifecycle in the docs
665 // Save culture information because it can be modified in FrameworkInitialize()
666 CultureInfo culture = Thread.CurrentThread.CurrentCulture;
667 CultureInfo uiculture = Thread.CurrentThread.CurrentUICulture;
668 FrameworkInitialize ();
669 context.ErrorPage = _errorPage;
672 InternalProcessRequest ();
675 UnloadRecursive (true);
677 Thread.CurrentThread.CurrentCulture = culture;
678 Thread.CurrentThread.CurrentUICulture = uiculture;
682 void InternalProcessRequest ()
684 _requestValueCollection = this.DeterminePostBackMode();
685 Trace.Write ("aspx.page", "Begin Init");
686 InitRecursive (null);
687 Trace.Write ("aspx.page", "End Init");
689 renderingForm = false;
691 Trace.Write ("aspx.page", "Begin LoadViewState");
692 LoadPageViewState ();
693 Trace.Write ("aspx.page", "End LoadViewState");
694 Trace.Write ("aspx.page", "Begin ProcessPostData");
695 ProcessPostData (_requestValueCollection, false);
696 Trace.Write ("aspx.page", "End ProcessPostData");
701 Trace.Write ("aspx.page", "Begin ProcessPostData Second Try");
702 ProcessPostData (secondPostData, true);
703 Trace.Write ("aspx.page", "End ProcessPostData Second Try");
704 Trace.Write ("aspx.page", "Begin Raise ChangedEvents");
705 RaiseChangedEvents ();
706 Trace.Write ("aspx.page", "End Raise ChangedEvents");
707 Trace.Write ("aspx.page", "Begin Raise PostBackEvent");
708 RaisePostBackEvents ();
709 Trace.Write ("aspx.page", "End Raise PostBackEvent");
714 string result = ProcessCallbackData ();
715 HtmlTextWriter callbackOutput = new HtmlTextWriter (_context.Response.Output);
716 callbackOutput.Write (result);
717 callbackOutput.Flush ();
722 Trace.Write ("aspx.page", "Begin PreRender");
723 PreRenderRecursiveInternal ();
724 Trace.Write ("aspx.page", "End PreRender");
726 Trace.Write ("aspx.page", "Begin SaveViewState");
727 SavePageViewState ();
728 Trace.Write ("aspx.page", "End SaveViewState");
731 Trace.Write ("aspx.page", "Begin Render");
732 HtmlTextWriter output = new HtmlTextWriter (_context.Response.Output);
733 RenderControl (output);
734 Trace.Write ("aspx.page", "End Render");
736 RenderTrace (output);
739 private void RenderTrace (HtmlTextWriter output)
741 TraceManager traceManager = HttpRuntime.TraceManager;
743 if (Trace.HaveTrace && !Trace.IsEnabled || !Trace.HaveTrace && !traceManager.Enabled)
748 if (!Trace.HaveTrace && traceManager.Enabled && !traceManager.PageOutput)
751 if (!traceManager.LocalOnly || Context.Request.IsLocal)
752 Trace.Render (output);
755 internal void RaisePostBackEvents ()
757 if (requiresRaiseEvent != null) {
758 RaisePostBackEvent (requiresRaiseEvent, null);
762 NameValueCollection postdata = _requestValueCollection;
763 if (postdata == null)
766 string eventTarget = postdata [postEventSourceID];
767 if (eventTarget == null || eventTarget.Length == 0) {
772 IPostBackEventHandler target = FindControl (eventTarget) as IPostBackEventHandler;
776 string eventArgument = postdata [postEventArgumentID];
777 RaisePostBackEvent (target, eventArgument);
780 internal void RaiseChangedEvents ()
782 if (requiresPostDataChanged == null)
785 foreach (IPostBackDataHandler ipdh in requiresPostDataChanged)
786 ipdh.RaisePostDataChangedEvent ();
788 requiresPostDataChanged = null;
791 [EditorBrowsable (EditorBrowsableState.Advanced)]
792 protected virtual void RaisePostBackEvent (IPostBackEventHandler sourceControl, string eventArgument)
794 sourceControl.RaisePostBackEvent (eventArgument);
797 [EditorBrowsable (EditorBrowsableState.Advanced)]
798 public void RegisterArrayDeclaration (string arrayName, string arrayValue)
800 scriptManager.RegisterArrayDeclaration (arrayName, arrayValue);
803 [EditorBrowsable (EditorBrowsableState.Advanced)]
804 public virtual void RegisterClientScriptBlock (string key, string script)
806 scriptManager.RegisterClientScriptBlock (key, script);
809 [EditorBrowsable (EditorBrowsableState.Advanced)]
810 public virtual void RegisterHiddenField (string hiddenFieldName, string hiddenFieldInitialValue)
812 scriptManager.RegisterHiddenField (hiddenFieldName, hiddenFieldInitialValue);
815 [MonoTODO("Used in HtmlForm")]
816 internal void RegisterClientScriptFile (string a, string b, string c)
818 throw new NotImplementedException ();
821 [EditorBrowsable (EditorBrowsableState.Advanced)]
822 public void RegisterOnSubmitStatement (string key, string script)
824 scriptManager.RegisterOnSubmitStatement (key, script);
827 [EditorBrowsable (EditorBrowsableState.Advanced)]
828 public void RegisterRequiresPostBack (Control control)
830 if (_requiresPostBack == null)
831 _requiresPostBack = new ArrayList ();
833 _requiresPostBack.Add (control.UniqueID);
836 [EditorBrowsable (EditorBrowsableState.Advanced)]
837 public virtual void RegisterRequiresRaiseEvent (IPostBackEventHandler control)
839 requiresRaiseEvent = control;
842 [EditorBrowsable (EditorBrowsableState.Advanced)]
843 public virtual void RegisterStartupScript (string key, string script)
845 scriptManager.RegisterStartupScript (key, script);
848 [EditorBrowsable (EditorBrowsableState.Advanced)]
849 public void RegisterViewStateHandler ()
851 handleViewState = true;
854 [EditorBrowsable (EditorBrowsableState.Advanced)]
855 protected virtual void SavePageStateToPersistenceMedium (object viewState)
857 _savedViewState = viewState;
860 [EditorBrowsable (EditorBrowsableState.Advanced)]
861 protected virtual object LoadPageStateFromPersistenceMedium ()
863 NameValueCollection postdata = _requestValueCollection;
865 if (postdata == null || (view_state = postdata ["__VIEWSTATE"]) == null)
868 _savedViewState = null;
869 LosFormatter fmt = new LosFormatter ();
872 _savedViewState = fmt.Deserialize (view_state);
873 } catch (Exception e) {
874 throw new HttpException ("Error restoring page viewstate.\n", e);
877 return _savedViewState;
880 internal void LoadPageViewState()
882 object sState = LoadPageStateFromPersistenceMedium ();
883 if (sState != null) {
884 Pair pair = (Pair) sState;
885 LoadViewStateRecursive (pair.First);
886 _requiresPostBack = pair.Second as ArrayList;
890 internal void SavePageViewState ()
892 if (!handleViewState)
895 Pair pair = new Pair ();
896 pair.First = SaveViewStateRecursive ();
897 if (_requiresPostBack != null && _requiresPostBack.Count > 0)
898 pair.Second = _requiresPostBack;
900 if (pair.First == null && pair.Second == null)
903 SavePageStateToPersistenceMedium (pair);
906 public virtual void Validate ()
908 if (_validators == null || _validators.Count == 0){
913 bool all_valid = true;
914 foreach (IValidator v in _validators){
916 if (v.IsValid == false)
924 [EditorBrowsable (EditorBrowsableState.Advanced)]
925 public virtual void VerifyRenderingInServerForm (Control control)
928 throw new HttpException ("Control '" + control.ClientID + " " + control.GetType () +
929 "' must be rendered within a HtmlForm");
939 ClientScriptManager ClientScript {
940 get { return scriptManager; }
944 public string GetWebResourceUrl(Type type, string resourceName)
947 throw new ArgumentNullException ("type");
949 if (resourceName == null || resourceName.Length == 0)
950 throw new ArgumentNullException ("type");
952 return System.Web.Handlers.AssemblyResourceLoader.GetResourceUrl (type, resourceName);
957 internal void PushDataItemContext (object o)
959 if (dataItemCtx == null)
960 dataItemCtx = new Stack ();
962 dataItemCtx.Push (o);
965 internal void PopDataItemContext ()
967 if (dataItemCtx == null)
968 throw new InvalidOperationException ();
973 internal object CurrentDataItem {
975 if (dataItemCtx == null)
976 throw new InvalidOperationException ("No data item");
978 return dataItemCtx.Peek ();
982 protected object Eval (string expression)
984 return DataBinder.Eval (CurrentDataItem, expression);
987 protected object Eval (string expression, string format)
989 return DataBinder.Eval (CurrentDataItem, expression, format);
992 protected object XPath (string xpathexpression)
994 return XPathBinder.Eval (CurrentDataItem, xpathexpression);
997 protected object XPath (string xpathexpression, string format)
999 return XPathBinder.Eval (CurrentDataItem, xpathexpression, format);
1002 protected IEnumerable XPathSelect (string xpathexpression)
1004 return XPathBinder.Select (CurrentDataItem, xpathexpression);
1007 public string GetCallbackEventReference (Control control, string argument, string clientCallback, string context)
1009 return GetCallbackEventReference (control, argument, clientCallback, context, null);
1012 public string GetCallbackEventReference (Control control, string argument, string clientCallback, string context, string clientErrorCallback)
1014 if (!ClientScript.IsClientScriptIncludeRegistered (typeof(Page), "callback"))
1015 ClientScript.RegisterClientScriptInclude (typeof(Page), "callback", GetWebResourceUrl (typeof(Page), "callback.js"));
1017 return string.Format ("WebForm_DoCallback ('{0}', {1}, {2}, {3}, {4})", control.UniqueID, argument, clientCallback, context, clientErrorCallback);
1020 public bool IsCallback {
1021 get { return _requestValueCollection != null && _requestValueCollection [callbackArgumentID] != null; }
1024 string ProcessCallbackData ()
1026 string callbackTarget = _requestValueCollection [callbackSourceID];
1027 if (callbackTarget == null || callbackTarget.Length == 0)
1028 throw new HttpException ("Callback target not provided.");
1030 ICallbackEventHandler target = FindControl (callbackTarget) as ICallbackEventHandler;
1032 throw new HttpException (string.Format ("Invalid callback target '{0}'.", callbackTarget));
1034 string callbackArgument = _requestValueCollection [callbackArgumentID];
1035 return target.RaiseCallbackEvent (callbackArgument);