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 using System.Web.UI.HtmlControls;
53 namespace System.Web.UI
57 [RootDesignerSerializer ("Microsoft.VSDesigner.WebForms.RootCodeDomSerializer, " + Consts.AssemblyMicrosoft_VSDesigner, "System.ComponentModel.Design.Serialization.CodeDomSerializer, " + Consts.AssemblySystem_Design, true)]
59 [DefaultEvent ("Load"), DesignerCategory ("ASPXCodeBehind")]
61 [Designer ("System.Web.UI.Design.ControlDesigner, " + Consts.AssemblySystem_Design, typeof (IDesigner))]
62 public class Page : TemplateControl, IHttpHandler
64 private bool _viewState = true;
65 private bool _viewStateMac;
66 private string _errorPage;
67 private bool _isValid;
68 private bool _smartNavigation;
69 private int _transactionMode;
70 private HttpContext _context;
71 private ValidatorCollection _validators;
72 private bool renderingForm;
73 private object _savedViewState;
74 private ArrayList _requiresPostBack;
75 private ArrayList _requiresPostBackCopy;
76 private ArrayList requiresPostDataChanged;
77 private IPostBackEventHandler requiresRaiseEvent;
78 private NameValueCollection secondPostData;
79 private bool requiresPostBackScript;
80 private bool postBackScriptRendered;
82 string viewStateUserKey;
83 NameValueCollection _requestValueCollection;
85 ClientScriptManager scriptManager;
87 [EditorBrowsable (EditorBrowsableState.Never)]
88 protected const string postEventArgumentID = "__EVENTARGUMENT";
89 [EditorBrowsable (EditorBrowsableState.Never)]
90 protected const string postEventSourceID = "__EVENTTARGET";
93 internal const string CallbackArgumentID = "__CALLBACKARGUMENT";
94 internal const string CallbackSourceID = "__CALLBACKTARGET";
95 internal const string PreviousPageID = "__PREVIOUSPAGE";
97 IPageHeader htmlHeader;
99 MasterPage masterPage;
100 string masterPageFile;
103 bool isCrossPagePostBack;
104 ArrayList requireStateControls;
105 Hashtable _validatorsByGroup;
112 scriptManager = new ClientScriptManager (this);
120 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
122 public HttpApplicationState Application
124 get { return _context.Application; }
127 [EditorBrowsable (EditorBrowsableState.Never)]
129 public bool AspCompatMode
131 get { return false; }
132 set { throw new NotImplementedException (); }
135 protected bool AspCompatMode
137 set { throw new NotImplementedException (); }
141 [EditorBrowsable (EditorBrowsableState.Never)]
143 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
144 [BrowsableAttribute (false)]
147 get { return Response.BufferOutput; }
148 set { Response.BufferOutput = value; }
151 protected bool Buffer
153 set { Response.BufferOutput = value; }
157 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
161 get { return _context.Cache; }
165 [EditorBrowsableAttribute (EditorBrowsableState.Advanced)]
167 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
168 [Browsable (false), DefaultValue ("")]
169 [WebSysDescription ("Value do override the automatic browser detection and force the page to use the specified browser.")]
170 public string ClientTarget
172 get { return (clientTarget == null) ? "" : clientTarget; }
174 clientTarget = value;
180 [EditorBrowsable (EditorBrowsableState.Never)]
182 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
183 [BrowsableAttribute (false)]
186 get { return Response.ContentEncoding.CodePage; }
187 set { Response.ContentEncoding = Encoding.GetEncoding (value); }
190 protected int CodePage
192 set { Response.ContentEncoding = Encoding.GetEncoding (value); }
196 [EditorBrowsable (EditorBrowsableState.Never)]
198 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
199 [BrowsableAttribute (false)]
200 public string ContentType
202 get { return Response.ContentType; }
203 set { Response.ContentType = value; }
206 protected string ContentType
208 set { Response.ContentType = value; }
212 protected override HttpContext Context
215 if (_context == null)
216 return HttpContext.Current;
222 [EditorBrowsable (EditorBrowsableState.Never)]
224 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
225 [BrowsableAttribute (false)]
226 public string Culture
228 get { return Thread.CurrentThread.CurrentCulture.Name; }
229 set { Thread.CurrentThread.CurrentCulture = new CultureInfo (value); }
232 protected string Culture
234 set { Thread.CurrentThread.CurrentCulture = new CultureInfo (value); }
239 public override bool EnableViewState
241 get { return _viewState; }
242 set { _viewState = value; }
246 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
247 [BrowsableAttribute (false)]
249 [EditorBrowsable (EditorBrowsableState.Never)]
250 protected bool EnableViewStateMac
252 get { return _viewStateMac; }
253 set { _viewStateMac = value; }
256 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
257 [Browsable (false), DefaultValue ("")]
258 [WebSysDescription ("The URL of a page used for error redirection.")]
259 public string ErrorPage
261 get { return _errorPage; }
264 if (_context != null)
265 _context.ErrorPage = value;
272 [EditorBrowsable (EditorBrowsableState.Never)]
273 protected ArrayList FileDependencies
276 if (Response != null)
277 Response.AddFileDependencies (value);
282 public override string ID
284 get { return base.ID; }
285 set { base.ID = value; }
288 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
290 public bool IsPostBack
293 return _requestValueCollection != null;
297 [EditorBrowsable (EditorBrowsableState.Never), Browsable (false)]
298 public bool IsReusable {
299 get { return false; }
302 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
306 get { return _isValid; }
309 [EditorBrowsable (EditorBrowsableState.Never)]
311 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
312 [BrowsableAttribute (false)]
314 get { return Thread.CurrentThread.CurrentCulture.LCID; }
315 set { Thread.CurrentThread.CurrentCulture = new CultureInfo (value); }
319 set { Thread.CurrentThread.CurrentCulture = new CultureInfo (value); }
323 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
325 public HttpRequest Request
327 get { return _context.Request; }
330 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
332 public HttpResponse Response
334 get { return _context.Response; }
337 [EditorBrowsable (EditorBrowsableState.Never)]
339 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
340 [BrowsableAttribute (false)]
341 public string ResponseEncoding
343 get { return Response.ContentEncoding.WebName; }
344 set { Response.ContentEncoding = Encoding.GetEncoding (value); }
347 protected string ResponseEncoding
349 set { Response.ContentEncoding = Encoding.GetEncoding (value); }
353 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
355 public HttpServerUtility Server
358 return Context.Server;
362 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
364 public virtual HttpSessionState Session
367 if (_context.Session == null)
368 throw new HttpException ("Session state can only be used " +
369 "when enableSessionState is set to true, either " +
370 "in a configuration file or in the Page directive.");
372 return _context.Session;
377 [FilterableAttribute (false)]
380 public bool SmartNavigation
382 get { return _smartNavigation; }
383 set { _smartNavigation = value; }
386 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
388 public TraceContext Trace
390 get { return Context.Trace; }
393 [EditorBrowsable (EditorBrowsableState.Never)]
395 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
396 [BrowsableAttribute (false)]
397 public bool TraceEnabled
399 get { return Trace.IsEnabled; }
400 set { Trace.IsEnabled = value; }
403 protected bool TraceEnabled
405 set { Trace.IsEnabled = value; }
409 [EditorBrowsable (EditorBrowsableState.Never)]
411 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
412 [BrowsableAttribute (false)]
413 public TraceMode TraceModeValue
415 get { return Trace.TraceMode; }
416 set { Trace.TraceMode = value; }
419 protected TraceMode TraceModeValue
421 set { Trace.TraceMode = value; }
425 [EditorBrowsable (EditorBrowsableState.Never)]
427 public int TransactionMode
429 get { return _transactionMode; }
430 set { _transactionMode = value; }
433 protected int TransactionMode
435 set { _transactionMode = value; }
439 [EditorBrowsable (EditorBrowsableState.Never)]
441 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
442 [BrowsableAttribute (false)]
443 public string UICulture
445 get { return Thread.CurrentThread.CurrentUICulture.Name; }
446 set { Thread.CurrentThread.CurrentUICulture = new CultureInfo (value); }
449 protected string UICulture
451 set { Thread.CurrentThread.CurrentUICulture = new CultureInfo (value); }
455 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
457 public IPrincipal User
459 get { return _context.User; }
462 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
464 public ValidatorCollection Validators
467 if (_validators == null)
468 _validators = new ValidatorCollection ();
473 [MonoTODO ("Use this when encrypting/decrypting ViewState")]
475 public string ViewStateUserKey {
476 get { return viewStateUserKey; }
477 set { viewStateUserKey = value; }
481 public override bool Visible
483 get { return base.Visible; }
484 set { base.Visible = value; }
491 [EditorBrowsable (EditorBrowsableState.Never)]
492 protected IAsyncResult AspCompatBeginProcessRequest (HttpContext context,
496 throw new NotImplementedException ();
499 [EditorBrowsable (EditorBrowsableState.Never)]
500 protected void AspCompatEndProcessRequest (IAsyncResult result)
502 throw new NotImplementedException ();
505 [EditorBrowsable (EditorBrowsableState.Advanced)]
506 protected virtual HtmlTextWriter CreateHtmlTextWriter (TextWriter tw)
508 return new HtmlTextWriter (tw);
511 [EditorBrowsable (EditorBrowsableState.Never)]
512 public void DesignerInitialize ()
514 InitRecursive (null);
517 [EditorBrowsable (EditorBrowsableState.Advanced)]
518 protected virtual NameValueCollection DeterminePostBackMode ()
520 if (_context == null)
523 HttpRequest req = _context.Request;
527 NameValueCollection coll = null;
528 if (0 == String.Compare (Request.HttpMethod, "POST", true))
531 coll = req.QueryString;
534 if (coll == null || coll ["__VIEWSTATE"] == null)
543 [EditorBrowsable (EditorBrowsableState.Advanced)]
544 public string GetPostBackClientEvent (Control control, string argument)
546 return scriptManager.GetPostBackClientEvent (control, argument);
552 [EditorBrowsable (EditorBrowsableState.Advanced)]
553 public string GetPostBackClientHyperlink (Control control, string argument)
555 return scriptManager.GetPostBackClientHyperlink (control, argument);
561 [EditorBrowsable (EditorBrowsableState.Advanced)]
562 public string GetPostBackEventReference (Control control)
564 return scriptManager.GetPostBackEventReference (control);
570 [EditorBrowsable (EditorBrowsableState.Advanced)]
571 public string GetPostBackEventReference (Control control, string argument)
573 return scriptManager.GetPostBackEventReference (control, argument);
576 internal void RequiresPostBackScript ()
578 requiresPostBackScript = true;
581 [EditorBrowsable (EditorBrowsableState.Never)]
582 public virtual int GetTypeHashCode ()
587 [EditorBrowsable (EditorBrowsableState.Never)]
588 protected virtual void InitOutputCache (int duration,
591 OutputCacheLocation location,
594 HttpCachePolicy cache = _context.Response.Cache;
595 bool set_vary = false;
598 case OutputCacheLocation.Any:
599 cache.SetCacheability (HttpCacheability.Public);
600 cache.SetMaxAge (new TimeSpan (0, 0, duration));
601 cache.SetLastModified (_context.Timestamp);
604 case OutputCacheLocation.Client:
605 cache.SetCacheability (HttpCacheability.Private);
606 cache.SetMaxAge (new TimeSpan (0, 0, duration));
607 cache.SetLastModified (_context.Timestamp);
609 case OutputCacheLocation.Downstream:
610 cache.SetCacheability (HttpCacheability.Public);
611 cache.SetMaxAge (new TimeSpan (0, 0, duration));
612 cache.SetLastModified (_context.Timestamp);
614 case OutputCacheLocation.Server:
615 cache.SetCacheability (HttpCacheability.Server);
618 case OutputCacheLocation.None:
623 if (varyByCustom != null)
624 cache.SetVaryByCustom (varyByCustom);
626 if (varyByParam != null && varyByParam.Length > 0) {
627 string[] prms = varyByParam.Split (';');
628 foreach (string p in prms)
629 cache.VaryByParams [p.Trim ()] = true;
630 cache.VaryByParams.IgnoreParams = false;
632 cache.VaryByParams.IgnoreParams = true;
635 if (varyByHeader != null && varyByHeader.Length > 0) {
636 string[] hdrs = varyByHeader.Split (';');
637 foreach (string h in hdrs)
638 cache.VaryByHeaders [h.Trim ()] = true;
642 cache.Duration = duration;
643 cache.SetExpires (_context.Timestamp.AddSeconds (duration));
649 [EditorBrowsable (EditorBrowsableState.Advanced)]
651 public bool IsClientScriptBlockRegistered (string key)
653 return scriptManager.IsClientScriptBlockRegistered (key);
659 [EditorBrowsable (EditorBrowsableState.Advanced)]
661 public bool IsStartupScriptRegistered (string key)
663 return scriptManager.IsStartupScriptRegistered (key);
666 public string MapPath (string virtualPath)
668 return Request.MapPath (virtualPath);
671 private void RenderPostBackScript (HtmlTextWriter writer, string formUniqueID)
673 writer.WriteLine ("<input type=\"hidden\" name=\"{0}\" value=\"\" />", postEventSourceID);
674 writer.WriteLine ("<input type=\"hidden\" name=\"{0}\" value=\"\" />", postEventArgumentID);
676 writer.WriteLine ("<script language=\"javascript\">");
677 writer.WriteLine ("<!--");
679 if (Request.Browser.Browser == ("Netscape") && Request.Browser.MajorVersion == 4)
680 writer.WriteLine ("\tvar theForm = document.{0};", formUniqueID);
682 writer.WriteLine ("\tvar theForm = document.getElementById ('{0}');", formUniqueID);
684 writer.WriteLine ("\tfunction __doPostBack(eventTarget, eventArgument) {");
685 writer.WriteLine ("\t\ttheForm.{0}.value = eventTarget;", postEventSourceID);
686 writer.WriteLine ("\t\ttheForm.{0}.value = eventArgument;", postEventArgumentID);
687 writer.WriteLine ("\t\ttheForm.submit();");
688 writer.WriteLine ("\t}");
689 writer.WriteLine ("// -->");
690 writer.WriteLine ("</script>");
693 internal void OnFormRender (HtmlTextWriter writer, string formUniqueID)
696 throw new HttpException ("Only 1 HtmlForm is allowed per page.");
698 renderingForm = true;
700 scriptManager.WriteHiddenFields (writer);
701 if (requiresPostBackScript) {
702 RenderPostBackScript (writer, formUniqueID);
703 postBackScriptRendered = true;
706 if (handleViewState) {
707 string vs = GetViewStateString ();
708 writer.Write ("<input type=\"hidden\" name=\"__VIEWSTATE\" ");
709 writer.WriteLine ("value=\"{0}\" />", vs);
712 scriptManager.WriteClientScriptBlocks (writer);
715 internal string GetViewStateString ()
717 if (_savedViewState == null)
719 StringWriter sr = new StringWriter ();
720 LosFormatter fmt = new LosFormatter ();
721 fmt.Serialize (sr, _savedViewState);
722 return sr.GetStringBuilder ().ToString ();
725 internal void OnFormPostRender (HtmlTextWriter writer, string formUniqueID)
727 scriptManager.WriteArrayDeclares (writer);
729 if (!postBackScriptRendered && requiresPostBackScript)
730 RenderPostBackScript (writer, formUniqueID);
732 scriptManager.WriteHiddenFields (writer);
733 scriptManager.WriteClientScriptIncludes (writer);
734 scriptManager.WriteStartupScriptBlocks (writer);
735 renderingForm = false;
736 postBackScriptRendered = false;
739 private void ProcessPostData (NameValueCollection data, bool second)
744 if (_requiresPostBackCopy == null && _requiresPostBack != null)
745 _requiresPostBackCopy = (ArrayList) _requiresPostBack.Clone ();
747 Hashtable used = new Hashtable ();
748 foreach (string id in data.AllKeys){
749 if (id == "__VIEWSTATE" || id == postEventSourceID || id == postEventArgumentID)
753 int dot = real_id.IndexOf ('.');
755 real_id = real_id.Substring (0, dot);
757 if (real_id == null || used.ContainsKey (real_id))
760 used.Add (real_id, real_id);
762 Control ctrl = FindControl (real_id);
764 IPostBackDataHandler pbdh = ctrl as IPostBackDataHandler;
765 IPostBackEventHandler pbeh = ctrl as IPostBackEventHandler;
769 RegisterRequiresRaiseEvent (pbeh);
773 if (pbdh.LoadPostData (real_id, data) == true) {
774 if (requiresPostDataChanged == null)
775 requiresPostDataChanged = new ArrayList ();
776 requiresPostDataChanged.Add (pbdh);
777 if (_requiresPostBackCopy != null)
778 _requiresPostBackCopy.Remove (ctrl.UniqueID);
780 } else if (!second) {
781 if (secondPostData == null)
782 secondPostData = new NameValueCollection ();
783 secondPostData.Add (real_id, data [id]);
787 ArrayList list1 = null;
788 if (_requiresPostBackCopy != null && _requiresPostBackCopy.Count > 0) {
789 string [] handlers = (string []) _requiresPostBackCopy.ToArray (typeof (string));
790 foreach (string id in handlers) {
791 IPostBackDataHandler pbdh = FindControl (id) as IPostBackDataHandler;
793 _requiresPostBackCopy.Remove (id);
794 if (pbdh.LoadPostData (id, data)) {
795 if (requiresPostDataChanged == null)
796 requiresPostDataChanged = new ArrayList ();
798 requiresPostDataChanged.Add (pbdh);
802 list1 = new ArrayList ();
807 _requiresPostBack = list1;
810 [EditorBrowsable (EditorBrowsableState.Never)]
811 public void ProcessRequest (HttpContext context)
814 if (clientTarget != null)
815 Request.ClientTarget = clientTarget;
817 WireupAutomaticEvents ();
818 //-- Control execution lifecycle in the docs
820 // Save culture information because it can be modified in FrameworkInitialize()
821 CultureInfo culture = Thread.CurrentThread.CurrentCulture;
822 CultureInfo uiculture = Thread.CurrentThread.CurrentUICulture;
823 FrameworkInitialize ();
824 context.ErrorPage = _errorPage;
827 InternalProcessRequest ();
830 UnloadRecursive (true);
832 if (Thread.CurrentThread.CurrentCulture.Equals (culture) == false)
833 Thread.CurrentThread.CurrentCulture = culture;
835 if (Thread.CurrentThread.CurrentUICulture.Equals (uiculture) == false)
836 Thread.CurrentThread.CurrentUICulture = uiculture;
841 internal void ProcessCrossPagePostBack (HttpContext context)
843 isCrossPagePostBack = true;
844 ProcessRequest (context);
848 void InternalProcessRequest ()
850 _requestValueCollection = this.DeterminePostBackMode();
853 if (!IsCrossPagePostBack)
854 LoadPreviousPageReference ();
856 OnPreInit (EventArgs.Empty);
858 Trace.Write ("aspx.page", "Begin Init");
859 InitRecursive (null);
860 Trace.Write ("aspx.page", "End Init");
863 OnInitComplete (EventArgs.Empty);
865 if (masterPageFile != null) {
866 Controls.Add (Master);
867 Master.FillPlaceHolders ();
871 renderingForm = false;
873 Trace.Write ("aspx.page", "Begin LoadViewState");
874 LoadPageViewState ();
875 Trace.Write ("aspx.page", "End LoadViewState");
876 Trace.Write ("aspx.page", "Begin ProcessPostData");
877 ProcessPostData (_requestValueCollection, false);
878 Trace.Write ("aspx.page", "End ProcessPostData");
882 if (IsCrossPagePostBack)
885 OnPreLoad (EventArgs.Empty);
890 Trace.Write ("aspx.page", "Begin ProcessPostData Second Try");
891 ProcessPostData (secondPostData, true);
892 Trace.Write ("aspx.page", "End ProcessPostData Second Try");
893 Trace.Write ("aspx.page", "Begin Raise ChangedEvents");
894 RaiseChangedEvents ();
895 Trace.Write ("aspx.page", "End Raise ChangedEvents");
896 Trace.Write ("aspx.page", "Begin Raise PostBackEvent");
897 RaisePostBackEvents ();
898 Trace.Write ("aspx.page", "End Raise PostBackEvent");
902 OnLoadComplete (EventArgs.Empty);
905 string result = ProcessCallbackData ();
906 HtmlTextWriter callbackOutput = new HtmlTextWriter (_context.Response.Output);
907 callbackOutput.Write (result);
908 callbackOutput.Flush ();
913 Trace.Write ("aspx.page", "Begin PreRender");
914 PreRenderRecursiveInternal ();
915 Trace.Write ("aspx.page", "End PreRender");
918 OnPreRenderComplete (EventArgs.Empty);
921 Trace.Write ("aspx.page", "Begin SaveViewState");
922 SavePageViewState ();
923 Trace.Write ("aspx.page", "End SaveViewState");
926 OnSaveStateComplete (EventArgs.Empty);
930 Trace.Write ("aspx.page", "Begin Render");
931 HtmlTextWriter output = new HtmlTextWriter (_context.Response.Output);
932 RenderControl (output);
933 Trace.Write ("aspx.page", "End Render");
935 RenderTrace (output);
938 private void RenderTrace (HtmlTextWriter output)
940 TraceManager traceManager = HttpRuntime.TraceManager;
942 if (Trace.HaveTrace && !Trace.IsEnabled || !Trace.HaveTrace && !traceManager.Enabled)
947 if (!Trace.HaveTrace && traceManager.Enabled && !traceManager.PageOutput)
950 if (!traceManager.LocalOnly || Context.Request.IsLocal)
951 Trace.Render (output);
954 internal void RaisePostBackEvents ()
956 if (requiresRaiseEvent != null) {
957 RaisePostBackEvent (requiresRaiseEvent, null);
961 NameValueCollection postdata = _requestValueCollection;
962 if (postdata == null)
965 string eventTarget = postdata [postEventSourceID];
966 if (eventTarget == null || eventTarget.Length == 0) {
971 IPostBackEventHandler target = FindControl (eventTarget) as IPostBackEventHandler;
975 string eventArgument = postdata [postEventArgumentID];
976 RaisePostBackEvent (target, eventArgument);
979 internal void RaiseChangedEvents ()
981 if (requiresPostDataChanged == null)
984 foreach (IPostBackDataHandler ipdh in requiresPostDataChanged)
985 ipdh.RaisePostDataChangedEvent ();
987 requiresPostDataChanged = null;
990 [EditorBrowsable (EditorBrowsableState.Advanced)]
991 protected virtual void RaisePostBackEvent (IPostBackEventHandler sourceControl, string eventArgument)
993 sourceControl.RaisePostBackEvent (eventArgument);
999 [EditorBrowsable (EditorBrowsableState.Advanced)]
1000 public void RegisterArrayDeclaration (string arrayName, string arrayValue)
1002 scriptManager.RegisterArrayDeclaration (arrayName, arrayValue);
1008 [EditorBrowsable (EditorBrowsableState.Advanced)]
1009 public virtual void RegisterClientScriptBlock (string key, string script)
1011 scriptManager.RegisterClientScriptBlock (key, script);
1017 [EditorBrowsable (EditorBrowsableState.Advanced)]
1018 public virtual void RegisterHiddenField (string hiddenFieldName, string hiddenFieldInitialValue)
1020 scriptManager.RegisterHiddenField (hiddenFieldName, hiddenFieldInitialValue);
1023 [MonoTODO("Used in HtmlForm")]
1024 internal void RegisterClientScriptFile (string a, string b, string c)
1026 throw new NotImplementedException ();
1032 [EditorBrowsable (EditorBrowsableState.Advanced)]
1033 public void RegisterOnSubmitStatement (string key, string script)
1035 scriptManager.RegisterOnSubmitStatement (key, script);
1038 [EditorBrowsable (EditorBrowsableState.Advanced)]
1039 public void RegisterRequiresPostBack (Control control)
1041 if (_requiresPostBack == null)
1042 _requiresPostBack = new ArrayList ();
1044 _requiresPostBack.Add (control.UniqueID);
1047 [EditorBrowsable (EditorBrowsableState.Advanced)]
1048 public virtual void RegisterRequiresRaiseEvent (IPostBackEventHandler control)
1050 requiresRaiseEvent = control;
1056 [EditorBrowsable (EditorBrowsableState.Advanced)]
1057 public virtual void RegisterStartupScript (string key, string script)
1059 scriptManager.RegisterStartupScript (key, script);
1062 [EditorBrowsable (EditorBrowsableState.Advanced)]
1063 public void RegisterViewStateHandler ()
1065 handleViewState = true;
1068 [EditorBrowsable (EditorBrowsableState.Advanced)]
1069 protected virtual void SavePageStateToPersistenceMedium (object viewState)
1071 _savedViewState = viewState;
1074 [EditorBrowsable (EditorBrowsableState.Advanced)]
1075 protected virtual object LoadPageStateFromPersistenceMedium ()
1077 NameValueCollection postdata = _requestValueCollection;
1079 if (postdata == null || (view_state = postdata ["__VIEWSTATE"]) == null)
1082 _savedViewState = null;
1083 LosFormatter fmt = new LosFormatter ();
1086 _savedViewState = fmt.Deserialize (view_state);
1087 } catch (Exception e) {
1088 throw new HttpException ("Error restoring page viewstate.\n", e);
1091 return _savedViewState;
1094 internal void LoadPageViewState()
1096 object sState = LoadPageStateFromPersistenceMedium ();
1097 if (sState != null) {
1099 Triplet data = (Triplet) sState;
1100 LoadPageControlState (data.Third);
1101 LoadViewStateRecursive (data.First);
1102 _requiresPostBack = data.Second as ArrayList;
1104 Pair pair = (Pair) sState;
1105 LoadViewStateRecursive (pair.First);
1106 _requiresPostBack = pair.Second as ArrayList;
1111 internal void SavePageViewState ()
1113 if (!handleViewState)
1117 object controlState = SavePageControlState ();
1120 object viewState = SaveViewStateRecursive ();
1121 object reqPostback = (_requiresPostBack != null && _requiresPostBack.Count > 0) ? _requiresPostBack : null;
1124 Triplet triplet = new Triplet ();
1125 triplet.First = viewState;
1126 triplet.Second = reqPostback;
1127 triplet.Third = controlState;
1129 if (triplet.First == null && triplet.Second == null && triplet.Third == null)
1132 SavePageStateToPersistenceMedium (triplet);
1134 Pair pair = new Pair ();
1135 pair.First = viewState;
1136 pair.Second = reqPostback;
1138 if (pair.First == null && pair.Second == null)
1141 SavePageStateToPersistenceMedium (pair);
1145 public virtual void Validate ()
1147 ValidateCollection (_validators);
1150 void ValidateCollection (ValidatorCollection validators)
1152 if (validators == null || validators.Count == 0){
1157 bool all_valid = true;
1158 foreach (IValidator v in validators){
1160 if (v.IsValid == false)
1168 [EditorBrowsable (EditorBrowsableState.Advanced)]
1169 public virtual void VerifyRenderingInServerForm (Control control)
1172 throw new HttpException ("Control '" + control.ClientID + " " + control.GetType () +
1173 "' must be rendered within a HtmlForm");
1183 ClientScriptManager ClientScript {
1184 get { return scriptManager; }
1189 static readonly object InitCompleteEvent = new object ();
1190 static readonly object LoadCompleteEvent = new object ();
1191 static readonly object PreInitEvent = new object ();
1192 static readonly object PreLoadEvent = new object ();
1193 static readonly object PreRenderCompleteEvent = new object ();
1194 static readonly object SaveStateCompleteEvent = new object ();
1196 public event EventHandler InitComplete {
1197 add { Events.AddHandler (InitCompleteEvent, value); }
1198 remove { Events.RemoveHandler (InitCompleteEvent, value); }
1201 public event EventHandler LoadComplete {
1202 add { Events.AddHandler (LoadCompleteEvent, value); }
1203 remove { Events.RemoveHandler (LoadCompleteEvent, value); }
1206 public event EventHandler PreInit {
1207 add { Events.AddHandler (PreInitEvent, value); }
1208 remove { Events.RemoveHandler (PreInitEvent, value); }
1211 public event EventHandler PreLoad {
1212 add { Events.AddHandler (PreLoadEvent, value); }
1213 remove { Events.RemoveHandler (PreLoadEvent, value); }
1216 public event EventHandler PreRenderComplete {
1217 add { Events.AddHandler (PreRenderCompleteEvent, value); }
1218 remove { Events.RemoveHandler (PreRenderCompleteEvent, value); }
1221 public event EventHandler SaveStateComplete {
1222 add { Events.AddHandler (SaveStateCompleteEvent, value); }
1223 remove { Events.RemoveHandler (SaveStateCompleteEvent, value); }
1226 protected virtual void OnInitComplete (EventArgs e)
1228 if (Events != null) {
1229 EventHandler eh = (EventHandler) (Events [InitCompleteEvent]);
1230 if (eh != null) eh (this, e);
1234 protected virtual void OnLoadComplete (EventArgs e)
1236 if (Events != null) {
1237 EventHandler eh = (EventHandler) (Events [LoadCompleteEvent]);
1238 if (eh != null) eh (this, e);
1242 protected virtual void OnPreInit (EventArgs e)
1244 if (Events != null) {
1245 EventHandler eh = (EventHandler) (Events [PreInitEvent]);
1246 if (eh != null) eh (this, e);
1250 protected virtual void OnPreLoad (EventArgs e)
1252 if (Events != null) {
1253 EventHandler eh = (EventHandler) (Events [PreLoadEvent]);
1254 if (eh != null) eh (this, e);
1258 protected virtual void OnPreRenderComplete (EventArgs e)
1260 if (Events != null) {
1261 EventHandler eh = (EventHandler) (Events [PreRenderCompleteEvent]);
1262 if (eh != null) eh (this, e);
1266 protected virtual void OnSaveStateComplete (EventArgs e)
1268 if (Events != null) {
1269 EventHandler eh = (EventHandler) (Events [SaveStateCompleteEvent]);
1270 if (eh != null) eh (this, e);
1274 public HtmlForm Form {
1275 get { return _form; }
1278 internal void RegisterForm (HtmlForm form)
1283 [BrowsableAttribute (false)]
1284 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
1285 public Page PreviousPage {
1286 get { return previousPage; }
1290 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
1291 [BrowsableAttribute (false)]
1292 public bool IsCallback {
1293 get { return _requestValueCollection != null && _requestValueCollection [CallbackArgumentID] != null; }
1296 [BrowsableAttribute (false)]
1297 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
1298 public bool IsCrossPagePostBack {
1299 get { return _requestValueCollection != null && isCrossPagePostBack; }
1302 string ProcessCallbackData ()
1304 string callbackTarget = _requestValueCollection [CallbackSourceID];
1305 if (callbackTarget == null || callbackTarget.Length == 0)
1306 throw new HttpException ("Callback target not provided.");
1308 ICallbackEventHandler target = FindControl (callbackTarget) as ICallbackEventHandler;
1310 throw new HttpException (string.Format ("Invalid callback target '{0}'.", callbackTarget));
1312 string callbackArgument = _requestValueCollection [CallbackArgumentID];
1313 return target.RaiseCallbackEvent (callbackArgument);
1316 [BrowsableAttribute (false)]
1317 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
1318 public IPageHeader Header {
1319 get { return htmlHeader; }
1322 internal void SetHeader (IPageHeader header)
1324 htmlHeader = header;
1327 [DefaultValueAttribute ("")]
1328 public string MasterPageFile {
1329 get { return masterPageFile; }
1330 set { masterPageFile = value; masterPage = null; }
1333 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
1334 [BrowsableAttribute (false)]
1335 public MasterPage Master {
1337 if (masterPage == null)
1338 masterPage = MasterPageParser.GetCompiledMasterInstance (masterPageFile, Server.MapPath (masterPageFile), Context);
1343 [EditorBrowsable (EditorBrowsableState.Advanced)]
1344 public void RegisterRequiresControlState (Control control)
1346 if (requireStateControls == null) requireStateControls = new ArrayList ();
1347 requireStateControls.Add (control);
1350 public bool RequiresControlState (Control control)
1352 if (requireStateControls == null) return false;
1353 return requireStateControls.Contains (control);
1356 [EditorBrowsable (EditorBrowsableState.Advanced)]
1357 public void UnregisterRequiresControlState (Control control)
1359 if (requireStateControls != null)
1360 requireStateControls.Remove (control);
1363 public ValidatorCollection GetValidators (string validationGroup)
1365 if (validationGroup == null || validationGroup == "")
1368 if (_validatorsByGroup == null) _validatorsByGroup = new Hashtable ();
1369 ValidatorCollection col = _validatorsByGroup [validationGroup] as ValidatorCollection;
1371 col = new ValidatorCollection ();
1372 _validatorsByGroup [validationGroup] = col;
1377 public virtual void Validate (string validationGroup)
1379 if (validationGroup == null || validationGroup == "")
1380 ValidateCollection (_validators);
1382 if (_validatorsByGroup != null) {
1383 ValidateCollection (_validatorsByGroup [validationGroup] as ValidatorCollection);
1390 object SavePageControlState ()
1392 if (requireStateControls == null) return null;
1393 object[] state = new object [requireStateControls.Count];
1395 bool allNull = true;
1396 for (int n=0; n<state.Length; n++) {
1397 state [n] = ((Control) requireStateControls [n]).SaveControlState ();
1398 if (state [n] != null) allNull = false;
1400 if (allNull) return null;
1404 void LoadPageControlState (object data)
1406 if (requireStateControls == null) return;
1408 object[] state = (object[]) data;
1409 int max = Math.Min (requireStateControls.Count, state != null ? state.Length : requireStateControls.Count);
1410 for (int n=0; n < max; n++) {
1411 Control ctl = (Control) requireStateControls [n];
1412 ctl.LoadControlState (state != null ? state [n] : null);
1416 void LoadPreviousPageReference ()
1418 if (_requestValueCollection != null) {
1419 string prevPage = _requestValueCollection [PreviousPageID];
1420 if (prevPage != null) {
1421 previousPage = (Page) PageParser.GetCompiledPageInstance (prevPage, Server.MapPath (prevPage), Context);
1422 previousPage.ProcessCrossPagePostBack (_context);
1424 previousPage = _context.LastPage;
1427 _context.LastPage = this;
1431 protected internal void AddContentTemplate (string templateName, ITemplate template)
1433 Master.AddContentTemplate (templateName, template);