Page.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  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.Reflection;
  15. using System.Security.Principal;
  16. using System.Text;
  17. using System.Web;
  18. using System.Web.Caching;
  19. using System.Web.SessionState;
  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 ArrayList _fileDependencies;
  29. private string _ID;
  30. private bool _isValid;
  31. private HttpServerUtility _server;
  32. private bool _smartNavigation = false;
  33. private TraceContext _trace;
  34. private bool _traceEnabled;
  35. private TraceMode _traceModeValue;
  36. private int _transactionMode;
  37. private string _UICulture;
  38. private HttpContext _context;
  39. private ValidatorCollection _validators;
  40. private bool _renderingForm;
  41. private bool _hasForm;
  42. private object _savedViewState;
  43. private ArrayList _requiresPostBack;
  44. private ArrayList requiresPostDataChanged;
  45. private ArrayList requiresRaiseEvent;
  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. ArrayList FileDependencies
  113. {
  114. set { _fileDependencies = value; }
  115. }
  116. public override string ID
  117. {
  118. get { return _ID; }
  119. set { _ID = value; }
  120. }
  121. public bool IsPostBack
  122. {
  123. get {
  124. return (Request.HttpMethod == "POST");
  125. }
  126. }
  127. [MonoTODO]
  128. public bool IsReusable
  129. {
  130. get { throw new NotImplementedException (); }
  131. }
  132. public bool IsValid
  133. {
  134. get { return _isValid; }
  135. }
  136. [MonoTODO]
  137. int LCID {
  138. set { throw new NotImplementedException (); }
  139. }
  140. public HttpRequest Request
  141. {
  142. get { return _context.Request; }
  143. }
  144. public HttpResponse Response
  145. {
  146. get { return _context.Response; }
  147. }
  148. string ResponseEncoding
  149. {
  150. set { Response.ContentEncoding = Encoding.GetEncoding (value); }
  151. }
  152. public HttpServerUtility Server
  153. {
  154. get { return _server; }
  155. }
  156. public virtual HttpSessionState Session
  157. {
  158. get { return _context.Session; }
  159. }
  160. public bool SmartNavigation
  161. {
  162. get { return _smartNavigation; }
  163. set { _smartNavigation = value; }
  164. }
  165. public TraceContext Trace
  166. {
  167. get { return _trace; }
  168. }
  169. bool TraceEnabled
  170. {
  171. set { _traceEnabled = value; }
  172. }
  173. TraceMode TraceModeValue
  174. {
  175. set { _traceModeValue = value; }
  176. }
  177. int TransactionMode
  178. {
  179. set { _transactionMode = value; }
  180. }
  181. string UICulture
  182. {
  183. set { _UICulture = value; }
  184. }
  185. public IPrincipal User
  186. {
  187. get { return _context.User; }
  188. }
  189. public ValidatorCollection Validators
  190. {
  191. get {
  192. if (_validators == null)
  193. _validators = new ValidatorCollection ();
  194. return _validators;
  195. }
  196. }
  197. public override bool Visible
  198. {
  199. get { return base.Visible; }
  200. set { base.Visible = value; }
  201. }
  202. #endregion
  203. #region Methods
  204. [MonoTODO]
  205. protected IAsyncResult AspCompatBeginProcessRequest (HttpContext context,
  206. AsyncCallback cb,
  207. object extraData)
  208. {
  209. throw new NotImplementedException ();
  210. }
  211. [MonoTODO]
  212. protected void AspcompatEndProcessRequest (IAsyncResult result)
  213. {
  214. throw new NotImplementedException ();
  215. }
  216. protected virtual HtmlTextWriter CreateHtmlTextWriter (TextWriter tw)
  217. {
  218. return new HtmlTextWriter (tw);
  219. }
  220. [MonoTODO]
  221. public void DesignerInitialize ()
  222. {
  223. throw new NotImplementedException ();
  224. }
  225. protected virtual NameValueCollection DeterminePostBackMode ()
  226. {
  227. if (IsPostBack)
  228. return _context.Request.Form;
  229. return _context.Request.QueryString;
  230. }
  231. [MonoTODO]
  232. public string GetPostBackClientEvent (Control control, string argument)
  233. {
  234. // Don't throw the exception. keep going
  235. //throw new NotImplementedException ();
  236. StringBuilder result = new StringBuilder ();
  237. result.AppendFormat ("GetPostBackClientEvent ('{0}', '{1}')", control.ID, argument);
  238. return result.ToString ();
  239. }
  240. [MonoTODO]
  241. public string GetPostBackClientHyperlink (Control control, string argument)
  242. {
  243. throw new NotImplementedException ();
  244. }
  245. [MonoTODO]
  246. public string GetPostBackEventReference (Control control)
  247. {
  248. // Don't throw the exception. keep going
  249. //throw new NotImplementedException ();
  250. return GetPostBackEventReference (control, "");
  251. }
  252. [MonoTODO]
  253. public string GetPostBackEventReference (Control control, string argument)
  254. {
  255. // Don't throw the exception. keep going
  256. //throw new NotImplementedException ();
  257. StringBuilder result = new StringBuilder ();
  258. result.AppendFormat ("GetPostBackEventReference ('{0}', '{1}')", control.ID, argument);
  259. return result.ToString ();
  260. }
  261. public virtual int GetTypeHashCode ()
  262. {
  263. return GetHashCode ();
  264. }
  265. [MonoTODO]
  266. protected virtual void InitOutputCache (int duration,
  267. string varyByHeader,
  268. string varyByCustom,
  269. OutputCacheLocation location,
  270. string varyByParam)
  271. {
  272. throw new NotImplementedException ();
  273. }
  274. [MonoTODO]
  275. public bool IsClientScriptBlockRegistered (string key)
  276. {
  277. throw new NotImplementedException ();
  278. }
  279. [MonoTODO]
  280. public bool IsStartupScriptRegistered (string key)
  281. {
  282. throw new NotImplementedException ();
  283. }
  284. [MonoTODO]
  285. public string MapPath (string virtualPath)
  286. {
  287. throw new NotImplementedException ();
  288. }
  289. private void InvokeEventMethod (string m_name, object sender, EventArgs e)
  290. {
  291. Type [] types = new Type [] {typeof (object), typeof (EventArgs)};
  292. MethodInfo evt_method = GetType ().GetMethod (m_name, types);
  293. if (evt_method != null){
  294. object [] parms = new object [2];
  295. parms [0] = sender;
  296. parms [1] = e;
  297. evt_method.Invoke (this, parms);
  298. }
  299. }
  300. private bool _got_state = false;
  301. private int _random;
  302. internal void OnFormRender (HtmlTextWriter writer, string formUniqueID)
  303. {
  304. if (_hasForm)
  305. throw new HttpException ("Only 1 HtmlForm is allowed per page.");
  306. _renderingForm = true;
  307. _hasForm = true;
  308. writer.WriteLine();
  309. writer.Write("<input type=\"hidden\" name=\"__VIEWSTATE\" ");
  310. writer.WriteLine("value=\"{0}\" />", GetViewStateString ());
  311. }
  312. public string GetViewStateString ()
  313. {
  314. StringBuilder state_string = new StringBuilder ();
  315. state_string.AppendFormat ("{0:X}", GetTypeHashCode ());
  316. if (_context != null)
  317. state_string.Append (_context.Request.QueryString.GetHashCode ());
  318. if (!_got_state) {
  319. Random rnd = new Random ();
  320. _random = rnd.Next ();
  321. if (_random < 0)
  322. _random = -_random;
  323. _random++;
  324. _got_state = true;
  325. }
  326. state_string.AppendFormat ("{0:X}", _random);
  327. return state_string.ToString ();
  328. }
  329. internal void OnFormPostRender (HtmlTextWriter writer, string formUniqueID)
  330. {
  331. _renderingForm = false;
  332. }
  333. private void _Page_Init (object sender, EventArgs e)
  334. {
  335. InvokeEventMethod ("Page_Init", sender, e);
  336. }
  337. private void _Page_Load (object sender, EventArgs e)
  338. {
  339. InvokeEventMethod ("Page_Load", sender, e);
  340. }
  341. private void ProcessPostData ()
  342. {
  343. NameValueCollection data = DeterminePostBackMode ();
  344. foreach (string id in data.AllKeys){
  345. if (id == "__VIEWSTATE")
  346. continue;
  347. Control ctrl = FindControl (id);
  348. if (ctrl != null){
  349. IPostBackDataHandler pbdh = ctrl as IPostBackDataHandler;
  350. if (pbdh != null) {
  351. if (pbdh.LoadPostData (id, data) == true) {
  352. if (requiresPostDataChanged == null)
  353. requiresPostDataChanged = new ArrayList ();
  354. requiresPostDataChanged.Add (ctrl);
  355. }
  356. }
  357. IPostBackEventHandler pbeh = ctrl as IPostBackEventHandler;
  358. if (pbeh != null) {
  359. if (requiresRaiseEvent == null)
  360. requiresRaiseEvent = new ArrayList ();
  361. requiresRaiseEvent.Add (ctrl);
  362. }
  363. }
  364. }
  365. }
  366. private bool init_done;
  367. public void ProcessRequest (HttpContext context)
  368. {
  369. if (!init_done){
  370. init_done = true;
  371. FrameworkInitialize ();
  372. // This 2 should depend on AutoEventWireUp in Page directive. Defaults to true.
  373. Init += new EventHandler (_Page_Init);
  374. Load += new EventHandler (_Page_Load);
  375. //-- Control execution lifecycle in the docs
  376. InitRecursive (null);
  377. }
  378. _got_state = false;
  379. _hasForm = false;
  380. _context = context;
  381. if (IsPostBack)
  382. LoadPageViewState ();
  383. //if (this is IPostBackDataHandler)
  384. // LoadPostData ();
  385. if (IsPostBack)
  386. ProcessPostData ();
  387. LoadRecursive ();
  388. if (IsPostBack) {
  389. RaiseChangedEvents ();
  390. RaisePostBackEvents ();
  391. }
  392. PreRenderRecursiveInternal ();
  393. SavePageViewState ();
  394. //--
  395. HtmlTextWriter output = new HtmlTextWriter (context.Response.Output);
  396. this.RenderControl (output);
  397. }
  398. internal void RaisePostBackEvents ()
  399. {
  400. if (requiresRaiseEvent == null)
  401. return;
  402. NameValueCollection postdata = DeterminePostBackMode ();
  403. foreach (Control c in requiresRaiseEvent)
  404. RaisePostBackEvent ((IPostBackEventHandler) c, postdata [c.ID]);
  405. requiresRaiseEvent.Clear ();
  406. }
  407. internal void RaiseChangedEvents ()
  408. {
  409. if (requiresPostDataChanged == null)
  410. return;
  411. foreach (IPostBackDataHandler ipdh in requiresPostDataChanged)
  412. ipdh.RaisePostDataChangedEvent ();
  413. requiresPostDataChanged.Clear ();
  414. }
  415. protected virtual void RaisePostBackEvent (IPostBackEventHandler sourceControl, string eventArgument)
  416. {
  417. sourceControl.RaisePostBackEvent (eventArgument);
  418. }
  419. [MonoTODO]
  420. public void RegisterArrayDeclaration (string arrayName, string arrayValue)
  421. {
  422. throw new NotImplementedException ();
  423. }
  424. [MonoTODO]
  425. public virtual void RegisterClientScriptBlock (string key, string script)
  426. {
  427. throw new NotImplementedException ();
  428. }
  429. [MonoTODO]
  430. public virtual void RegisterHiddenField (string hiddenFieldName, string hiddenFieldInitialValue)
  431. {
  432. throw new NotImplementedException ();
  433. }
  434. [MonoTODO]
  435. public void RegisterClientScriptFile (string a, string b, string c)
  436. {
  437. throw new NotImplementedException ();
  438. }
  439. [MonoTODO]
  440. public void RegisterOnSubmitStatement (string key, string script)
  441. {
  442. throw new NotImplementedException ();
  443. }
  444. public void RegisterRequiresPostBack (Control control)
  445. {
  446. if (_requiresPostBack == null)
  447. _requiresPostBack = new ArrayList ();
  448. _requiresPostBack.Add (control);
  449. }
  450. public virtual void RegisterRequiresRaiseEvent (IPostBackEventHandler control)
  451. {
  452. if (requiresRaiseEvent == null)
  453. requiresRaiseEvent = new ArrayList ();
  454. requiresRaiseEvent.Add (control);
  455. }
  456. [MonoTODO]
  457. public void RegisterViewStateHandler ()
  458. {
  459. // Do nothing
  460. }
  461. protected virtual void SavePageStateToPersistenceMedium (object viewState)
  462. {
  463. _savedViewState = viewState;
  464. }
  465. protected virtual object LoadPageStateFromPersistenceMedium ()
  466. {
  467. return _savedViewState;
  468. }
  469. internal void LoadPageViewState()
  470. {
  471. object sState = LoadPageStateFromPersistenceMedium ();
  472. if (sState != null) {
  473. Pair pair = (Pair) sState;
  474. LoadViewStateRecursive (pair.First);
  475. _requiresPostBack = pair.Second as ArrayList;
  476. }
  477. }
  478. internal void SavePageViewState ()
  479. {
  480. Pair pair = new Pair ();
  481. pair.First = SaveViewStateRecursive ();
  482. pair.Second = _requiresPostBack;
  483. SavePageStateToPersistenceMedium (pair);
  484. }
  485. public virtual void Validate ()
  486. {
  487. if (_validators == null || _validators.Count == 0){
  488. _isValid = true;
  489. return;
  490. }
  491. bool all_valid = true;
  492. foreach (IValidator v in _validators){
  493. v.Validate ();
  494. if (v.IsValid == false)
  495. all_valid = false;
  496. }
  497. if (all_valid)
  498. _isValid = true;
  499. }
  500. public virtual void VerifyRenderingInServerForm (Control control)
  501. {
  502. if (!_renderingForm)
  503. throw new HttpException ("Control '" + control.ClientID + " " + control.GetType () +
  504. "' must be rendered within a HtmlForm");
  505. }
  506. #endregion
  507. }
  508. }