Page.cs 15 KB

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