Page.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  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 = 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 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. public string MapPath (string virtualPath)
  291. {
  292. return Request.MapPath (virtualPath);
  293. }
  294. private void RenderPostBackScript (HtmlTextWriter writer, string formUniqueID)
  295. {
  296. writer.WriteLine ("<input type=\"hidden\" name=\"__EVENTTARGET\" value=\"\" />");
  297. writer.WriteLine ("<input type=\"hidden\" name=\"__EVENTARGUMENT\" value=\"\" />");
  298. writer.WriteLine ();
  299. writer.WriteLine ("<script language=\"javascript\">");
  300. writer.WriteLine ("<!--");
  301. writer.WriteLine ("\tfunction __doPostBack(eventTarget, eventArgument) {");
  302. writer.WriteLine ("\t\tvar theform = document.{0};", formUniqueID);
  303. writer.WriteLine ("\t\ttheform.__EVENTTARGET.value = eventTarget;");
  304. writer.WriteLine ("\t\ttheform.__EVENTARGUMENT.value = eventArgument;");
  305. writer.WriteLine ("\t\ttheform.submit();");
  306. writer.WriteLine ("\t}");
  307. writer.WriteLine ("// -->");
  308. writer.WriteLine ("</script>");
  309. }
  310. internal void OnFormRender (HtmlTextWriter writer, string formUniqueID)
  311. {
  312. if (renderingForm)
  313. throw new HttpException ("Only 1 HtmlForm is allowed per page.");
  314. renderingForm = true;
  315. writer.WriteLine ();
  316. writer.Write ("<input type=\"hidden\" name=\"__VIEWSTATE\" ");
  317. writer.WriteLine ("value=\"{0}\" />", GetViewStateString ());
  318. if (requiresPostBackScript) {
  319. RenderPostBackScript (writer, formUniqueID);
  320. postBackScriptRendered = true;
  321. }
  322. }
  323. internal string GetViewStateString ()
  324. {
  325. StringWriter sr = new StringWriter ();
  326. LosFormatter fmt = new LosFormatter ();
  327. fmt.Serialize (sr, _savedViewState);
  328. return sr.GetStringBuilder ().ToString ();
  329. }
  330. internal void OnFormPostRender (HtmlTextWriter writer, string formUniqueID)
  331. {
  332. if (!postBackScriptRendered && requiresPostBackScript)
  333. RenderPostBackScript (writer, formUniqueID);
  334. renderingForm = false;
  335. postBackScriptRendered = false;
  336. }
  337. private void ProcessPostData (NameValueCollection data, bool second)
  338. {
  339. if (data == null)
  340. return;
  341. Hashtable used = new Hashtable ();
  342. foreach (string id in data.AllKeys){
  343. if (id == "__VIEWSTATE" || id == "__EVENTTARGET" || id == "__EVENTARGUMENT")
  344. continue;
  345. string real_id = id;
  346. int dot = real_id.IndexOf ('.');
  347. if (dot >= 1)
  348. real_id = real_id.Substring (0, dot);
  349. if (real_id == null || used.ContainsKey (real_id))
  350. continue;
  351. used.Add (real_id, real_id);
  352. Control ctrl = FindControl (real_id);
  353. if (ctrl != null){
  354. IPostBackDataHandler pbdh = ctrl as IPostBackDataHandler;
  355. IPostBackEventHandler pbeh = ctrl as IPostBackEventHandler;
  356. if (pbdh == null) {
  357. if (pbeh != null)
  358. RegisterRequiresRaiseEvent (pbeh);
  359. continue;
  360. }
  361. if (pbdh.LoadPostData (real_id, data) == true) {
  362. if (requiresPostDataChanged == null)
  363. requiresPostDataChanged = new ArrayList ();
  364. requiresPostDataChanged.Add (pbdh);
  365. }
  366. } else if (!second) {
  367. if (secondPostData == null)
  368. secondPostData = new NameValueCollection ();
  369. secondPostData.Add (real_id, null);
  370. }
  371. }
  372. }
  373. public void ProcessRequest (HttpContext context)
  374. {
  375. _context = context;
  376. WebTrace.PushContext ("Page.ProcessRequest ()");
  377. WebTrace.WriteLine ("Entering");
  378. WireupAutomaticEvents ();
  379. WebTrace.WriteLine ("Finished hookup");
  380. //-- Control execution lifecycle in the docs
  381. WebTrace.WriteLine ("FrameworkInitialize");
  382. FrameworkInitialize ();
  383. WebTrace.WriteLine ("InitRecursive");
  384. InitRecursive (null);
  385. renderingForm = false;
  386. if (IsPostBack) {
  387. LoadPageViewState ();
  388. ProcessPostData (DeterminePostBackMode (), false);
  389. }
  390. WebTrace.WriteLine ("LoadRecursive");
  391. LoadRecursive ();
  392. if (IsPostBack) {
  393. ProcessPostData (secondPostData, true);
  394. RaiseChangedEvents ();
  395. RaisePostBackEvents ();
  396. }
  397. WebTrace.WriteLine ("PreRenderRecursiveInternal");
  398. PreRenderRecursiveInternal ();
  399. WebTrace.WriteLine ("SavePageViewState");
  400. SavePageViewState ();
  401. //--
  402. StringBuilder sb = new StringBuilder ();
  403. StringWriter sr = new StringWriter (sb);
  404. HtmlTextWriter output = new HtmlTextWriter (context.Response.Output);
  405. WebTrace.WriteLine ("RenderControl");
  406. RenderControl (output);
  407. _context = null;
  408. WebTrace.WriteLine ("UnloadRecursive");
  409. UnloadRecursive (true);
  410. WebTrace.WriteLine ("End");
  411. WebTrace.PopContext ();
  412. }
  413. internal void RaisePostBackEvents ()
  414. {
  415. NameValueCollection postdata = DeterminePostBackMode ();
  416. if (postdata == null)
  417. return;
  418. string eventTarget = postdata ["__EVENTTARGET"];
  419. if (eventTarget != null && eventTarget.Length > 0) {
  420. Control target = FindControl (eventTarget);
  421. if (!(target is IPostBackEventHandler))
  422. return;
  423. string eventArgument = postdata ["__EVENTARGUMENT"];
  424. RaisePostBackEvent ((IPostBackEventHandler) target, eventArgument);
  425. return;
  426. }
  427. if (requiresRaiseEvent == null)
  428. return;
  429. foreach (Control c in requiresRaiseEvent)
  430. RaisePostBackEvent ((IPostBackEventHandler) c, postdata [c.ID]);
  431. requiresRaiseEvent.Clear ();
  432. }
  433. internal void RaiseChangedEvents ()
  434. {
  435. if (requiresPostDataChanged == null)
  436. return;
  437. foreach (IPostBackDataHandler ipdh in requiresPostDataChanged)
  438. ipdh.RaisePostDataChangedEvent ();
  439. requiresPostDataChanged.Clear ();
  440. }
  441. protected virtual void RaisePostBackEvent (IPostBackEventHandler sourceControl, string eventArgument)
  442. {
  443. sourceControl.RaisePostBackEvent (eventArgument);
  444. }
  445. [MonoTODO]
  446. public void RegisterArrayDeclaration (string arrayName, string arrayValue)
  447. {
  448. throw new NotImplementedException ();
  449. }
  450. [MonoTODO]
  451. public virtual void RegisterClientScriptBlock (string key, string script)
  452. {
  453. throw new NotImplementedException ();
  454. }
  455. [MonoTODO]
  456. public virtual void RegisterHiddenField (string hiddenFieldName, string hiddenFieldInitialValue)
  457. {
  458. throw new NotImplementedException ();
  459. }
  460. [MonoTODO]
  461. public void RegisterClientScriptFile (string a, string b, string c)
  462. {
  463. throw new NotImplementedException ();
  464. }
  465. [MonoTODO]
  466. public void RegisterOnSubmitStatement (string key, string script)
  467. {
  468. throw new NotImplementedException ();
  469. }
  470. public void RegisterRequiresPostBack (Control control)
  471. {
  472. if (_requiresPostBack == null)
  473. _requiresPostBack = new ArrayList ();
  474. _requiresPostBack.Add (control.ID);
  475. }
  476. public virtual void RegisterRequiresRaiseEvent (IPostBackEventHandler control)
  477. {
  478. if (requiresRaiseEvent == null)
  479. requiresRaiseEvent = new ArrayList ();
  480. requiresRaiseEvent.Add (control);
  481. }
  482. [MonoTODO]
  483. public void RegisterViewStateHandler ()
  484. {
  485. // Do nothing
  486. }
  487. protected virtual void SavePageStateToPersistenceMedium (object viewState)
  488. {
  489. _savedViewState = viewState;
  490. }
  491. protected virtual object LoadPageStateFromPersistenceMedium ()
  492. {
  493. NameValueCollection postdata = DeterminePostBackMode ();
  494. string view_state;
  495. if (postdata == null || (view_state = postdata ["__VIEWSTATE"]) == null)
  496. return null;
  497. _savedViewState = null;
  498. LosFormatter fmt = new LosFormatter ();
  499. try {
  500. _savedViewState = fmt.Deserialize (view_state);
  501. } catch (Exception e) {
  502. throw new HttpException ("Error restoring page viewstate.\n{0}", e);
  503. }
  504. return _savedViewState;
  505. }
  506. internal void LoadPageViewState()
  507. {
  508. WebTrace.PushContext ("LoadPageViewState");
  509. object sState = LoadPageStateFromPersistenceMedium ();
  510. WebTrace.WriteLine ("sState = '{0}'", sState);
  511. if (sState != null) {
  512. Pair pair = (Pair) sState;
  513. LoadViewStateRecursive (pair.First);
  514. _requiresPostBack = pair.Second as ArrayList;
  515. }
  516. WebTrace.PopContext ();
  517. }
  518. internal void SavePageViewState ()
  519. {
  520. Pair pair = new Pair ();
  521. pair.First = SaveViewStateRecursive ();
  522. if (_requiresPostBack != null && _requiresPostBack.Count > 0)
  523. pair.Second = _requiresPostBack;
  524. SavePageStateToPersistenceMedium (pair);
  525. }
  526. public virtual void Validate ()
  527. {
  528. if (_validators == null || _validators.Count == 0){
  529. _isValid = true;
  530. return;
  531. }
  532. bool all_valid = true;
  533. foreach (IValidator v in _validators){
  534. v.Validate ();
  535. if (v.IsValid == false)
  536. all_valid = false;
  537. }
  538. if (all_valid)
  539. _isValid = true;
  540. }
  541. public virtual void VerifyRenderingInServerForm (Control control)
  542. {
  543. if (!renderingForm)
  544. throw new HttpException ("Control '" + control.ClientID + " " + control.GetType () +
  545. "' must be rendered within a HtmlForm");
  546. }
  547. #endregion
  548. }
  549. }