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 ();
806 _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 Thread.CurrentThread.CurrentCulture = culture;
833 Thread.CurrentThread.CurrentUICulture = uiculture;
838 internal void ProcessCrossPagePostBack (HttpContext context)
840 isCrossPagePostBack = true;
841 ProcessRequest (context);
845 void InternalProcessRequest ()
847 _requestValueCollection = this.DeterminePostBackMode();
850 if (!IsCrossPagePostBack)
851 LoadPreviousPageReference ();
853 OnPreInit (EventArgs.Empty);
855 Trace.Write ("aspx.page", "Begin Init");
856 InitRecursive (null);
857 Trace.Write ("aspx.page", "End Init");
860 OnInitComplete (EventArgs.Empty);
862 if (masterPageFile != null) {
863 Controls.Add (Master);
864 Master.FillPlaceHolders ();
868 renderingForm = false;
870 Trace.Write ("aspx.page", "Begin LoadViewState");
871 LoadPageViewState ();
872 Trace.Write ("aspx.page", "End LoadViewState");
873 Trace.Write ("aspx.page", "Begin ProcessPostData");
874 ProcessPostData (_requestValueCollection, false);
875 Trace.Write ("aspx.page", "End ProcessPostData");
879 if (IsCrossPagePostBack)
882 OnPreLoad (EventArgs.Empty);
887 Trace.Write ("aspx.page", "Begin ProcessPostData Second Try");
888 ProcessPostData (secondPostData, true);
889 Trace.Write ("aspx.page", "End ProcessPostData Second Try");
890 Trace.Write ("aspx.page", "Begin Raise ChangedEvents");
891 RaiseChangedEvents ();
892 Trace.Write ("aspx.page", "End Raise ChangedEvents");
893 Trace.Write ("aspx.page", "Begin Raise PostBackEvent");
894 RaisePostBackEvents ();
895 Trace.Write ("aspx.page", "End Raise PostBackEvent");
899 OnLoadComplete (EventArgs.Empty);
902 string result = ProcessCallbackData ();
903 HtmlTextWriter callbackOutput = new HtmlTextWriter (_context.Response.Output);
904 callbackOutput.Write (result);
905 callbackOutput.Flush ();
910 Trace.Write ("aspx.page", "Begin PreRender");
911 PreRenderRecursiveInternal ();
912 Trace.Write ("aspx.page", "End PreRender");
915 OnPreRenderComplete (EventArgs.Empty);
918 Trace.Write ("aspx.page", "Begin SaveViewState");
919 SavePageViewState ();
920 Trace.Write ("aspx.page", "End SaveViewState");
923 OnSaveStateComplete (EventArgs.Empty);
927 Trace.Write ("aspx.page", "Begin Render");
928 HtmlTextWriter output = new HtmlTextWriter (_context.Response.Output);
929 RenderControl (output);
930 Trace.Write ("aspx.page", "End Render");
932 RenderTrace (output);
935 private void RenderTrace (HtmlTextWriter output)
937 TraceManager traceManager = HttpRuntime.TraceManager;
939 if (Trace.HaveTrace && !Trace.IsEnabled || !Trace.HaveTrace && !traceManager.Enabled)
944 if (!Trace.HaveTrace && traceManager.Enabled && !traceManager.PageOutput)
947 if (!traceManager.LocalOnly || Context.Request.IsLocal)
948 Trace.Render (output);
951 internal void RaisePostBackEvents ()
953 if (requiresRaiseEvent != null) {
954 RaisePostBackEvent (requiresRaiseEvent, null);
958 NameValueCollection postdata = _requestValueCollection;
959 if (postdata == null)
962 string eventTarget = postdata [postEventSourceID];
963 if (eventTarget == null || eventTarget.Length == 0) {
968 IPostBackEventHandler target = FindControl (eventTarget) as IPostBackEventHandler;
972 string eventArgument = postdata [postEventArgumentID];
973 RaisePostBackEvent (target, eventArgument);
976 internal void RaiseChangedEvents ()
978 if (requiresPostDataChanged == null)
981 foreach (IPostBackDataHandler ipdh in requiresPostDataChanged)
982 ipdh.RaisePostDataChangedEvent ();
984 requiresPostDataChanged = null;
987 [EditorBrowsable (EditorBrowsableState.Advanced)]
988 protected virtual void RaisePostBackEvent (IPostBackEventHandler sourceControl, string eventArgument)
990 sourceControl.RaisePostBackEvent (eventArgument);
996 [EditorBrowsable (EditorBrowsableState.Advanced)]
997 public void RegisterArrayDeclaration (string arrayName, string arrayValue)
999 scriptManager.RegisterArrayDeclaration (arrayName, arrayValue);
1005 [EditorBrowsable (EditorBrowsableState.Advanced)]
1006 public virtual void RegisterClientScriptBlock (string key, string script)
1008 scriptManager.RegisterClientScriptBlock (key, script);
1014 [EditorBrowsable (EditorBrowsableState.Advanced)]
1015 public virtual void RegisterHiddenField (string hiddenFieldName, string hiddenFieldInitialValue)
1017 scriptManager.RegisterHiddenField (hiddenFieldName, hiddenFieldInitialValue);
1020 [MonoTODO("Used in HtmlForm")]
1021 internal void RegisterClientScriptFile (string a, string b, string c)
1023 throw new NotImplementedException ();
1029 [EditorBrowsable (EditorBrowsableState.Advanced)]
1030 public void RegisterOnSubmitStatement (string key, string script)
1032 scriptManager.RegisterOnSubmitStatement (key, script);
1035 [EditorBrowsable (EditorBrowsableState.Advanced)]
1036 public void RegisterRequiresPostBack (Control control)
1038 if (_requiresPostBack == null)
1039 _requiresPostBack = new ArrayList ();
1041 _requiresPostBack.Add (control.UniqueID);
1044 [EditorBrowsable (EditorBrowsableState.Advanced)]
1045 public virtual void RegisterRequiresRaiseEvent (IPostBackEventHandler control)
1047 requiresRaiseEvent = control;
1053 [EditorBrowsable (EditorBrowsableState.Advanced)]
1054 public virtual void RegisterStartupScript (string key, string script)
1056 scriptManager.RegisterStartupScript (key, script);
1059 [EditorBrowsable (EditorBrowsableState.Advanced)]
1060 public void RegisterViewStateHandler ()
1062 handleViewState = true;
1065 [EditorBrowsable (EditorBrowsableState.Advanced)]
1066 protected virtual void SavePageStateToPersistenceMedium (object viewState)
1068 _savedViewState = viewState;
1071 [EditorBrowsable (EditorBrowsableState.Advanced)]
1072 protected virtual object LoadPageStateFromPersistenceMedium ()
1074 NameValueCollection postdata = _requestValueCollection;
1076 if (postdata == null || (view_state = postdata ["__VIEWSTATE"]) == null)
1079 _savedViewState = null;
1080 LosFormatter fmt = new LosFormatter ();
1083 _savedViewState = fmt.Deserialize (view_state);
1084 } catch (Exception e) {
1085 throw new HttpException ("Error restoring page viewstate.\n", e);
1088 return _savedViewState;
1091 internal void LoadPageViewState()
1093 object sState = LoadPageStateFromPersistenceMedium ();
1094 if (sState != null) {
1096 Triplet data = (Triplet) sState;
1097 LoadPageControlState (data.Third);
1098 LoadViewStateRecursive (data.First);
1099 _requiresPostBack = data.Second as ArrayList;
1101 Pair pair = (Pair) sState;
1102 LoadViewStateRecursive (pair.First);
1103 _requiresPostBack = pair.Second as ArrayList;
1108 internal void SavePageViewState ()
1110 if (!handleViewState)
1114 object controlState = SavePageControlState ();
1117 object viewState = SaveViewStateRecursive ();
1118 object reqPostback = (_requiresPostBack != null && _requiresPostBack.Count > 0) ? _requiresPostBack : null;
1121 Triplet triplet = new Triplet ();
1122 triplet.First = viewState;
1123 triplet.Second = reqPostback;
1124 triplet.Third = controlState;
1126 if (triplet.First == null && triplet.Second == null && triplet.Third == null)
1129 SavePageStateToPersistenceMedium (triplet);
1131 Pair pair = new Pair ();
1132 pair.First = viewState;
1133 pair.Second = reqPostback;
1135 if (pair.First == null && pair.Second == null)
1138 SavePageStateToPersistenceMedium (pair);
1142 public virtual void Validate ()
1144 ValidateCollection (_validators);
1147 void ValidateCollection (ValidatorCollection validators)
1149 if (validators == null || validators.Count == 0){
1154 bool all_valid = true;
1155 foreach (IValidator v in validators){
1157 if (v.IsValid == false)
1165 [EditorBrowsable (EditorBrowsableState.Advanced)]
1166 public virtual void VerifyRenderingInServerForm (Control control)
1169 throw new HttpException ("Control '" + control.ClientID + " " + control.GetType () +
1170 "' must be rendered within a HtmlForm");
1180 ClientScriptManager ClientScript {
1181 get { return scriptManager; }
1186 static readonly object InitCompleteEvent = new object ();
1187 static readonly object LoadCompleteEvent = new object ();
1188 static readonly object PreInitEvent = new object ();
1189 static readonly object PreLoadEvent = new object ();
1190 static readonly object PreRenderCompleteEvent = new object ();
1191 static readonly object SaveStateCompleteEvent = new object ();
1193 public event EventHandler InitComplete {
1194 add { Events.AddHandler (InitCompleteEvent, value); }
1195 remove { Events.RemoveHandler (InitCompleteEvent, value); }
1198 public event EventHandler LoadComplete {
1199 add { Events.AddHandler (LoadCompleteEvent, value); }
1200 remove { Events.RemoveHandler (LoadCompleteEvent, value); }
1203 public event EventHandler PreInit {
1204 add { Events.AddHandler (PreInitEvent, value); }
1205 remove { Events.RemoveHandler (PreInitEvent, value); }
1208 public event EventHandler PreLoad {
1209 add { Events.AddHandler (PreLoadEvent, value); }
1210 remove { Events.RemoveHandler (PreLoadEvent, value); }
1213 public event EventHandler PreRenderComplete {
1214 add { Events.AddHandler (PreRenderCompleteEvent, value); }
1215 remove { Events.RemoveHandler (PreRenderCompleteEvent, value); }
1218 public event EventHandler SaveStateComplete {
1219 add { Events.AddHandler (SaveStateCompleteEvent, value); }
1220 remove { Events.RemoveHandler (SaveStateCompleteEvent, value); }
1223 protected virtual void OnInitComplete (EventArgs e)
1225 if (Events != null) {
1226 EventHandler eh = (EventHandler) (Events [InitCompleteEvent]);
1227 if (eh != null) eh (this, e);
1231 protected virtual void OnLoadComplete (EventArgs e)
1233 if (Events != null) {
1234 EventHandler eh = (EventHandler) (Events [LoadCompleteEvent]);
1235 if (eh != null) eh (this, e);
1239 protected virtual void OnPreInit (EventArgs e)
1241 if (Events != null) {
1242 EventHandler eh = (EventHandler) (Events [PreInitEvent]);
1243 if (eh != null) eh (this, e);
1247 protected virtual void OnPreLoad (EventArgs e)
1249 if (Events != null) {
1250 EventHandler eh = (EventHandler) (Events [PreLoadEvent]);
1251 if (eh != null) eh (this, e);
1255 protected virtual void OnPreRenderComplete (EventArgs e)
1257 if (Events != null) {
1258 EventHandler eh = (EventHandler) (Events [PreRenderCompleteEvent]);
1259 if (eh != null) eh (this, e);
1263 protected virtual void OnSaveStateComplete (EventArgs e)
1265 if (Events != null) {
1266 EventHandler eh = (EventHandler) (Events [SaveStateCompleteEvent]);
1267 if (eh != null) eh (this, e);
1271 public HtmlForm Form {
1272 get { return _form; }
1275 internal void RegisterForm (HtmlForm form)
1280 [BrowsableAttribute (false)]
1281 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
1282 public Page PreviousPage {
1283 get { return previousPage; }
1287 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
1288 [BrowsableAttribute (false)]
1289 public bool IsCallback {
1290 get { return _requestValueCollection != null && _requestValueCollection [CallbackArgumentID] != null; }
1293 [BrowsableAttribute (false)]
1294 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
1295 public bool IsCrossPagePostBack {
1296 get { return _requestValueCollection != null && isCrossPagePostBack; }
1299 string ProcessCallbackData ()
1301 string callbackTarget = _requestValueCollection [CallbackSourceID];
1302 if (callbackTarget == null || callbackTarget.Length == 0)
1303 throw new HttpException ("Callback target not provided.");
1305 ICallbackEventHandler target = FindControl (callbackTarget) as ICallbackEventHandler;
1307 throw new HttpException (string.Format ("Invalid callback target '{0}'.", callbackTarget));
1309 string callbackArgument = _requestValueCollection [CallbackArgumentID];
1310 return target.RaiseCallbackEvent (callbackArgument);
1313 [BrowsableAttribute (false)]
1314 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
1315 public IPageHeader Header {
1316 get { return htmlHeader; }
1319 internal void SetHeader (IPageHeader header)
1321 htmlHeader = header;
1324 [DefaultValueAttribute ("")]
1325 public string MasterPageFile {
1326 get { return masterPageFile; }
1327 set { masterPageFile = value; masterPage = null; }
1330 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
1331 [BrowsableAttribute (false)]
1332 public MasterPage Master {
1334 if (masterPage == null)
1335 masterPage = MasterPageParser.GetCompiledMasterInstance (masterPageFile, Server.MapPath (masterPageFile), Context);
1340 [EditorBrowsable (EditorBrowsableState.Advanced)]
1341 public void RegisterRequiresControlState (Control control)
1343 if (requireStateControls == null) requireStateControls = new ArrayList ();
1344 requireStateControls.Add (control);
1347 public bool RequiresControlState (Control control)
1349 if (requireStateControls == null) return false;
1350 return requireStateControls.Contains (control);
1353 [EditorBrowsable (EditorBrowsableState.Advanced)]
1354 public void UnregisterRequiresControlState (Control control)
1356 if (requireStateControls != null)
1357 requireStateControls.Remove (control);
1360 public ValidatorCollection GetValidators (string validationGroup)
1362 if (validationGroup == null || validationGroup == "")
1365 if (_validatorsByGroup == null) _validatorsByGroup = new Hashtable ();
1366 ValidatorCollection col = _validatorsByGroup [validationGroup] as ValidatorCollection;
1368 col = new ValidatorCollection ();
1369 _validatorsByGroup [validationGroup] = col;
1374 public virtual void Validate (string validationGroup)
1376 if (validationGroup == null || validationGroup == "")
1377 ValidateCollection (_validators);
1379 if (_validatorsByGroup != null) {
1380 ValidateCollection (_validatorsByGroup [validationGroup] as ValidatorCollection);
1387 object SavePageControlState ()
1389 if (requireStateControls == null) return null;
1390 object[] state = new object [requireStateControls.Count];
1392 bool allNull = true;
1393 for (int n=0; n<state.Length; n++) {
1394 state [n] = ((Control) requireStateControls [n]).SaveControlState ();
1395 if (state [n] != null) allNull = false;
1397 if (allNull) return null;
1401 void LoadPageControlState (object data)
1403 if (requireStateControls == null) return;
1405 object[] state = (object[]) data;
1406 int max = Math.Min (requireStateControls.Count, state != null ? state.Length : requireStateControls.Count);
1407 for (int n=0; n < max; n++) {
1408 Control ctl = (Control) requireStateControls [n];
1409 ctl.LoadControlState (state != null ? state [n] : null);
1413 void LoadPreviousPageReference ()
1415 if (_requestValueCollection != null) {
1416 string prevPage = _requestValueCollection [PreviousPageID];
1417 if (prevPage != null) {
1418 previousPage = (Page) PageParser.GetCompiledPageInstance (prevPage, Server.MapPath (prevPage), Context);
1419 previousPage.ProcessCrossPagePostBack (_context);
1421 previousPage = _context.LastPage;
1424 _context.LastPage = this;