Page.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  1. //
  2. // System.Web.UI.Page
  3. //
  4. // Authors:
  5. // Duncan Mak ([email protected])
  6. // Gonzalo Paniagua ([email protected])
  7. //
  8. // (C) 2002 Ximian, Inc. (http://www.ximian.com)
  9. //
  10. using System;
  11. using System.Collections;
  12. using System.Collections.Specialized;
  13. using System.IO;
  14. using System.Security.Principal;
  15. using System.Text;
  16. using System.Web;
  17. using System.Web.Caching;
  18. using System.Web.SessionState;
  19. using System.Web.Util;
  20. namespace System.Web.UI
  21. {
  22. public class Page : TemplateControl, IHttpHandler
  23. {
  24. private string _culture;
  25. private bool _viewState = true;
  26. private bool _viewStateMac = false;
  27. private string _errorPage;
  28. private string _ID;
  29. private bool _isValid;
  30. private bool _smartNavigation = false;
  31. private TraceContext _trace;
  32. private bool _traceEnabled;
  33. private TraceMode _traceModeValue;
  34. private int _transactionMode;
  35. private string _UICulture;
  36. private HttpContext _context;
  37. private ValidatorCollection _validators;
  38. private bool renderingForm;
  39. private object _savedViewState;
  40. private ArrayList _requiresPostBack;
  41. private ArrayList requiresPostDataChanged;
  42. private ArrayList requiresRaiseEvent;
  43. private NameValueCollection secondPostData;
  44. private bool requiresPostBackScript = false;
  45. private bool postBackScriptRendered = false;
  46. #region Fields
  47. protected const string postEventArgumentID = ""; //FIXME
  48. protected const string postEventSourceID = "";
  49. #endregion
  50. #region Constructor
  51. public Page ()
  52. {
  53. Page = this;
  54. }
  55. #endregion
  56. #region Properties
  57. public HttpApplicationState Application
  58. {
  59. get { return _context.Application; }
  60. }
  61. bool AspCompatMode
  62. {
  63. set { throw new NotImplementedException (); }
  64. }
  65. bool Buffer
  66. {
  67. set { Response.BufferOutput = value; }
  68. }
  69. public Cache Cache
  70. {
  71. get { return _context.Cache; }
  72. }
  73. [MonoTODO]
  74. public string ClientTarget
  75. {
  76. get { throw new NotImplementedException (); }
  77. set { throw new NotImplementedException (); }
  78. }
  79. [MonoTODO]
  80. int CodePage
  81. {
  82. set { throw new NotImplementedException (); }
  83. }
  84. [MonoTODO]
  85. string ContentType
  86. {
  87. set { throw new NotImplementedException (); }
  88. }
  89. protected override HttpContext Context
  90. {
  91. get { return _context; }
  92. }
  93. string Culture
  94. {
  95. set { _culture = value; }
  96. }
  97. public override bool EnableViewState
  98. {
  99. get { return _viewState; }
  100. set { _viewState = value; }
  101. }
  102. protected bool EnableViewStateMac
  103. {
  104. get { return _viewStateMac; }
  105. set { _viewStateMac = value; }
  106. }
  107. public string ErrorPage
  108. {
  109. get { return _errorPage; }
  110. set { _errorPage = value; }
  111. }
  112. protected ArrayList FileDependencies
  113. {
  114. set {
  115. if (Response != null)
  116. Response.AddFileDependencies (value);
  117. }
  118. }
  119. public override string ID
  120. {
  121. get { return _ID; }
  122. set { _ID = value; }
  123. }
  124. public bool IsPostBack
  125. {
  126. get {
  127. return (0 == String.Compare (Request.HttpMethod, "POST", true));
  128. }
  129. }
  130. [MonoTODO]
  131. public bool IsReusable
  132. {
  133. get { throw new NotImplementedException (); }
  134. }
  135. public bool IsValid
  136. {
  137. get { return _isValid; }
  138. }
  139. [MonoTODO]
  140. int LCID {
  141. set { throw new NotImplementedException (); }
  142. }
  143. public HttpRequest Request
  144. {
  145. get { return _context.Request; }
  146. }
  147. public HttpResponse Response
  148. {
  149. get { return _context.Response; }
  150. }
  151. string ResponseEncoding
  152. {
  153. set { Response.ContentEncoding = Encoding.GetEncoding (value); }
  154. }
  155. public HttpServerUtility Server
  156. {
  157. get {
  158. return Context.Server;
  159. }
  160. }
  161. public virtual HttpSessionState Session
  162. {
  163. get { return _context.Session; }
  164. }
  165. public bool SmartNavigation
  166. {
  167. get { return _smartNavigation; }
  168. set { _smartNavigation = value; }
  169. }
  170. public TraceContext Trace
  171. {
  172. get { return _trace; }
  173. }
  174. bool TraceEnabled
  175. {
  176. set { _traceEnabled = value; }
  177. }
  178. TraceMode TraceModeValue
  179. {
  180. set { _traceModeValue = value; }
  181. }
  182. int TransactionMode
  183. {
  184. set { _transactionMode = value; }
  185. }
  186. string UICulture
  187. {
  188. set { _UICulture = value; }
  189. }
  190. public IPrincipal User
  191. {
  192. get { return _context.User; }
  193. }
  194. public ValidatorCollection Validators
  195. {
  196. get {
  197. if (_validators == null)
  198. _validators = new ValidatorCollection ();
  199. return _validators;
  200. }
  201. }
  202. public override bool Visible
  203. {
  204. get { return base.Visible; }
  205. set { base.Visible = value; }
  206. }
  207. #endregion
  208. #region Methods
  209. [MonoTODO]
  210. protected IAsyncResult AspCompatBeginProcessRequest (HttpContext context,
  211. AsyncCallback cb,
  212. object extraData)
  213. {
  214. throw new NotImplementedException ();
  215. }
  216. [MonoTODO]
  217. protected void AspcompatEndProcessRequest (IAsyncResult result)
  218. {
  219. throw new NotImplementedException ();
  220. }
  221. protected virtual HtmlTextWriter CreateHtmlTextWriter (TextWriter tw)
  222. {
  223. return new HtmlTextWriter (tw);
  224. }
  225. [MonoTODO]
  226. public void DesignerInitialize ()
  227. {
  228. throw new NotImplementedException ();
  229. }
  230. protected virtual NameValueCollection DeterminePostBackMode ()
  231. {
  232. if (_context == null)
  233. return null;
  234. HttpRequest req = _context.Request;
  235. if (req == null)
  236. return null;
  237. NameValueCollection coll = null;
  238. if (IsPostBack)
  239. coll = _context.Request.Form;
  240. else
  241. coll = _context.Request.QueryString;
  242. if (coll == null || coll ["__VIEWSTATE"] == null)
  243. return null;
  244. return coll;
  245. }
  246. public string GetPostBackClientEvent (Control control, string argument)
  247. {
  248. return GetPostBackEventReference (control, argument);
  249. }
  250. public string GetPostBackClientHyperlink (Control control, string argument)
  251. {
  252. return "javascript:" + GetPostBackEventReference (control, argument);
  253. }
  254. public string GetPostBackEventReference (Control control)
  255. {
  256. return GetPostBackEventReference (control, "");
  257. }
  258. public string GetPostBackEventReference (Control control, string argument)
  259. {
  260. RequiresPostBackScript ();
  261. return String.Format ("__doPostBack ('{0}', '{1}')", control.ID, argument);
  262. }
  263. internal void RequiresPostBackScript ()
  264. {
  265. requiresPostBackScript = true;
  266. }
  267. public virtual int GetTypeHashCode ()
  268. {
  269. return GetHashCode ();
  270. }
  271. [MonoTODO]
  272. protected virtual void InitOutputCache (int duration,
  273. string varyByHeader,
  274. string varyByCustom,
  275. OutputCacheLocation location,
  276. string varyByParam)
  277. {
  278. throw new NotImplementedException ();
  279. }
  280. [MonoTODO]
  281. public bool IsClientScriptBlockRegistered (string key)
  282. {
  283. throw new NotImplementedException ();
  284. }
  285. [MonoTODO]
  286. public bool IsStartupScriptRegistered (string key)
  287. {
  288. throw new NotImplementedException ();
  289. }
  290. [MonoTODO]
  291. public string MapPath (string virtualPath)
  292. {
  293. throw new NotImplementedException ();
  294. }
  295. private void RenderPostBackScript (HtmlTextWriter writer, string formUniqueID)
  296. {
  297. writer.WriteLine ("<input type=\"hidden\" name=\"__EVENTTARGET\" value=\"\" />");
  298. writer.WriteLine ("<input type=\"hidden\" name=\"__EVENTARGUMENT\" value=\"\" />");
  299. writer.WriteLine ();
  300. writer.WriteLine ("<script language=\"javascript\">");
  301. writer.WriteLine ("<!--");
  302. writer.WriteLine ("\tfunction __doPostBack(eventTarget, eventArgument) {");
  303. writer.WriteLine ("\t\tvar theform = document.{0};", formUniqueID);
  304. writer.WriteLine ("\t\ttheform.__EVENTTARGET.value = eventTarget;");
  305. writer.WriteLine ("\t\ttheform.__EVENTARGUMENT.value = eventArgument;");
  306. writer.WriteLine ("\t\ttheform.submit();");
  307. writer.WriteLine ("\t}");
  308. writer.WriteLine ("// -->");
  309. writer.WriteLine ("</script>");
  310. }
  311. internal void OnFormRender (HtmlTextWriter writer, string formUniqueID)
  312. {
  313. if (renderingForm)
  314. throw new HttpException ("Only 1 HtmlForm is allowed per page.");
  315. renderingForm = true;
  316. writer.WriteLine ();
  317. writer.Write ("<input type=\"hidden\" name=\"__VIEWSTATE\" ");
  318. writer.WriteLine ("value=\"{0}\" />", GetViewStateString ());
  319. if (requiresPostBackScript) {
  320. RenderPostBackScript (writer, formUniqueID);
  321. postBackScriptRendered = true;
  322. }
  323. }
  324. internal string GetViewStateString ()
  325. {
  326. StringWriter sr = new StringWriter ();
  327. LosFormatter fmt = new LosFormatter ();
  328. fmt.Serialize (sr, _savedViewState);
  329. return sr.GetStringBuilder ().ToString ();
  330. }
  331. internal void OnFormPostRender (HtmlTextWriter writer, string formUniqueID)
  332. {
  333. if (!postBackScriptRendered && requiresPostBackScript)
  334. RenderPostBackScript (writer, formUniqueID);
  335. renderingForm = false;
  336. postBackScriptRendered = false;
  337. }
  338. private void ProcessPostData (NameValueCollection data, bool second)
  339. {
  340. if (data == null)
  341. return;
  342. foreach (string id in data.AllKeys){
  343. if (id == "__VIEWSTATE")
  344. continue;
  345. Control ctrl = FindControl (id);
  346. if (ctrl != null){
  347. IPostBackDataHandler pbdh = ctrl as IPostBackDataHandler;
  348. IPostBackEventHandler pbeh = ctrl as IPostBackEventHandler;
  349. if (pbdh == null) {
  350. if (pbeh != null)
  351. RegisterRequiresRaiseEvent (pbeh);
  352. continue;
  353. }
  354. if (pbdh.LoadPostData (id, data) == true) {
  355. if (requiresPostDataChanged == null)
  356. requiresPostDataChanged = new ArrayList ();
  357. requiresPostDataChanged.Add (pbdh);
  358. }
  359. } else if (!second) {
  360. if (secondPostData == null)
  361. secondPostData = new NameValueCollection ();
  362. secondPostData.Add (id, null);
  363. }
  364. }
  365. }
  366. public void ProcessRequest (HttpContext context)
  367. {
  368. _context = context;
  369. WebTrace.PushContext ("Page.ProcessRequest ()");
  370. WebTrace.WriteLine ("Entering");
  371. WireupAutomaticEvents ();
  372. WebTrace.WriteLine ("Finished hookup");
  373. //-- Control execution lifecycle in the docs
  374. WebTrace.WriteLine ("Controls.Clear");
  375. Controls.Clear ();
  376. WebTrace.WriteLine ("FrameworkInitialize");
  377. FrameworkInitialize ();
  378. WebTrace.WriteLine ("InitRecursive");
  379. InitRecursive (null);
  380. renderingForm = false;
  381. _context = context;
  382. if (IsPostBack) {
  383. LoadPageViewState ();
  384. ProcessPostData (DeterminePostBackMode (), false);
  385. }
  386. WebTrace.WriteLine ("LoadRecursive");
  387. LoadRecursive ();
  388. if (IsPostBack) {
  389. ProcessPostData (secondPostData, true);
  390. RaiseChangedEvents ();
  391. RaisePostBackEvents ();
  392. }
  393. WebTrace.WriteLine ("PreRenderRecursiveInternal");
  394. PreRenderRecursiveInternal ();
  395. WebTrace.WriteLine ("SavePageViewState");
  396. SavePageViewState ();
  397. //--
  398. StringBuilder sb = new StringBuilder ();
  399. StringWriter sr = new StringWriter (sb);
  400. HtmlTextWriter output = new HtmlTextWriter (context.Response.Output);
  401. WebTrace.WriteLine ("RenderControl");
  402. RenderControl (output);
  403. _context = null;
  404. WebTrace.WriteLine ("UnloadRecursive");
  405. UnloadRecursive (true);
  406. WebTrace.WriteLine ("End");
  407. WebTrace.PopContext ();
  408. }
  409. internal void RaisePostBackEvents ()
  410. {
  411. NameValueCollection postdata = DeterminePostBackMode ();
  412. if (postdata == null)
  413. return;
  414. string eventTarget = postdata ["__EVENTTARGET"];
  415. if (eventTarget != null && eventTarget.Length > 0) {
  416. Control target = FindControl (eventTarget);
  417. if (!(target is IPostBackEventHandler))
  418. return;
  419. string eventArgument = postdata ["__EVENTARGUMENT"];
  420. RaisePostBackEvent ((IPostBackEventHandler) target, eventArgument);
  421. return;
  422. }
  423. if (requiresRaiseEvent == null)
  424. return;
  425. foreach (Control c in requiresRaiseEvent)
  426. RaisePostBackEvent ((IPostBackEventHandler) c, postdata [c.ID]);
  427. requiresRaiseEvent.Clear ();
  428. }
  429. internal void RaiseChangedEvents ()
  430. {
  431. if (requiresPostDataChanged == null)
  432. return;
  433. foreach (IPostBackDataHandler ipdh in requiresPostDataChanged)
  434. ipdh.RaisePostDataChangedEvent ();
  435. requiresPostDataChanged.Clear ();
  436. }
  437. protected virtual void RaisePostBackEvent (IPostBackEventHandler sourceControl, string eventArgument)
  438. {
  439. sourceControl.RaisePostBackEvent (eventArgument);
  440. }
  441. [MonoTODO]
  442. public void RegisterArrayDeclaration (string arrayName, string arrayValue)
  443. {
  444. throw new NotImplementedException ();
  445. }
  446. [MonoTODO]
  447. public virtual void RegisterClientScriptBlock (string key, string script)
  448. {
  449. throw new NotImplementedException ();
  450. }
  451. [MonoTODO]
  452. public virtual void RegisterHiddenField (string hiddenFieldName, string hiddenFieldInitialValue)
  453. {
  454. throw new NotImplementedException ();
  455. }
  456. [MonoTODO]
  457. public void RegisterClientScriptFile (string a, string b, string c)
  458. {
  459. throw new NotImplementedException ();
  460. }
  461. [MonoTODO]
  462. public void RegisterOnSubmitStatement (string key, string script)
  463. {
  464. throw new NotImplementedException ();
  465. }
  466. public void RegisterRequiresPostBack (Control control)
  467. {
  468. if (_requiresPostBack == null)
  469. _requiresPostBack = new ArrayList ();
  470. _requiresPostBack.Add (control);
  471. }
  472. public virtual void RegisterRequiresRaiseEvent (IPostBackEventHandler control)
  473. {
  474. if (requiresRaiseEvent == null)
  475. requiresRaiseEvent = new ArrayList ();
  476. requiresRaiseEvent.Add (control);
  477. }
  478. [MonoTODO]
  479. public void RegisterViewStateHandler ()
  480. {
  481. // Do nothing
  482. }
  483. protected virtual void SavePageStateToPersistenceMedium (object viewState)
  484. {
  485. _savedViewState = viewState;
  486. }
  487. protected virtual object LoadPageStateFromPersistenceMedium ()
  488. {
  489. NameValueCollection postdata = DeterminePostBackMode ();
  490. string view_state;
  491. if (postdata == null || (view_state = postdata ["__VIEWSTATE"]) == null)
  492. return null;
  493. _savedViewState = null;
  494. LosFormatter fmt = new LosFormatter ();
  495. try {
  496. _savedViewState = fmt.Deserialize (view_state);
  497. } catch {
  498. throw new HttpException ("Error restoring page viewstate.");
  499. }
  500. return _savedViewState;
  501. }
  502. internal void LoadPageViewState()
  503. {
  504. WebTrace.PushContext ("LoadPageViewState");
  505. object sState = LoadPageStateFromPersistenceMedium ();
  506. WebTrace.WriteLine ("sState = '{0}'", sState);
  507. if (sState != null) {
  508. Pair pair = (Pair) sState;
  509. LoadViewStateRecursive (pair.First);
  510. _requiresPostBack = pair.Second as ArrayList;
  511. }
  512. WebTrace.PopContext ();
  513. }
  514. internal void SavePageViewState ()
  515. {
  516. Pair pair = new Pair ();
  517. pair.First = SaveViewStateRecursive ();
  518. pair.Second = _requiresPostBack;
  519. SavePageStateToPersistenceMedium (pair);
  520. }
  521. public virtual void Validate ()
  522. {
  523. if (_validators == null || _validators.Count == 0){
  524. _isValid = true;
  525. return;
  526. }
  527. bool all_valid = true;
  528. foreach (IValidator v in _validators){
  529. v.Validate ();
  530. if (v.IsValid == false)
  531. all_valid = false;
  532. }
  533. if (all_valid)
  534. _isValid = true;
  535. }
  536. public virtual void VerifyRenderingInServerForm (Control control)
  537. {
  538. if (!renderingForm)
  539. throw new HttpException ("Control '" + control.ClientID + " " + control.GetType () +
  540. "' must be rendered within a HtmlForm");
  541. }
  542. #endregion
  543. }
  544. }