Page.cs 15 KB

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