Page.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  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; FIXME: the correct is this one. commented out to let xsp work
  237. coll = _context.Request.QueryString;
  238. else
  239. coll = _context.Request.QueryString;
  240. if (coll == null || coll ["__VIEWSTATE"] == null)
  241. return null;
  242. return coll;
  243. }
  244. [MonoTODO]
  245. public string GetPostBackClientEvent (Control control, string argument)
  246. {
  247. // Don't throw the exception. keep going
  248. //throw new NotImplementedException ();
  249. StringBuilder result = new StringBuilder ();
  250. result.AppendFormat ("GetPostBackClientEvent ('{0}', '{1}')", control.ID, argument);
  251. return result.ToString ();
  252. }
  253. public string GetPostBackClientHyperlink (Control control, string argument)
  254. {
  255. return "javascript:" + GetPostBackEventReference (control, argument);
  256. }
  257. public string GetPostBackEventReference (Control control)
  258. {
  259. return GetPostBackEventReference (control, "");
  260. }
  261. public string GetPostBackEventReference (Control control, string argument)
  262. {
  263. RequiresPostBackScript ();
  264. return String.Format ("__doPostBack ('{0}', '{1}')", control.ID, argument);
  265. }
  266. internal void RequiresPostBackScript ()
  267. {
  268. requiresPostBackScript = true;
  269. }
  270. public virtual int GetTypeHashCode ()
  271. {
  272. return GetHashCode ();
  273. }
  274. [MonoTODO]
  275. protected virtual void InitOutputCache (int duration,
  276. string varyByHeader,
  277. string varyByCustom,
  278. OutputCacheLocation location,
  279. string varyByParam)
  280. {
  281. throw new NotImplementedException ();
  282. }
  283. [MonoTODO]
  284. public bool IsClientScriptBlockRegistered (string key)
  285. {
  286. throw new NotImplementedException ();
  287. }
  288. [MonoTODO]
  289. public bool IsStartupScriptRegistered (string key)
  290. {
  291. throw new NotImplementedException ();
  292. }
  293. [MonoTODO]
  294. public string MapPath (string virtualPath)
  295. {
  296. throw new NotImplementedException ();
  297. }
  298. MethodInfo [] autoEventsMethods = null;
  299. private void InvokeEventMethod (string m_name, object sender, EventArgs e)
  300. {
  301. if (autoEventsMethods == null) {
  302. BindingFlags bf = BindingFlags.Public | BindingFlags.NonPublic |
  303. BindingFlags.DeclaredOnly | BindingFlags.Instance;
  304. MethodInfo [] m1 = GetType ().GetMethods (bf);
  305. bf = BindingFlags.Public | BindingFlags.NonPublic |
  306. BindingFlags.DeclaredOnly | BindingFlags.Static;
  307. MethodInfo [] m2 = GetType ().GetMethods (bf);
  308. autoEventsMethods = new MethodInfo [m1.Length + m2.Length];
  309. m1.CopyTo (autoEventsMethods, 0);
  310. m2.CopyTo (autoEventsMethods, m1.Length);
  311. }
  312. foreach (MethodInfo m in autoEventsMethods) {
  313. if (m.ReturnType != typeof (void))
  314. continue;
  315. if (m.Name != m_name)
  316. continue;
  317. ParameterInfo [] pi = m.GetParameters ();
  318. if (pi.Length != 2)
  319. continue;
  320. if (pi [0].ParameterType != typeof (object) ||
  321. pi [1].ParameterType != typeof (EventArgs))
  322. continue;
  323. object [] parms = new object [2];
  324. parms [0] = sender;
  325. parms [1] = e;
  326. m.Invoke (this, parms);
  327. break;
  328. }
  329. }
  330. private void RenderPostBackScript (HtmlTextWriter writer, string formUniqueID)
  331. {
  332. writer.WriteLine ("<input type=\"hidden\" name=\"__EVENTTARGET\" value=\"\" />");
  333. writer.WriteLine ("<input type=\"hidden\" name=\"__EVENTARGUMENT\" value=\"\" />");
  334. writer.WriteLine ();
  335. writer.WriteLine ("<script language=\"javascript\">");
  336. writer.WriteLine ("<!--");
  337. writer.WriteLine ("\tfunction __doPostBack(eventTarget, eventArgument) {");
  338. writer.WriteLine ("\t\tvar theform = document.{0};", formUniqueID);
  339. writer.WriteLine ("\t\ttheform.__EVENTTARGET.value = eventTarget;");
  340. writer.WriteLine ("\t\ttheform.__EVENTARGUMENT.value = eventArgument;");
  341. writer.WriteLine ("\t\ttheform.submit();");
  342. writer.WriteLine ("\t}");
  343. writer.WriteLine ("// -->");
  344. writer.WriteLine ("</script>");
  345. }
  346. private bool got_state = false;
  347. private int _random;
  348. private int queryStringHash;
  349. internal void OnFormRender (HtmlTextWriter writer, string formUniqueID)
  350. {
  351. if (renderingForm)
  352. throw new HttpException ("Only 1 HtmlForm is allowed per page.");
  353. renderingForm = true;
  354. writer.WriteLine ();
  355. writer.Write ("<input type=\"hidden\" name=\"__VIEWSTATE\" ");
  356. writer.WriteLine ("value=\"{0}\" />", GetViewStateString ());
  357. if (requiresPostBackScript) {
  358. RenderPostBackScript (writer, formUniqueID);
  359. postBackScriptRendered = true;
  360. }
  361. }
  362. public string GetViewStateString ()
  363. {
  364. StringBuilder state_string = new StringBuilder ();
  365. state_string.AppendFormat ("{0:X}", GetTypeHashCode ());
  366. state_string.AppendFormat ("{0:X}", queryStringHash);
  367. if (!got_state) {
  368. Random rnd = new Random ();
  369. _random = rnd.Next ();
  370. if (_random < 0)
  371. _random = -_random;
  372. _random++;
  373. got_state = true;
  374. }
  375. state_string.AppendFormat ("{0:X}", _random);
  376. return state_string.ToString ();
  377. }
  378. internal void OnFormPostRender (HtmlTextWriter writer, string formUniqueID)
  379. {
  380. if (!postBackScriptRendered && requiresPostBackScript)
  381. RenderPostBackScript (writer, formUniqueID);
  382. renderingForm = false;
  383. postBackScriptRendered = false;
  384. }
  385. private void _Page_Init (object sender, EventArgs e)
  386. {
  387. InvokeEventMethod ("Page_Init", sender, e);
  388. }
  389. private void _Page_Load (object sender, EventArgs e)
  390. {
  391. InvokeEventMethod ("Page_Load", sender, e);
  392. }
  393. private void _Page_DataBind (object sender, EventArgs e)
  394. {
  395. InvokeEventMethod ("Page_DataBind", sender, e);
  396. }
  397. private void _Page_PreRender (object sender, EventArgs e)
  398. {
  399. InvokeEventMethod ("Page_PreRender", sender, e);
  400. }
  401. private void _Page_Dispose (object sender, EventArgs e)
  402. {
  403. InvokeEventMethod ("Page_Dispose", sender, e);
  404. }
  405. private void _Page_Error (object sender, EventArgs e)
  406. {
  407. InvokeEventMethod ("Page_Error", sender, e);
  408. }
  409. private void ProcessPostData (NameValueCollection data, bool second)
  410. {
  411. if (data == null)
  412. return;
  413. foreach (string id in data.AllKeys){
  414. if (id == "__VIEWSTATE")
  415. continue;
  416. Control ctrl = FindControl (id);
  417. if (ctrl != null){
  418. IPostBackDataHandler pbdh = ctrl as IPostBackDataHandler;
  419. IPostBackEventHandler pbeh = ctrl as IPostBackEventHandler;
  420. if (pbdh == null) {
  421. if (pbeh != null)
  422. RegisterRequiresRaiseEvent (pbeh);
  423. continue;
  424. }
  425. if (pbdh.LoadPostData (id, data) == true) {
  426. if (requiresPostDataChanged == null)
  427. requiresPostDataChanged = new ArrayList ();
  428. requiresPostDataChanged.Add (pbdh);
  429. }
  430. } else if (!second) {
  431. if (secondPostData == null)
  432. secondPostData = new NameValueCollection ();
  433. secondPostData.Add (id, null);
  434. }
  435. }
  436. }
  437. private bool init_done;
  438. public void ProcessRequest (HttpContext context)
  439. {
  440. if (!init_done){
  441. init_done = true;
  442. // These should depend on AutoEventWireUp in Page directive. Defaults to true.
  443. Init += new EventHandler (_Page_Init);
  444. Load += new EventHandler (_Page_Load);
  445. DataBinding += new EventHandler (_Page_DataBind);
  446. PreRender += new EventHandler (_Page_PreRender);
  447. Disposed += new EventHandler (_Page_Dispose);
  448. Error += new EventHandler (_Page_Error);
  449. }
  450. //-- Control execution lifecycle in the docs
  451. Controls.Clear ();
  452. FrameworkInitialize ();
  453. InitRecursive (null);
  454. got_state = false;
  455. renderingForm = false;
  456. _context = context;
  457. queryStringHash = _context.Request.QueryString.GetHashCode ();
  458. if (IsPostBack) {
  459. LoadPageViewState ();
  460. ProcessPostData (DeterminePostBackMode (), false);
  461. }
  462. LoadRecursive ();
  463. if (IsPostBack) {
  464. ProcessPostData (secondPostData, true);
  465. RaiseChangedEvents ();
  466. RaisePostBackEvents ();
  467. }
  468. PreRenderRecursiveInternal ();
  469. SavePageViewState ();
  470. //--
  471. StringBuilder sb = new StringBuilder ();
  472. StringWriter sr = new StringWriter (sb);
  473. HtmlTextWriter output = new HtmlTextWriter (context.Response.Output);
  474. RenderControl (output);
  475. _context = null;
  476. }
  477. internal void RaisePostBackEvents ()
  478. {
  479. NameValueCollection postdata = DeterminePostBackMode ();
  480. string eventTarget = postdata ["__EVENTTARGET"];
  481. if (eventTarget != null && eventTarget.Length > 0) {
  482. Control target = FindControl (eventTarget);
  483. if (!(target is IPostBackEventHandler))
  484. return;
  485. string eventArgument = postdata ["__EVENTARGUMENT"];
  486. RaisePostBackEvent ((IPostBackEventHandler) target, eventArgument);
  487. return;
  488. }
  489. if (requiresRaiseEvent == null)
  490. return;
  491. foreach (Control c in requiresRaiseEvent)
  492. RaisePostBackEvent ((IPostBackEventHandler) c, postdata [c.ID]);
  493. requiresRaiseEvent.Clear ();
  494. }
  495. internal void RaiseChangedEvents ()
  496. {
  497. if (requiresPostDataChanged == null)
  498. return;
  499. foreach (IPostBackDataHandler ipdh in requiresPostDataChanged)
  500. ipdh.RaisePostDataChangedEvent ();
  501. requiresPostDataChanged.Clear ();
  502. }
  503. protected virtual void RaisePostBackEvent (IPostBackEventHandler sourceControl, string eventArgument)
  504. {
  505. sourceControl.RaisePostBackEvent (eventArgument);
  506. }
  507. [MonoTODO]
  508. public void RegisterArrayDeclaration (string arrayName, string arrayValue)
  509. {
  510. throw new NotImplementedException ();
  511. }
  512. [MonoTODO]
  513. public virtual void RegisterClientScriptBlock (string key, string script)
  514. {
  515. throw new NotImplementedException ();
  516. }
  517. [MonoTODO]
  518. public virtual void RegisterHiddenField (string hiddenFieldName, string hiddenFieldInitialValue)
  519. {
  520. throw new NotImplementedException ();
  521. }
  522. [MonoTODO]
  523. public void RegisterClientScriptFile (string a, string b, string c)
  524. {
  525. throw new NotImplementedException ();
  526. }
  527. [MonoTODO]
  528. public void RegisterOnSubmitStatement (string key, string script)
  529. {
  530. throw new NotImplementedException ();
  531. }
  532. public void RegisterRequiresPostBack (Control control)
  533. {
  534. if (_requiresPostBack == null)
  535. _requiresPostBack = new ArrayList ();
  536. _requiresPostBack.Add (control);
  537. }
  538. public virtual void RegisterRequiresRaiseEvent (IPostBackEventHandler control)
  539. {
  540. if (requiresRaiseEvent == null)
  541. requiresRaiseEvent = new ArrayList ();
  542. requiresRaiseEvent.Add (control);
  543. }
  544. [MonoTODO]
  545. public void RegisterViewStateHandler ()
  546. {
  547. // Do nothing
  548. }
  549. protected virtual void SavePageStateToPersistenceMedium (object viewState)
  550. {
  551. _savedViewState = viewState;
  552. }
  553. protected virtual object LoadPageStateFromPersistenceMedium ()
  554. {
  555. return _savedViewState;
  556. }
  557. internal void LoadPageViewState()
  558. {
  559. object sState = LoadPageStateFromPersistenceMedium ();
  560. if (sState != null) {
  561. Pair pair = (Pair) sState;
  562. LoadViewStateRecursive (pair.First);
  563. _requiresPostBack = pair.Second as ArrayList;
  564. }
  565. }
  566. internal void SavePageViewState ()
  567. {
  568. Pair pair = new Pair ();
  569. pair.First = SaveViewStateRecursive ();
  570. pair.Second = _requiresPostBack;
  571. SavePageStateToPersistenceMedium (pair);
  572. }
  573. public virtual void Validate ()
  574. {
  575. if (_validators == null || _validators.Count == 0){
  576. _isValid = true;
  577. return;
  578. }
  579. bool all_valid = true;
  580. foreach (IValidator v in _validators){
  581. v.Validate ();
  582. if (v.IsValid == false)
  583. all_valid = false;
  584. }
  585. if (all_valid)
  586. _isValid = true;
  587. }
  588. public virtual void VerifyRenderingInServerForm (Control control)
  589. {
  590. if (!renderingForm)
  591. throw new HttpException ("Control '" + control.ClientID + " " + control.GetType () +
  592. "' must be rendered within a HtmlForm");
  593. }
  594. #endregion
  595. }
  596. }