Page.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706
  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 IPostBackEventHandler requiresRaiseEvent;
  43. private NameValueCollection secondPostData;
  44. private bool requiresPostBackScript = false;
  45. private bool postBackScriptRendered = false;
  46. Hashtable clientScriptBlocks;
  47. Hashtable startupScriptBlocks;
  48. Hashtable hiddenFields;
  49. bool handleViewState;
  50. protected const string postEventArgumentID = "__EVENTARGUMENT";
  51. protected const string postEventSourceID = "__EVENTTARGET";
  52. #region Constructor
  53. public Page ()
  54. {
  55. Page = this;
  56. }
  57. #endregion
  58. #region Properties
  59. public HttpApplicationState Application
  60. {
  61. get { return _context.Application; }
  62. }
  63. bool AspCompatMode
  64. {
  65. set { throw new NotImplementedException (); }
  66. }
  67. bool Buffer
  68. {
  69. set { Response.BufferOutput = value; }
  70. }
  71. public Cache Cache
  72. {
  73. get { return _context.Cache; }
  74. }
  75. [MonoTODO]
  76. public string ClientTarget
  77. {
  78. get { throw new NotImplementedException (); }
  79. set { throw new NotImplementedException (); }
  80. }
  81. [MonoTODO]
  82. int CodePage
  83. {
  84. set { throw new NotImplementedException (); }
  85. }
  86. [MonoTODO]
  87. string ContentType
  88. {
  89. set { throw new NotImplementedException (); }
  90. }
  91. protected override HttpContext Context
  92. {
  93. get { return _context; }
  94. }
  95. string Culture
  96. {
  97. set { _culture = value; }
  98. }
  99. public override bool EnableViewState
  100. {
  101. get { return _viewState; }
  102. set { _viewState = value; }
  103. }
  104. protected bool EnableViewStateMac
  105. {
  106. get { return _viewStateMac; }
  107. set { _viewStateMac = value; }
  108. }
  109. public string ErrorPage
  110. {
  111. get { return _errorPage; }
  112. set { _errorPage = value; }
  113. }
  114. protected ArrayList FileDependencies
  115. {
  116. set {
  117. if (Response != null)
  118. Response.AddFileDependencies (value);
  119. }
  120. }
  121. public override string ID
  122. {
  123. get { return _ID; }
  124. set { _ID = value; }
  125. }
  126. public bool IsPostBack
  127. {
  128. get {
  129. return (0 == String.Compare (Request.HttpMethod, "POST", true));
  130. }
  131. }
  132. public bool IsReusable {
  133. get { return false; }
  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 = req.Form;
  240. else
  241. coll = req.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.UniqueID, argument);
  262. }
  263. internal void RequiresPostBackScript ()
  264. {
  265. requiresPostBackScript = true;
  266. }
  267. public virtual int GetTypeHashCode ()
  268. {
  269. return 0;
  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. public bool IsClientScriptBlockRegistered (string key)
  281. {
  282. if (clientScriptBlocks == null)
  283. return false;
  284. return clientScriptBlocks.ContainsKey (key);
  285. }
  286. public bool IsStartupScriptRegistered (string key)
  287. {
  288. if (startupScriptBlocks == null)
  289. return false;
  290. return startupScriptBlocks.ContainsKey (key);
  291. }
  292. public string MapPath (string virtualPath)
  293. {
  294. return Request.MapPath (virtualPath);
  295. }
  296. private void RenderPostBackScript (HtmlTextWriter writer, string formUniqueID)
  297. {
  298. writer.WriteLine ("<input type=\"hidden\" name=\"{0}\" value=\"\" />", postEventSourceID);
  299. writer.WriteLine ("<input type=\"hidden\" name=\"{0}\" value=\"\" />", postEventArgumentID);
  300. writer.WriteLine ();
  301. writer.WriteLine ("<script language=\"javascript\">");
  302. writer.WriteLine ("<!--");
  303. writer.WriteLine ("\tfunction __doPostBack(eventTarget, eventArgument) {");
  304. writer.WriteLine ("\t\tvar theform = document.{0};", formUniqueID);
  305. writer.WriteLine ("\t\ttheform.{0}.value = eventTarget;", postEventSourceID);
  306. writer.WriteLine ("\t\ttheform.{0}.value = eventArgument;", postEventArgumentID);
  307. writer.WriteLine ("\t\ttheform.submit();");
  308. writer.WriteLine ("\t}");
  309. writer.WriteLine ("// -->");
  310. writer.WriteLine ("</script>");
  311. }
  312. static void WriteScripts (HtmlTextWriter writer, Hashtable scripts)
  313. {
  314. if (scripts == null)
  315. return;
  316. foreach (string key in scripts.Values)
  317. writer.WriteLine (key);
  318. }
  319. void WriteHiddenFields (HtmlTextWriter writer)
  320. {
  321. if (hiddenFields == null)
  322. return;
  323. foreach (string key in hiddenFields.Keys) {
  324. string value = hiddenFields [key] as string;
  325. writer.WriteLine ("\n<input type=\"hidden\" name=\"{0}\" value=\"{1}\" />", key, value);
  326. }
  327. hiddenFields = null;
  328. }
  329. internal void OnFormRender (HtmlTextWriter writer, string formUniqueID)
  330. {
  331. if (renderingForm)
  332. throw new HttpException ("Only 1 HtmlForm is allowed per page.");
  333. renderingForm = true;
  334. writer.WriteLine ();
  335. WriteHiddenFields (writer);
  336. if (requiresPostBackScript) {
  337. RenderPostBackScript (writer, formUniqueID);
  338. postBackScriptRendered = true;
  339. }
  340. if (handleViewState) {
  341. writer.Write ("<input type=\"hidden\" name=\"__VIEWSTATE\" ");
  342. writer.WriteLine ("value=\"{0}\" />", GetViewStateString ());
  343. }
  344. WriteScripts (writer, clientScriptBlocks);
  345. }
  346. internal string GetViewStateString ()
  347. {
  348. StringWriter sr = new StringWriter ();
  349. LosFormatter fmt = new LosFormatter ();
  350. fmt.Serialize (sr, _savedViewState);
  351. return sr.GetStringBuilder ().ToString ();
  352. }
  353. internal void OnFormPostRender (HtmlTextWriter writer, string formUniqueID)
  354. {
  355. if (!postBackScriptRendered && requiresPostBackScript)
  356. RenderPostBackScript (writer, formUniqueID);
  357. WriteHiddenFields (writer);
  358. WriteScripts (writer, startupScriptBlocks);
  359. renderingForm = false;
  360. postBackScriptRendered = false;
  361. }
  362. private void ProcessPostData (NameValueCollection data, bool second)
  363. {
  364. if (data == null)
  365. return;
  366. Hashtable used = new Hashtable ();
  367. foreach (string id in data.AllKeys){
  368. if (id == "__VIEWSTATE" || id == postEventSourceID || id == postEventArgumentID)
  369. continue;
  370. string real_id = id;
  371. int dot = real_id.IndexOf ('.');
  372. if (dot >= 1)
  373. real_id = real_id.Substring (0, dot);
  374. if (real_id == null || used.ContainsKey (real_id))
  375. continue;
  376. used.Add (real_id, real_id);
  377. Control ctrl = FindControl (real_id);
  378. if (ctrl != null){
  379. IPostBackDataHandler pbdh = ctrl as IPostBackDataHandler;
  380. IPostBackEventHandler pbeh = ctrl as IPostBackEventHandler;
  381. if (pbdh == null) {
  382. if (pbeh != null)
  383. RegisterRequiresRaiseEvent (pbeh);
  384. continue;
  385. }
  386. if (pbdh.LoadPostData (real_id, data) == true) {
  387. if (requiresPostDataChanged == null)
  388. requiresPostDataChanged = new ArrayList ();
  389. requiresPostDataChanged.Add (pbdh);
  390. }
  391. } else if (!second) {
  392. if (secondPostData == null)
  393. secondPostData = new NameValueCollection ();
  394. secondPostData.Add (real_id, null);
  395. }
  396. }
  397. }
  398. public void ProcessRequest (HttpContext context)
  399. {
  400. _context = context;
  401. WebTrace.PushContext ("Page.ProcessRequest ()");
  402. WebTrace.WriteLine ("Entering");
  403. WireupAutomaticEvents ();
  404. WebTrace.WriteLine ("Finished hookup");
  405. //-- Control execution lifecycle in the docs
  406. WebTrace.WriteLine ("FrameworkInitialize");
  407. FrameworkInitialize ();
  408. WebTrace.WriteLine ("InitRecursive");
  409. InitRecursive (null);
  410. renderingForm = false;
  411. if (IsPostBack) {
  412. LoadPageViewState ();
  413. ProcessPostData (DeterminePostBackMode (), false);
  414. }
  415. WebTrace.WriteLine ("LoadRecursive");
  416. LoadRecursive ();
  417. if (IsPostBack) {
  418. ProcessPostData (secondPostData, true);
  419. RaiseChangedEvents ();
  420. RaisePostBackEvents ();
  421. }
  422. WebTrace.WriteLine ("PreRenderRecursiveInternal");
  423. PreRenderRecursiveInternal ();
  424. WebTrace.WriteLine ("SavePageViewState");
  425. SavePageViewState ();
  426. //--
  427. HtmlTextWriter output = new HtmlTextWriter (context.Response.Output);
  428. WebTrace.WriteLine ("RenderControl");
  429. RenderControl (output);
  430. _context = null;
  431. WebTrace.WriteLine ("UnloadRecursive");
  432. UnloadRecursive (true);
  433. WebTrace.WriteLine ("End");
  434. WebTrace.PopContext ();
  435. }
  436. internal void RaisePostBackEvents ()
  437. {
  438. if (requiresRaiseEvent != null) {
  439. RaisePostBackEvent (requiresRaiseEvent, null);
  440. return;
  441. }
  442. NameValueCollection postdata = DeterminePostBackMode ();
  443. if (postdata == null)
  444. return;
  445. string eventTarget = postdata [postEventSourceID];
  446. if (eventTarget == null || eventTarget.Length == 0)
  447. return;
  448. IPostBackEventHandler target = FindControl (eventTarget) as IPostBackEventHandler;
  449. if (target == null)
  450. return;
  451. string eventArgument = postdata [postEventArgumentID];
  452. RaisePostBackEvent (target, eventArgument);
  453. }
  454. internal void RaiseChangedEvents ()
  455. {
  456. if (requiresPostDataChanged == null)
  457. return;
  458. foreach (IPostBackDataHandler ipdh in requiresPostDataChanged)
  459. ipdh.RaisePostDataChangedEvent ();
  460. requiresPostDataChanged.Clear ();
  461. }
  462. protected virtual void RaisePostBackEvent (IPostBackEventHandler sourceControl, string eventArgument)
  463. {
  464. sourceControl.RaisePostBackEvent (eventArgument);
  465. }
  466. [MonoTODO]
  467. public void RegisterArrayDeclaration (string arrayName, string arrayValue)
  468. {
  469. throw new NotImplementedException ();
  470. }
  471. public virtual void RegisterClientScriptBlock (string key, string script)
  472. {
  473. if (IsClientScriptBlockRegistered (key))
  474. return;
  475. if (clientScriptBlocks == null)
  476. clientScriptBlocks = new Hashtable ();
  477. clientScriptBlocks.Add (key, script);
  478. }
  479. public virtual void RegisterHiddenField (string hiddenFieldName, string hiddenFieldInitialValue)
  480. {
  481. if (hiddenFields == null)
  482. hiddenFields = new Hashtable ();
  483. if (!hiddenFields.ContainsKey (hiddenFieldName))
  484. hiddenFields.Add (hiddenFieldName, hiddenFieldInitialValue);
  485. }
  486. [MonoTODO]
  487. public void RegisterClientScriptFile (string a, string b, string c)
  488. {
  489. throw new NotImplementedException ();
  490. }
  491. [MonoTODO]
  492. public void RegisterOnSubmitStatement (string key, string script)
  493. {
  494. throw new NotImplementedException ();
  495. }
  496. public void RegisterRequiresPostBack (Control control)
  497. {
  498. if (_requiresPostBack == null)
  499. _requiresPostBack = new ArrayList ();
  500. _requiresPostBack.Add (control.ID);
  501. }
  502. public virtual void RegisterRequiresRaiseEvent (IPostBackEventHandler control)
  503. {
  504. requiresRaiseEvent = control;
  505. }
  506. public virtual void RegisterStartupScript (string key, string script)
  507. {
  508. if (IsStartupScriptRegistered (key))
  509. return;
  510. if (startupScriptBlocks == null)
  511. startupScriptBlocks = new Hashtable ();
  512. startupScriptBlocks.Add (key, script);
  513. }
  514. public void RegisterViewStateHandler ()
  515. {
  516. handleViewState = true;
  517. }
  518. protected virtual void SavePageStateToPersistenceMedium (object viewState)
  519. {
  520. _savedViewState = viewState;
  521. }
  522. protected virtual object LoadPageStateFromPersistenceMedium ()
  523. {
  524. NameValueCollection postdata = DeterminePostBackMode ();
  525. string view_state;
  526. if (postdata == null || (view_state = postdata ["__VIEWSTATE"]) == null)
  527. return null;
  528. _savedViewState = null;
  529. LosFormatter fmt = new LosFormatter ();
  530. try {
  531. _savedViewState = fmt.Deserialize (view_state);
  532. } catch (Exception e) {
  533. throw new HttpException ("Error restoring page viewstate.\n{0}", e);
  534. }
  535. return _savedViewState;
  536. }
  537. internal void LoadPageViewState()
  538. {
  539. WebTrace.PushContext ("LoadPageViewState");
  540. object sState = LoadPageStateFromPersistenceMedium ();
  541. WebTrace.WriteLine ("sState = '{0}'", sState);
  542. if (sState != null) {
  543. Pair pair = (Pair) sState;
  544. LoadViewStateRecursive (pair.First);
  545. _requiresPostBack = pair.Second as ArrayList;
  546. }
  547. WebTrace.PopContext ();
  548. }
  549. internal void SavePageViewState ()
  550. {
  551. if (!handleViewState)
  552. return;
  553. Pair pair = new Pair ();
  554. pair.First = SaveViewStateRecursive ();
  555. if (_requiresPostBack != null && _requiresPostBack.Count > 0)
  556. pair.Second = _requiresPostBack;
  557. SavePageStateToPersistenceMedium (pair);
  558. }
  559. public virtual void Validate ()
  560. {
  561. if (_validators == null || _validators.Count == 0){
  562. _isValid = true;
  563. return;
  564. }
  565. bool all_valid = true;
  566. foreach (IValidator v in _validators){
  567. v.Validate ();
  568. if (v.IsValid == false)
  569. all_valid = false;
  570. }
  571. if (all_valid)
  572. _isValid = true;
  573. }
  574. public virtual void VerifyRenderingInServerForm (Control control)
  575. {
  576. if (!renderingForm)
  577. throw new HttpException ("Control '" + control.ClientID + " " + control.GetType () +
  578. "' must be rendered within a HtmlForm");
  579. }
  580. #endregion
  581. }
  582. }